From 3205527ebe771013fd6e809b9daf588d20cd96ab Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 29 May 2023 21:04:31 +1000 Subject: [PATCH] Change target of PurchaseOrder notification (#4905) * Change target of PurchaseOrder notification - Triggered when order is placed - No longer when order is created * unit test fixes --- InvenTree/order/models.py | 19 ++++++++----------- InvenTree/order/tests.py | 9 +++++++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index f3dc54f16a..8dc71aa011 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -484,6 +484,14 @@ class PurchaseOrder(TotalPriceMixin, Order): trigger_event('purchaseorder.placed', id=self.pk) + # Notify users that the order has been placed + notify_responsible( + self, + PurchaseOrder, + exclude=self.created_by, + content=InvenTreeNotificationBodies.NewOrder + ) + @transaction.atomic def complete_order(self): """Marks the PurchaseOrder as COMPLETE. @@ -677,17 +685,6 @@ class PurchaseOrder(TotalPriceMixin, Order): ) -@receiver(post_save, sender=PurchaseOrder, dispatch_uid='purchase_order_post_save') -def after_save_purchase_order(sender, instance: PurchaseOrder, created: bool, **kwargs): - """Callback function to be executed after a PurchaseOrder is saved.""" - if not InvenTree.ready.canAppAccessDatabase(allow_test=True) or InvenTree.ready.isImportingData(): - return - - if created: - # Notify the responsible users that the purchase order has been created - notify_responsible(instance, sender, exclude=instance.created_by) - - class SalesOrder(TotalPriceMixin, Order): """A SalesOrder represents a list of goods shipped outwards to a customer.""" diff --git a/InvenTree/order/tests.py b/InvenTree/order/tests.py index 1f27ef0b9a..151a2ba631 100644 --- a/InvenTree/order/tests.py +++ b/InvenTree/order/tests.py @@ -367,18 +367,23 @@ class OrderTest(TestCase): - The creating user should *not* receive a notification """ - PurchaseOrder.objects.create( + po = PurchaseOrder.objects.create( supplier=Company.objects.get(pk=1), reference='XYZABC', created_by=get_user_model().objects.get(pk=3), responsible=Owner.create(obj=get_user_model().objects.get(pk=4)), ) + # Initially, no notifications + messages = common.models.NotificationMessage.objects.filter( category='order.new_purchaseorder', ) - self.assertEqual(messages.count(), 1) + self.assertEqual(messages.count(), 0) + + # Place the order + po.place_order() # A notification should have been generated for user 4 (who is a member of group 3) self.assertTrue(messages.filter(user__pk=4).exists())