Send notifications individually.
[matthijs/projects/xerxes.git] / tools / notify.py
index bc0b4d84336d95f835f31b6dc1a8a6cb34b22914..50eb375e2dd3b4268c8df43751334e5f594714ac 100644 (file)
@@ -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 <address@domain.nl>
+
+    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: