]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/app_globals.py
016a9e1eaf0bf360544da778e9f893ebe323bd65
[bluechips.git] / bluechips / lib / app_globals.py
1 """The application's Globals object"""
2
3 import logging
4
5 from pylons import config, request
6 from paste.deploy.converters import asbool
7 from mailer import Message
8
9 log = logging.getLogger(__name__)
10
11 class Globals(object):
12     """Globals acts as a container for objects available throughout the
13     life of the application
14     """
15
16     def __init__(self):
17         """One instance of Globals is created during application
18         initialization and is available during requests via the 'g'
19         variable
20         """
21         pass
22
23     def send_message(self, msg):
24         """
25         Wrap the call to mailer.send() so that we can do stuff like defer mail
26         sending, wrap in a test fixture, selectively disable mailing in certain
27         environments, etc.
28         """
29         if asbool(config.get('testing')) or asbool(config.get('network_free')):
30             if 'mailer.messages' not in request.environ:
31                 request.environ['mailer.messages'] = []
32             request.environ['mailer.messages'].append(msg)
33             log.info("From: %s\nTo: %s\nSubject: %s\n\n%s",
34                      msg.From, msg.To, msg.Subject, msg.Body)
35         else:
36             self.mailer.send(msg)
37
38     def handle_notification(self, users, subject, body):
39         "Send a notification email."
40         recipients = [u.email for u in users if u.email is not None]
41         if len(recipients) > 0:
42             msg = Message(From=config.get('mailer.from',
43                                           'root@localhost'),
44                           To=recipients)
45             msg.Subject = "BlueChips: %s" % subject
46             msg.Body = body
47             self.send_message(msg)