From e33d29f1831c7ad64ee8f729fd8929099f8763ba Mon Sep 17 00:00:00 2001 From: "Szabo, Zoltan" Date: Tue, 4 Oct 2022 17:26:26 +0200 Subject: [PATCH] No DREF notification again to the already notified users --- dref/serializers.py | 24 ++++++++++++++++++------ dref/tasks.py | 11 +++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/dref/serializers.py b/dref/serializers.py index f0045b6e3..da079cc9a 100644 --- a/dref/serializers.py +++ b/dref/serializers.py @@ -424,10 +424,15 @@ def create(self, validated_data): dref_assessment_report = super().create(validated_data) dref_assessment_report.needs_identified.clear() return dref_assessment_report + if 'users' in validated_data: + to = {u.email for u in validated_data['users']} + else: + to = None dref = super().create(validated_data) - transaction.on_commit( - lambda: send_dref_email.delay(dref.id, 'New') - ) + if to: + transaction.on_commit( + lambda: send_dref_email.delay(dref.id, list(to), 'New') + ) return dref def update(self, instance, validated_data): @@ -458,10 +463,17 @@ def update(self, instance, validated_data): dref_assessment_report = super().update(instance, validated_data) dref_assessment_report.needs_identified.clear() return dref_assessment_report + # we don't send notification again to the already notified users: + if 'users' in validated_data: + to = {u.email for u in validated_data['users'] + if u.email not in {t.email for t in instance.users.iterator()}} + else: + to = None dref = super().update(instance, validated_data) - transaction.on_commit( - lambda: send_dref_email.delay(dref.id, 'Updated') - ) + if to: + transaction.on_commit( + lambda: send_dref_email.delay(dref.id, list(to), 'Updated') + ) return dref diff --git a/dref/tasks.py b/dref/tasks.py index 8debbc00c..188c03143 100644 --- a/dref/tasks.py +++ b/dref/tasks.py @@ -6,15 +6,14 @@ @shared_task -def send_dref_email(dref_id, new_or_updated=''): - instance = Dref.objects.get(id=dref_id) - email_context = get_email_context(instance) - users_emails = [t.email for t in instance.users.iterator()] - if users_emails: +def send_dref_email(dref_id, users_emails, new_or_updated=''): + if dref_id and users_emails: + instance = Dref.objects.get(id=dref_id) + email_context = get_email_context(instance) send_notification( f'{new_or_updated} DREF: {instance.title}', users_emails, render_to_string('email/dref/dref.html', email_context), f'{new_or_updated} DREF' ) - return email_context + return email_context