]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/status.py
fixed some issues resulting from upgrading sqlalchemy, made more ORM-y
[bluechips.git] / bluechips / controllers / status.py
1 """
2 Calculate the current state of the books
3 """
4
5 import logging
6
7 from bluechips.lib.base import *
8 from bluechips.lib.totals import *
9
10 import sqlalchemy
11
12 from datetime import date, timedelta
13
14 from bluechips.model.types import Currency
15
16 from pylons import request
17
18 log = logging.getLogger(__name__)
19
20 class StatusController(BaseController):
21     def index(self):
22         c.debts = debts()
23         c.settle = settle(c.debts)
24         
25         c.total = self._total()
26         
27         year = date.today() - timedelta(days=365)
28         this_year = date.today().replace(month=1, day=1)
29         this_month = date.today().replace(day=1)
30         last_month = (date.today() - timedelta(days=30)).replace(day=1)
31         
32         c.year_total, c.this_year_total, c.this_month_total = \
33             [self._total(model.Expenditure.date >= i)
34              for i in [year, this_year, this_month]]
35         
36
37         c.last_month_total = self._total(sqlalchemy.and_(
38                     model.Expenditure.date >= last_month,
39                     model.Expenditure.date < this_month))
40         
41         c.expenditures = meta.Session.query(model.Expenditure).\
42             filter(model.Expenditure.spender==request.environ['user']).\
43             limit(10).all()
44         c.transfers = meta.Session.query(model.Transfer).\
45             filter(sqlalchemy.or_(
46                 model.Transfer.debtor==request.environ['user'],
47                 model.Transfer.creditor==request.environ['user'])).\
48                 limit(10).all()
49         
50         return render('/status/index.mako')
51     
52     def _total(self, conditions=None):
53         q = meta.Session.query(sqlalchemy.func.SUM(
54             model.Expenditure.amount))
55         if conditions is not None:
56             q = q.filter(conditions)
57         return q.scalar()