]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/lib/app_globals.py
added email notifications. requires schema change to add email column to users table.
[bluechips.git] / bluechips / lib / app_globals.py
index 342c8cb8614f1276bf7130ca964bbbb08bddb3ea..016a9e1eaf0bf360544da778e9f893ebe323bd65 100644 (file)
@@ -1,5 +1,13 @@
 """The application's Globals object"""
 
+import logging
+
+from pylons import config, request
+from paste.deploy.converters import asbool
+from mailer import Message
+
+log = logging.getLogger(__name__)
+
 class Globals(object):
     """Globals acts as a container for objects available throughout the
     life of the application
@@ -11,3 +19,29 @@ class Globals(object):
         variable
         """
         pass
+
+    def send_message(self, msg):
+        """
+        Wrap the call to mailer.send() so that we can do stuff like defer mail
+        sending, wrap in a test fixture, selectively disable mailing in certain
+        environments, etc.
+        """
+        if asbool(config.get('testing')) or asbool(config.get('network_free')):
+            if 'mailer.messages' not in request.environ:
+                request.environ['mailer.messages'] = []
+            request.environ['mailer.messages'].append(msg)
+            log.info("From: %s\nTo: %s\nSubject: %s\n\n%s",
+                     msg.From, msg.To, msg.Subject, msg.Body)
+        else:
+            self.mailer.send(msg)
+
+    def handle_notification(self, users, subject, body):
+        "Send a notification email."
+        recipients = [u.email for u in users if u.email is not None]
+        if len(recipients) > 0:
+            msg = Message(From=config.get('mailer.from',
+                                          'root@localhost'),
+                          To=recipients)
+            msg.Subject = "BlueChips: %s" % subject
+            msg.Body = body
+            self.send_message(msg)