]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/spend.py
fd884536b1d02811ce0977c40bbbe8c9c9956831
[bluechips.git] / bluechips / controllers / spend.py
1 """
2 Handle expenditures
3 """
4
5 import logging
6
7 from bluechips.lib.base import *
8 from bluechips.widgets import spend
9
10 from pylons import request
11 from pylons.decorators.rest import dispatch_on
12
13 from decimal import Decimal, InvalidOperation
14
15 log = logging.getLogger(__name__)
16
17 class SpendController(BaseController):
18     def index(self):
19         c.title = 'Add a New Expenditure'
20         
21         c.expenditure = dict()
22         c.expenditure['spender'] = request.environ['user']
23         
24         return render('/spend/index.mako')
25     
26     def edit(self, id):
27         c.title = 'Edit an Expenditure'
28         
29         c.expenditure = meta.Session.query(model.Expenditure).get(id)
30         
31         return render('/spend/index.mako')
32     
33     def update(self, id=None):
34         # Validate the submission
35         if not valid(self, spend.new_spend_form):
36             if id is None:
37                 return self.index()
38             else:
39                 return self.edit(id)
40         
41         # Either create a new object, or, if we're editing, get the
42         # old one
43         if id is None:
44             e = model.Expenditure()
45         else:
46             e = meta.Session.query(model.Expenditure).get(id)
47         
48         # Set the fields that were submitted
49         update_sar(e, self.form_result)
50         meta.Session.save_or_update(e)
51         
52         if id is None:
53             e.even_split()
54         else:
55             e.update_split()
56         
57         meta.Session.commit()
58         
59         h.flash('Expenditure recorded.')
60         h.flash("""Want to do something unusual?
61
62 <ul id="expenditure_options">
63   <li>%s</li>
64   <li>%s</li>
65 </ul>""" % (h.link_to('Change the split', h.url_for(controller='spend',
66                                                    action='split',
67                                                    id=e.id)),
68            h.link_to('Spin off a subitem', h.url_for(controller='spend',
69                                                      action='subitem',
70                                                      id=e.id))))
71         
72         return h.redirect_to('/')
73     
74     @dispatch_on(POST='_post_split',
75                  GET='_get_split')
76     def split(self, id):
77         abort(500)
78     
79     def _get_split(self, id):
80         c.title = 'Change Expenditure Split'
81         
82         c.expenditure = meta.Session.query(model.Expenditure).get(id)
83         c.users = meta.Session.query(model.User)
84         
85         return render('/spend/split.mako')
86     
87     def _post_split(self, id):
88         c.values = request.params
89         c.errors = dict()
90         
91         split_dict = dict()
92         
93         for username, percent in c.values.iteritems():
94             try:
95                 user = meta.Session.query(model.User).\
96                     filter(model.User.username==username).one()
97                 split_dict[user] = Decimal(percent)
98             except InvalidOperation:
99                 c.errors[username] = 'Please enter a number'
100         if c.errors != dict():
101             return self._get_split(id)
102         
103         e = meta.Session.query(model.Expenditure).get(id)
104         e.split(split_dict)
105         
106         meta.Session.commit()
107         
108         h.flash('Expenditure redivided')
109         
110         return h.redirect_to('/')