From b492bd5bd2db29058409f00daa35290230ef2427 Mon Sep 17 00:00:00 2001 From: Ross Poulton Date: Tue, 11 Aug 2009 09:02:48 +0000 Subject: [PATCH] Issue #90: Users can receive multiple e-mails. If an e-mail address is used in the 'Queue CC' set up and then that e-mail address is used as a submitter for a ticket, they received two emails. Worse, if that user was ALSO the 'assigned to' person for that ticket, they got THREE emails! This fix will ensure a particular email goes to an address only once. Thanks to Andreas Kotowicz for this patch. --- forms.py | 19 ++++++++++++++----- views/api.py | 16 ++++++++++++---- views/staff.py | 16 ++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/forms.py b/forms.py index 5641aad2..1f53df16 100644 --- a/forms.py +++ b/forms.py @@ -133,6 +133,8 @@ class TicketForm(forms.Form): 'ticket': t, 'queue': q, } + + messages_sent_to = [] if t.submitter_email: send_templated_mail( @@ -143,8 +145,9 @@ class TicketForm(forms.Form): fail_silently=True, files=files, ) + messages_sent_to.append(t.submitter_email) - if t.assigned_to and t.assigned_to != user and getattr(t.assigned_to.usersettings.settings, 'email_on_ticket_assign', False): + if t.assigned_to and t.assigned_to != user and getattr(t.assigned_to.usersettings.settings, 'email_on_ticket_assign', False) and t.assigned_to.email and t.assigned_to.email not in messages_sent_to: send_templated_mail( 'assigned_owner', context, @@ -153,8 +156,9 @@ class TicketForm(forms.Form): fail_silently=True, files=files, ) + messages_sent_to.append(t.assigned_to.email) - if q.new_ticket_cc: + if q.new_ticket_cc and q.new_ticket_cc not in messages_sent_to: send_templated_mail( 'newticket_cc', context, @@ -163,8 +167,9 @@ class TicketForm(forms.Form): fail_silently=True, files=files, ) + messages_sent_to.append(q.new_ticket_cc) - if q.updated_ticket_cc and q.updated_ticket_cc != q.new_ticket_cc: + if q.updated_ticket_cc and q.updated_ticket_cc != q.new_ticket_cc and q.updated_ticket_cc not in messages_sent_to: send_templated_mail( 'newticket_cc', context, @@ -272,6 +277,8 @@ class PublicTicketForm(forms.Form): 'queue': q, } + messages_sent_to = [] + send_templated_mail( 'newticket_submitter', context, @@ -280,8 +287,9 @@ class PublicTicketForm(forms.Form): fail_silently=True, files=files, ) + messages_sent_to.append(t.submitter_email) - if q.new_ticket_cc: + if q.new_ticket_cc and q.new_ticket_cc not in messages_sent_to: send_templated_mail( 'newticket_cc', context, @@ -290,8 +298,9 @@ class PublicTicketForm(forms.Form): fail_silently=True, files=files, ) + messages_sent_to.append(q.new_ticket_cc) - if q.updated_ticket_cc and q.updated_ticket_cc != q.new_ticket_cc: + if q.updated_ticket_cc and q.updated_ticket_cc != q.new_ticket_cc and q.updated_ticket_cc not in messages_sent_to: send_templated_mail( 'newticket_cc', context, diff --git a/views/api.py b/views/api.py index 9374f572..017c51ba 100644 --- a/views/api.py +++ b/views/api.py @@ -196,6 +196,8 @@ class API: 'queue': ticket.queue, 'comment': f.comment, } + + messages_sent_to = [] if public and ticket.submitter_email: send_templated_mail( @@ -205,8 +207,9 @@ class API: sender=ticket.queue.from_address, fail_silently=True, ) + messages_sent_to.append(ticket.submitter_email) - if ticket.queue.updated_ticket_cc: + if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to: send_templated_mail( 'updated_cc', context, @@ -214,8 +217,9 @@ class API: sender=ticket.queue.from_address, fail_silently=True, ) + messages_sent_to.append(ticket.queue.updated_ticket_cc) - if ticket.assigned_to and self.request.user != ticket.assigned_to and getattr(ticket.assigned_to.usersettings.settings, 'email_on_ticket_apichange', False): + if ticket.assigned_to and self.request.user != ticket.assigned_to and getattr(ticket.assigned_to.usersettings.settings, 'email_on_ticket_apichange', False) and ticket.assigned_to.email and ticket.assigned_to.email not in messages_sent_to: send_templated_mail( 'updated_owner', context, @@ -257,6 +261,8 @@ class API: } subject = '%s %s (Resolved)' % (ticket.ticket, ticket.title) + + messages_sent_to = [] if ticket.submitter_email: send_templated_mail( @@ -266,8 +272,9 @@ class API: sender=ticket.queue.from_address, fail_silently=True, ) + messages_sent_to.append(ticket.submitter_email) - if ticket.queue.updated_ticket_cc: + if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to: send_templated_mail( 'resolved_cc', context, @@ -275,8 +282,9 @@ class API: sender=ticket.queue.from_address, fail_silently=True, ) + messages_sent_to.append(ticket.queue.updated_ticket_cc) - if ticket.assigned_to and self.request.user != ticket.assigned_to and getattr(ticket.assigned_to.usersettings.settings, 'email_on_ticket_apichange', False): + if ticket.assigned_to and self.request.user != ticket.assigned_to and getattr(ticket.assigned_to.usersettings.settings, 'email_on_ticket_apichange', False) and ticket.assigned_to.email and ticket.assigned_to.email not in messages_sent_to: send_templated_mail( 'resolved_resolved', context, diff --git a/views/staff.py b/views/staff.py index fbebfc94..6eba78b6 100644 --- a/views/staff.py +++ b/views/staff.py @@ -233,6 +233,8 @@ def update_ticket(request, ticket_id, public=False): if f.new_status == Ticket.RESOLVED_STATUS: ticket.resolution = comment + messages_sent_to = [] + if ticket.submitter_email and public and (f.comment or (f.new_status in (Ticket.RESOLVED_STATUS, Ticket.CLOSED_STATUS))): context.update( resolution=ticket.resolution, @@ -254,8 +256,9 @@ def update_ticket(request, ticket_id, public=False): fail_silently=True, files=files, ) + messages_sent_to.append(ticket.submitter_email) - if ticket.assigned_to and request.user != ticket.assigned_to and ticket.assigned_to.email: + if ticket.assigned_to and request.user != ticket.assigned_to and ticket.assigned_to.email and ticket.assigned_to.email not in messages_sent_to: # We only send e-mails to staff members if the ticket is updated by # another user. The actual template varies, depending on what has been # changed. @@ -277,8 +280,9 @@ def update_ticket(request, ticket_id, public=False): fail_silently=True, files=files, ) + messages_sent_to.append(ticket.assigned_to.email) - if ticket.queue.updated_ticket_cc: + if ticket.queue.updated_ticket_cc and ticket.queue.updated_ticket_cc not in messages_sent_to: if reassigned: template_cc = 'assigned_cc' elif f.new_status == Ticket.RESOLVED_STATUS: @@ -347,6 +351,8 @@ def mass_update(request): 'resolution': t.resolution, } + messages_sent_to = [] + if t.submitter_email: send_templated_mail( 'closed_submitter', @@ -355,8 +361,9 @@ def mass_update(request): sender=t.queue.from_address, fail_silently=True, ) + messages_sent_to.append(t.submitter_email) - if t.assigned_to and request.user != t.assigned_to and t.assigned_to.email: + if t.assigned_to and request.user != t.assigned_to and t.assigned_to.email and t.assigned_to.email not in messages_sent_to: send_templated_mail( 'closed_owner', context, @@ -364,8 +371,9 @@ def mass_update(request): sender=t.queue.from_address, fail_silently=True, ) + messages_sent_to.append(t.assigned_to.email) - if t.queue.updated_ticket_cc: + if t.queue.updated_ticket_cc and t.queue.updated_ticket_cc not in messages_sent_to: send_templated_mail( 'closed_cc', context,