Skip to content

Commit cfc223c

Browse files
Merge pull request #1546 from IFRCGo/feature/no-next-email-to-the-notified-people
No DREF notification again to the already notified users
2 parents 2d35f82 + e33d29f commit cfc223c

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

dref/serializers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,15 @@ def create(self, validated_data):
424424
dref_assessment_report = super().create(validated_data)
425425
dref_assessment_report.needs_identified.clear()
426426
return dref_assessment_report
427+
if 'users' in validated_data:
428+
to = {u.email for u in validated_data['users']}
429+
else:
430+
to = None
427431
dref = super().create(validated_data)
428-
transaction.on_commit(
429-
lambda: send_dref_email.delay(dref.id, 'New')
430-
)
432+
if to:
433+
transaction.on_commit(
434+
lambda: send_dref_email.delay(dref.id, list(to), 'New')
435+
)
431436
return dref
432437

433438
def update(self, instance, validated_data):
@@ -458,10 +463,17 @@ def update(self, instance, validated_data):
458463
dref_assessment_report = super().update(instance, validated_data)
459464
dref_assessment_report.needs_identified.clear()
460465
return dref_assessment_report
466+
# we don't send notification again to the already notified users:
467+
if 'users' in validated_data:
468+
to = {u.email for u in validated_data['users']
469+
if u.email not in {t.email for t in instance.users.iterator()}}
470+
else:
471+
to = None
461472
dref = super().update(instance, validated_data)
462-
transaction.on_commit(
463-
lambda: send_dref_email.delay(dref.id, 'Updated')
464-
)
473+
if to:
474+
transaction.on_commit(
475+
lambda: send_dref_email.delay(dref.id, list(to), 'Updated')
476+
)
465477
return dref
466478

467479

dref/tasks.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77

88
@shared_task
9-
def send_dref_email(dref_id, new_or_updated=''):
10-
instance = Dref.objects.get(id=dref_id)
11-
email_context = get_email_context(instance)
12-
users_emails = [t.email for t in instance.users.iterator()]
13-
if users_emails:
9+
def send_dref_email(dref_id, users_emails, new_or_updated=''):
10+
if dref_id and users_emails:
11+
instance = Dref.objects.get(id=dref_id)
12+
email_context = get_email_context(instance)
1413
send_notification(
1514
f'{new_or_updated} DREF: {instance.title}',
1615
users_emails,
1716
render_to_string('email/dref/dref.html', email_context),
1817
f'{new_or_updated} DREF'
1918
)
20-
return email_context
19+
return email_context

0 commit comments

Comments
 (0)