From e47608f5199b84ea6f40a5f29b23efd18154d53f Mon Sep 17 00:00:00 2001
From: Andreas Ellewsen <ae@uio.no>
Date: Wed, 10 Nov 2021 11:14:34 +0100
Subject: [PATCH] Reduce notification amount for roles

If other fields than one of the date fields was changed, an extra task
would be created, causing duplicate notification on the date. We now
check that the field has changed before making a task.
---
 greg/signals.py | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/greg/signals.py b/greg/signals.py
index 9cf2a8ea..5780e614 100644
--- a/greg/signals.py
+++ b/greg/signals.py
@@ -129,19 +129,23 @@ def save_notification_callback(sender, instance, created, *args, **kwargs):
         return
     # Queue future notifications on start and end date for roles
     if isinstance(instance, Role):
-        if instance.start_date:
+        if (
+            "start_date" in instance._changed_fields  # pylint: disable=protected-access
+            and instance.start_date
+        ):
             Schedule.objects.create(
                 func="greg.signals._queue_role_start_notification",
                 args=f"{instance.id},True",
                 next_run=date_to_datetime_midnight(instance.start_date),
                 schedule_type=Schedule.ONCE,
             )
-        Schedule.objects.create(
-            func="greg.signals._queue_role_end_notification",
-            args=f"{instance.id},True",
-            next_run=date_to_datetime_midnight(instance.end_date),
-            schedule_type=Schedule.ONCE,
-        )
+        if "end_date" in instance._changed_fields:  # pylint: disable=protected-access
+            Schedule.objects.create(
+                func="greg.signals._queue_role_end_notification",
+                args=f"{instance.id},True",
+                next_run=date_to_datetime_midnight(instance.end_date),
+                schedule_type=Schedule.ONCE,
+            )
     meta = _create_metadata(instance)
     operation = "add" if created else "update"
     _store_notification(
-- 
GitLab