X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Flib%2Fapp_globals.py;h=016a9e1eaf0bf360544da778e9f893ebe323bd65;hb=25e0dd950ef119f1bf6b7ab0a54c730f4f9f5922;hp=342c8cb8614f1276bf7130ca964bbbb08bddb3ea;hpb=b57666fb3255e06ec0d55cabd26a01c83d214326;p=bluechips.git diff --git a/bluechips/lib/app_globals.py b/bluechips/lib/app_globals.py index 342c8cb..016a9e1 100644 --- a/bluechips/lib/app_globals.py +++ b/bluechips/lib/app_globals.py @@ -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)