Skip to content

Commit

Permalink
Merge pull request #1546 from IFRCGo/feature/no-next-email-to-the-not…
Browse files Browse the repository at this point in the history
…ified-people

No DREF notification again to the already notified users
  • Loading branch information
szabozoltan69 authored Oct 4, 2022
2 parents 2d35f82 + e33d29f commit cfc223c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
24 changes: 18 additions & 6 deletions dref/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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


Expand Down
11 changes: 5 additions & 6 deletions dref/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cfc223c

Please sign in to comment.