From: Matthijs Kooijman Date: Sat, 31 Jan 2009 10:14:40 +0000 (+0100) Subject: Send notifications individually. X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=8a87b4e71401028038260f4ec79788091a98143e;p=matthijs%2Fprojects%2Fxerxes.git Send notifications individually. Previously, a notification was sent as a single message with multiple recipients. Now, each of the recipients gets his or her own message. This prepares for sending notification to many users. --- diff --git a/templates/influences/email/character_changed.html b/templates/influences/email/character_changed.html index c858119..524f64d 100644 --- a/templates/influences/email/character_changed.html +++ b/templates/influences/email/character_changed.html @@ -9,8 +9,8 @@ Subject: {% blocktrans %}Character "{{ character }}" created.{% endblocktrans %} Subject: {% blocktrans %}Character "{{ character }}" was changed.{% endblocktrans %} {% endif%} \\ -{% if recipients.0.first_name %} -{% blocktrans with recipients.0.first_name as name %}Hello {{ name }}{% endblocktrans %}, +{% if first_name %} +{% blocktrans %}Hello {{ first_name }}{% endblocktrans %}, {% else %} {% trans "L.S." %}, {% endif %} diff --git a/templates/influences/email/influence_changed.html b/templates/influences/email/influence_changed.html index 3c56337..ee08310 100644 --- a/templates/influences/email/influence_changed.html +++ b/templates/influences/email/influence_changed.html @@ -9,8 +9,8 @@ Subject: {% blocktrans %}Influence "{{ influence }}" submitted.{% endblocktrans Subject: {% blocktrans %}Influence "{{ influence }}" was changed.{% endblocktrans %} {% endif%} \\ -{% if recipients.0.first_name %} -{% blocktrans with recipients.0.first_name as name %}Hello {{ name }}{% endblocktrans %}, +{% if first_name %} +{% blocktrans %}Hello {{ first_name }}{% endblocktrans %}, {% else %} {% trans "L.S." %}, {% endif %} diff --git a/templates/influences/email/influence_comment_added.html b/templates/influences/email/influence_comment_added.html index 7ebe70c..60b62d2 100644 --- a/templates/influences/email/influence_comment_added.html +++ b/templates/influences/email/influence_comment_added.html @@ -5,8 +5,8 @@ From: Xerxes (Evolution Events) X-Mailer: Xerxes Subject: {% blocktrans %}Comment added to influence "{{ influence }}".{% endblocktrans %} \\ -{% if recipients.0.first_name %} -{% blocktrans with recipients.0.first_name as name %}Hello {{ name }}{% endblocktrans %}, +{% if first_name %} +{% blocktrans %}Hello {{ first_name }}{% endblocktrans %}, {% else %} {% trans "L.S." %}, {% endif %} diff --git a/tools/notify.py b/tools/notify.py index bc0b4d8..50eb375 100644 --- a/tools/notify.py +++ b/tools/notify.py @@ -8,51 +8,69 @@ Notify someone about something. """ def notify(recipients, template, context = {}): recipients = make_iter(recipients) - addresses = []; + # Keep a dict of address -> (firstname, lastname) + to = {}; for r in recipients: if (isinstance(r, User)): - addresses.append(r.email) + to[r.email] = (r.first_name, r.last_name) elif (isinstance(r, Group)): - addresses += [m.email for m in r.user_set.all()] + to.update([(m.email, (m.first_name, m.last_name)) for m in r.user_set.all()]) else: # Assume it is an email address - addresses.append(r) - # TODO: Make addresses unique - - context['recipients'] = recipients - context['addresses'] = addresses - - rendered = loader.render_to_string(template, context) - (headers, body) = rendered.split('\n\n', 1) - - # Turn the headers into a dict so EmailMessage can turn them into a - # string again. Bit pointless, but it works. - # Perhaps we should just use python email stuff directly. OTOH, we - # still always need to parse for the From header. - - headers_dict = {} - # If no From header is present, let EmailMessage do the default - # thing - from_email = None - for header in headers.split('\n'): - (field, value) = header.split(':') - if (field == 'From'): - from_email = value - elif (field == 'Subject'): - subject = value - else: - # Don't put From and Subject in the dict, else they'll be - # present twice. - headers_dict[field] = value - - msg = EmailMessage( - # Only setting the From address through headers won't set the - # envelope address right. - from_email = from_email, - subject = subject, - body = body, - to = addresses, - headers = headers_dict - ) - msg.send() + to[r] = None + print to + for (address, name) in to.items(): + if name is None: + name = (None, None) + + context['first_name'] = name[0] + context['last_name'] = name[1] + + rendered = loader.render_to_string(template, context) + (headers, body) = rendered.split('\n\n', 1) + + # Turn the headers into a dict so EmailMessage can turn them into a + # string again. Bit pointless, but it works. + # Perhaps we should just use python email stuff directly. OTOH, we + # still always need to parse for the From header. + + headers_dict = {} + # If no From header is present, let EmailMessage do the default + # thing + from_email = None + subject = '' + for header in headers.split('\n'): + (field, value) = header.split(':') + if (field == 'From'): + from_email = value + elif (field == 'Subject'): + subject = value + else: + # Don't put From and Subject in the dict, else they'll be + # present twice. + headers_dict[field] = value + print address, name + msg = EmailMessage( + # Only setting the From address through headers won't set the + # envelope address right. + from_email = from_email, + subject = subject, + body = body, + to = [make_rfc822_recipient(address, name)], + headers = headers_dict + ) + msg.send() + +def make_rfc822_recipient(address, name): + """ + Creates a rfc 822 style recipient: Firstname Lastname + + Takes an address and a tuple with firstname and lastname. The tuple can + also be None when no name is known. + """ + if name: + return "%s %s <%s>" % (name[0], name[1], address) + else: + return address + # vim: set sts=4 sw=4 expandtab: