From 98f31ff4911cfacd0aea381967a9dfc9262c0889 Mon Sep 17 00:00:00 2001 From: Michael Price Date: Thu, 22 Feb 2018 21:51:52 -0400 Subject: [PATCH] Use the get_or_create method instead of a blind create to avoid duplicated notifications for the same event when the user is subscribed to the object's event and to the global event. Signed-off-by: Michael Price --- mayan/apps/events/classes.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mayan/apps/events/classes.py b/mayan/apps/events/classes.py index 6ac26769ab..d26d7ff328 100644 --- a/mayan/apps/events/classes.py +++ b/mayan/apps/events/classes.py @@ -116,9 +116,13 @@ class EventType(object): except PermissionDenied: pass else: - notification = Notification.objects.create(action=result, user=user) + notification, created = Notification.objects.get_or_create( + action=result, user=user + ) else: - notification = Notification.objects.create(action=result, user=user) + notification, created = Notification.objects.get_or_create( + action=result, user=user + ) if result.target: content_type = ContentType.objects.get_for_model(model=result.target) @@ -138,7 +142,10 @@ class EventType(object): except PermissionDenied: pass else: - notification = Notification.objects.create(action=result, user=user) + notification, created = Notification.objects.get_or_create( + action=result, user=user + ) + if not notification and result.action_object: content_type = ContentType.objects.get_for_model(model=result.action_object) @@ -157,7 +164,10 @@ class EventType(object): except PermissionDenied: pass else: - notification = Notification.objects.create(action=result, user=user) + notification, created = Notification.objects.get_or_create( + action=result, user=user + ) + def get_stored_event_type(self): if not self.stored_event_type: