Added post_delete hook to StockItem

moved the business logic of 'deciding if a low stock notification has to be sent' to part.tasks
This commit is contained in:
rocheparadox 2021-11-01 08:25:59 +05:30
parent 60c2aab06d
commit 76c1e936db
2 changed files with 26 additions and 9 deletions

View File

@ -37,3 +37,16 @@ def notify_low_stock(part: Part):
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)
def notify_low_stock_if_required(part: Part):
"""
Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have
starred the part
"""
if part.is_part_low_on_stock():
inventree_tasks.offload_task(
'part.tasks.notify_low_stock',
part
)

View File

@ -17,7 +17,7 @@ from django.db.models import Sum, Q
from django.db.models.functions import Coalesce
from django.core.validators import MinValueValidator
from django.contrib.auth.models import User
from django.db.models.signals import pre_delete, post_save
from django.db.models.signals import pre_delete, post_save, post_delete
from django.dispatch import receiver
from markdownx.models import MarkdownxField
@ -36,12 +36,12 @@ import label.models
from InvenTree.status_codes import StockStatus, StockHistoryCode
from InvenTree.models import InvenTreeTree, InvenTreeAttachment
from InvenTree.fields import InvenTreeModelMoneyField, InvenTreeURLField
from InvenTree import tasks as inventree_tasks
from users.models import Owner
from company import models as CompanyModels
from part import models as PartModels
from part import tasks as part_tasks
class StockLocation(InvenTreeTree):
@ -1652,18 +1652,22 @@ def before_delete_stock_item(sender, instance, using, **kwargs):
child.save()
@receiver(post_delete, sender=StockItem, dispatch_uid='stock_item_post_delete_log')
def after_delete_stock_item(sender, instance: StockItem, **kwargs):
"""
Function to be executed after a StockItem object is deleted
"""
part_tasks.notify_low_stock_if_required(instance.part)
@receiver(post_save, sender=StockItem, dispatch_uid='stock_item_post_save_log')
def after_save_stock_item(sender, instance: StockItem, **kwargs):
"""
Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have
starred the part
Hook function to be executed after StockItem object is saved/updated
"""
if instance.part.is_part_low_on_stock():
inventree_tasks.offload_task(
'part.tasks.notify_low_stock',
instance.part
)
part_tasks.notify_low_stock_if_required(instance.part)
class StockItemAttachment(InvenTreeAttachment):