]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/spend.py
Fix indentation on base template
[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
12 log = logging.getLogger(__name__)
13
14 class SpendController(BaseController):
15     def index(self):
16         c.title = 'Add a New Expenditure'
17         
18         c.expenditure = dict()
19         c.expenditure['spender'] = request.environ['user']
20         
21         return render('/spend/index.mako')
22     
23     def edit(self, id):
24         c.title = 'Edit an Expenditure'
25         
26         c.expenditure = meta.Session.query(model.Expenditure).get(id)
27         
28         return render('/spend/index.mako')
29     
30     def update(self, id=None):
31         # Validate the submission
32         if not valid(self, spend.new_spend_form):
33             if id is None:
34                 return self.index()
35             else:
36                 return self.edit(id)
37         
38         # Either create a new object, or, if we're editing, get the
39         # old one
40         if id is None:
41             e = model.Expenditure()
42         else:
43             e = meta.Session.query(model.Expenditure).get(id)
44         
45         # Set the fields that were submitted
46         update_sar(e, self.form_result)
47         meta.Session.save_or_update(e)
48         
49         if id is None:
50             e.even_split()
51         else:
52             e.update_split()
53         
54         meta.Session.commit()
55         
56         h.flash('Expenditure recorded.')
57         h.flash("""Want to do something unusual?
58
59 <ul id="expenditure_options">
60   <li>%s</li>
61   <li>%s</li>
62 </ul>""" % (h.link_to('Change the split', h.url_for(controller='spend',
63                                                    action='split',
64                                                    id=e.id)),
65            h.link_to('Spin off a subitem', h.url_for(controller='spend',
66                                                      action='subitem',
67                                                      id=e.id))))
68         
69         return h.redirect_to('/')