mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Created part.tasks file and moved notify_low_stock function to the same from InvenTree.tasks. The argument type is changed from StockItem to Part
Added trans to headers of table in email template of low_stock_notification.html added is_part_low_on_stock() function to the part model to check if the part's stock has fallen below the minimum quantity used offload_task function to run the low stock notification function asynchronously
This commit is contained in:
parent
fca15a0439
commit
40da41959b
@ -310,30 +310,3 @@ def send_email(subject, body, recipients, from_email=None, html_message=None):
|
||||
fail_silently=False,
|
||||
html_message=html_message
|
||||
)
|
||||
|
||||
|
||||
def notify_low_stock(stock_item):
|
||||
"""
|
||||
Notify users who have starred a part when its stock quantity falls below the minimum threshold
|
||||
"""
|
||||
|
||||
from allauth.account.models import EmailAddress
|
||||
starred_users = EmailAddress.objects.filter(user__starred_parts__part=stock_item.part)
|
||||
|
||||
if len(starred_users) > 0:
|
||||
logger.info(f"Notify users regarding low stock of {stock_item.part.name}")
|
||||
context = {
|
||||
'part_name': stock_item.part.name,
|
||||
# Part url can be used to open the page of part in application from the email.
|
||||
# It can be facilitated when the application base url is accessible programmatically.
|
||||
# 'part_url': f'{application_base_url}/part/{stock_item.part.id}',
|
||||
|
||||
# quantity is in decimal field datatype. Since the same datatype is used in models,
|
||||
# it is not converted to number/integer,
|
||||
'part_quantity': stock_item.quantity,
|
||||
'minimum_quantity': stock_item.part.minimum_stock
|
||||
}
|
||||
subject = _(f'Attention! {stock_item.part.name} is low on stock')
|
||||
html_message = render_to_string('stock/low_stock_notification.html', context)
|
||||
recipients = starred_users.values_list('email', flat=True)
|
||||
send_email(subject, '', recipients, html_message=html_message)
|
||||
|
@ -1988,6 +1988,9 @@ class Part(MPTTModel):
|
||||
def related_count(self):
|
||||
return len(self.get_related_parts())
|
||||
|
||||
def is_part_low_on_stock(self):
|
||||
return self.total_stock <= self.minimum_stock
|
||||
|
||||
|
||||
def attach_file(instance, filename):
|
||||
""" Function for storing a file for a PartAttachment
|
||||
|
39
InvenTree/part/tasks.py
Normal file
39
InvenTree/part/tasks.py
Normal file
@ -0,0 +1,39 @@
|
||||
# Author: Roche Christopher
|
||||
# Created at 10:26 AM on 31/10/21
|
||||
|
||||
import logging
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
from InvenTree import tasks as inventree_tasks
|
||||
from part.models import Part
|
||||
|
||||
logger = logging.getLogger("inventree")
|
||||
|
||||
|
||||
def notify_low_stock(part: Part):
|
||||
"""
|
||||
Notify users who have starred a part when its stock quantity falls below the minimum threshold
|
||||
"""
|
||||
|
||||
from allauth.account.models import EmailAddress
|
||||
starred_users_email = EmailAddress.objects.filter(user__starred_parts__part=part)
|
||||
|
||||
if len(starred_users_email) > 0:
|
||||
logger.info(f"Notify users regarding low stock of {part.name}")
|
||||
context = {
|
||||
'part_name': part.name,
|
||||
# Part url can be used to open the page of part in application from the email.
|
||||
# It can be facilitated when the application base url is accessible programmatically.
|
||||
# 'part_url': f'{application_base_url}/part/{stock_item.part.id}',
|
||||
|
||||
# quantity is in decimal field datatype. Since the same datatype is used in models,
|
||||
# it is not converted to number/integer,
|
||||
'part_quantity': part.total_stock,
|
||||
'minimum_quantity': part.minimum_stock
|
||||
}
|
||||
subject = _(f'Attention! {part.name} is low on stock')
|
||||
html_message = render_to_string('stock/low_stock_notification.html', context)
|
||||
recipients = starred_users_email.values_list('email', flat=True)
|
||||
inventree_tasks.send_email(subject, '', recipients, html_message=html_message)
|
@ -1659,8 +1659,11 @@ def after_save_stock_item(sender, instance: StockItem, **kwargs):
|
||||
starred the part
|
||||
"""
|
||||
|
||||
if instance.quantity <= instance.part.minimum_stock:
|
||||
inventree_tasks.notify_low_stock(instance)
|
||||
if instance.part.is_part_low_on_stock():
|
||||
inventree_tasks.offload_task(
|
||||
'part.tasks.notify_low_stock',
|
||||
instance.part
|
||||
)
|
||||
|
||||
|
||||
class StockItemAttachment(InvenTreeAttachment):
|
||||
|
@ -5,13 +5,13 @@
|
||||
<table style="border-collapse:collapse; width: 80%;margin-left: 10%; font-size: 1rem">
|
||||
|
||||
<tr style="background: aliceblue; height: 4rem;">
|
||||
<th colspan="3" style="padding-bottom: 1rem; font-size: 1.5rem; color:rgb(210,0, 0)">Part low on stock</th>
|
||||
<th colspan="3" style="padding-bottom: 1rem; font-size: 1.5rem; color:rgb(210,0, 0)">{% trans "Part low on stock" %}</th>
|
||||
</tr>
|
||||
|
||||
<tr style="height: 3rem; border-bottom: 1px solid">
|
||||
<th>Part Name</th>
|
||||
<th>Available Quantity</th>
|
||||
<th>Minimum Quantity</th>
|
||||
<th>{% trans "Part Name" %}</th>
|
||||
<th>{% trans "Available Quantity" %}</th>
|
||||
<th>{% trans "Minimum Quantity" %}</th>
|
||||
</tr>
|
||||
|
||||
<tr style="height: 3rem">
|
||||
|
Loading…
Reference in New Issue
Block a user