]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/status.py
Make the distribution test actually do something
[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(sqlalchemy.text('1=1'))
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.expenditures.c.date >= i)
34              for i in [year, this_year, this_month]]
35         
36
37         c.last_month_total = self._total(sqlalchemy.and_(
38                     model.expenditures.c.date >= last_month,
39                     model.expenditures.c.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, where):
53         return Currency(meta.Session.execute(sqlalchemy.sql.select([
54                 sqlalchemy.func.sum(model.expenditures.c.amount).\
55                     label('total')]).\
56                     where(where)).scalar() or 0)