]> asedeno.scripts.mit.edu Git - bluechips.git/commitdiff
added email notifications. requires schema change to add email column to users table.
authorScott Torborg <scott@crookedmedia.com>
Thu, 5 Nov 2009 06:53:47 +0000 (20:53 -1000)
committerScott Torborg <scott@crookedmedia.com>
Thu, 5 Nov 2009 06:53:47 +0000 (20:53 -1000)
bluechips/config/environment.py
bluechips/controllers/spend.py
bluechips/controllers/transfer.py
bluechips/lib/app_globals.py
bluechips/model/__init__.py
bluechips/templates/emails/expenditure.txt [new file with mode: 0644]
bluechips/templates/emails/transfer.txt [new file with mode: 0644]

index 72a92004bf6c9df94fc8880d137ce0eb6277d6cb..c6ba9f0c0eb9cb30b400900d333ef7dbcac42786 100644 (file)
@@ -4,6 +4,7 @@ import os
 from mako.lookup import TemplateLookup
 from pylons import config
 from sqlalchemy import engine_from_config
+from mailer import Mailer
 
 import bluechips.lib.app_globals as app_globals
 import bluechips.lib.helpers
@@ -42,3 +43,8 @@ def load_environment(global_conf, app_conf):
     
     # CONFIGURATION OPTIONS HERE (note: all config options will override
     # any Pylons config options)
+    config['pylons.app_globals'].mailer = Mailer(config.get('mailer.host',
+                                                            '127.0.0.1'))
+    if 'mailer.user' in config:
+        config['pylons.app_globals'].mailer.login(config['mailer.user'],
+                                                  config['mailer.password'])
index 749d73039a4c34550d5baaadf607315f098b65b8..e309b00410421dd397d27b7dd26f047a1b69af74 100644 (file)
@@ -8,7 +8,7 @@ from decimal import Decimal, InvalidOperation
 
 from bluechips.lib.base import *
 
-from pylons import request
+from pylons import request, app_globals as g
 from pylons.decorators.rest import dispatch_on
 from pylons.decorators import validate
 
@@ -16,6 +16,8 @@ from formencode import validators, Schema
 from formencode.foreach import ForEach
 from formencode.variabledecode import NestedVariables
 
+from mailer import Message
+
 log = logging.getLogger(__name__)
 
 
@@ -71,8 +73,10 @@ class SpendController(BaseController):
         if id is None:
             e = model.Expenditure()
             meta.Session.add(e)
+            op = 'created'
         else:
             e = meta.Session.query(model.Expenditure).get(id)
+            op = 'updated'
         
         # Set the fields that were submitted
         shares = self.form_result.pop('shares')
@@ -88,10 +92,17 @@ class SpendController(BaseController):
         e.split(split_dict)
         
         meta.Session.commit()
-        
-        if id is None:
-            h.flash("Expenditure created.")
-        else:
-            h.flash('Expenditure updated.')
        
+        show = ("Expenditure of %s paid for by %s %s." %
+                (e.amount, e.spender, op))
+        h.flash(show)
+
+        # Send email notification to involved users if they have an email set.
+        involved_users = set(sp.user for sp in e.splits if sp.share != 0)
+        involved_users.add(e.spender)
+        body = render('/emails/expenditure.txt',
+                      extra_vars={'expenditure': e,
+                                  'op': op})
+        g.handle_notification(involved_users, show, body)
+
         return h.redirect_to('/')
index 0fb9a557f6948b8899e69b602b2bd1f43259d34c..016f508df07f7d379d468db895be23454dd2ec8e 100644 (file)
@@ -8,11 +8,13 @@ from datetime import date
 
 from bluechips.lib.base import *
 
-from pylons import request
+from pylons import request, app_globals as g
 from pylons.decorators import validate
 
 from formencode import Schema, validators
 
+from mailer import Message
+
 log = logging.getLogger(__name__)
 
 
@@ -47,15 +49,21 @@ class TransferController(BaseController):
         if id is None:
             t = model.Transfer()
             meta.Session.add(t)
+            op = 'created'
         else:
             t = meta.Session.query(model.Transfer).get(id)
+            op = 'updated'
         
         update_sar(t, self.form_result)
         meta.Session.commit()
        
-        if id is None:
-            h.flash('Transfer created.')
-        else:
-            h.flash('Transfer updated.')
-        
+        show = ('Transfer of %s from %s to %s %s.' %
+                (t.amount, t.debtor, t.creditor, op))
+        h.flash(show)
+
+        # Send email notification to involved users if they have an email set.
+        body = render('/emails/transfer.txt', extra_vars={'transfer': t,
+                                                          'op': op})
+        g.handle_notification((t.debtor, t.creditor), show, body)
+
         return h.redirect_to('/')
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)
index ba5404f5ff4277185b5b707ec6d969b61a0e3a3d..8efaec6ae9abcdcfaeea8cfcab585797c0cfbaac 100644 (file)
@@ -27,7 +27,8 @@ users = sa.Table('users', meta.metadata,
                  sa.Column('id', sa.types.Integer, primary_key=True),
                  sa.Column('username', sa.types.Unicode(32), nullable=False),
                  sa.Column('name', sa.types.Unicode(64)),
-                 sa.Column('resident', sa.types.Boolean, default=True)
+                 sa.Column('resident', sa.types.Boolean, default=True),
+                 sa.Column('email', sa.types.Unicode(64))
                  )
 
 expenditures = sa.Table('expenditures', meta.metadata,
diff --git a/bluechips/templates/emails/expenditure.txt b/bluechips/templates/emails/expenditure.txt
new file mode 100644 (file)
index 0000000..bca63ff
--- /dev/null
@@ -0,0 +1,14 @@
+The following expenditure was ${op}:
+
+${expenditure.amount} paid for by ${expenditure.spender}
+
+Description:
+${expenditure.description}
+
+The shares of this expenditure are:
+% for split in expenditure.splits:
+${split.user}: ${split.share}
+% endfor
+
+To view or edit this expenditure, visit:
+${h.url_for(controller='spend', action='edit', id=expenditure.id, qualified=True)}
diff --git a/bluechips/templates/emails/transfer.txt b/bluechips/templates/emails/transfer.txt
new file mode 100644 (file)
index 0000000..6fffa93
--- /dev/null
@@ -0,0 +1,9 @@
+The following transfer was ${op}:
+
+${transfer.amount} from ${transfer.debtor} to ${transfer.creditor}
+
+Description:
+${transfer.description}
+
+To view or edit this transfer, visit:
+${h.url_for(controller='transfer', action='edit', id=transfer.id, qualified=True)}