From 76c1e936db78424e0d6953c4062eb32863e302c6 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Mon, 1 Nov 2021 08:25:59 +0530 Subject: [PATCH] Added post_delete hook to StockItem moved the business logic of 'deciding if a low stock notification has to be sent' to part.tasks --- InvenTree/part/tasks.py | 13 +++++++++++++ InvenTree/stock/models.py | 22 +++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index 667e70f1a9..9fc05ec3f1 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -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 + ) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 69b061d25a..657469a744 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -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):