mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
refactor to use general function
This commit is contained in:
parent
95b5eee59d
commit
64c01bff82
@ -2,17 +2,13 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.template.loader import render_to_string
|
|
||||||
|
|
||||||
from allauth.account.models import EmailAddress
|
|
||||||
|
|
||||||
from common.models import NotificationEntry
|
|
||||||
|
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.tasks
|
import InvenTree.tasks
|
||||||
|
import common.notifications
|
||||||
|
|
||||||
import part.models
|
import part.models
|
||||||
|
|
||||||
@ -20,44 +16,23 @@ logger = logging.getLogger("inventree")
|
|||||||
|
|
||||||
|
|
||||||
def notify_low_stock(part: part.models.Part):
|
def notify_low_stock(part: part.models.Part):
|
||||||
"""
|
context = {
|
||||||
Notify users who have starred a part when its stock quantity falls below the minimum threshold
|
# Pass the "Part" object through to the template context
|
||||||
"""
|
'part': part,
|
||||||
|
'link': InvenTree.helpers.construct_absolute_url(part.get_absolute_url()),
|
||||||
|
'templates':{
|
||||||
|
'html': 'email/low_stock_notification.html',
|
||||||
|
'subject': "[InvenTree] " + _("Low stock notification"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
# Check if we have notified recently...
|
common.notifications.trigger_notifaction(
|
||||||
delta = timedelta(days=1)
|
part,
|
||||||
|
'part.notify_low_stock',
|
||||||
if NotificationEntry.check_recent('part.notify_low_stock', part.pk, delta):
|
receiver_fnc=part.get_subscribers,
|
||||||
logger.info(f"Low stock notification has recently been sent for '{part.full_name}' - SKIPPING")
|
notification_context=context,
|
||||||
return
|
|
||||||
|
|
||||||
logger.info(f"Sending low stock notification email for {part.full_name}")
|
|
||||||
|
|
||||||
# Get a list of users who are subcribed to this part
|
|
||||||
subscribers = part.get_subscribers()
|
|
||||||
|
|
||||||
emails = EmailAddress.objects.filter(
|
|
||||||
user__in=subscribers,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: In the future, include the part image in the email template
|
|
||||||
|
|
||||||
if len(emails) > 0:
|
|
||||||
logger.info(f"Notify users regarding low stock of {part.name}")
|
|
||||||
context = {
|
|
||||||
# Pass the "Part" object through to the template context
|
|
||||||
'part': part,
|
|
||||||
'link': InvenTree.helpers.construct_absolute_url(part.get_absolute_url()),
|
|
||||||
}
|
|
||||||
|
|
||||||
subject = "[InvenTree] " + _("Low stock notification")
|
|
||||||
html_message = render_to_string('email/low_stock_notification.html', context)
|
|
||||||
recipients = emails.values_list('email', flat=True)
|
|
||||||
|
|
||||||
InvenTree.tasks.send_email(subject, '', recipients, html_message=html_message)
|
|
||||||
|
|
||||||
NotificationEntry.notify('part.notify_low_stock', part.pk)
|
|
||||||
|
|
||||||
|
|
||||||
def notify_low_stock_if_required(part: part.models.Part):
|
def notify_low_stock_if_required(part: part.models.Part):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user