X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Fcontrollers%2Fstatus.py;h=9c0358f80056c95fbac0178dc7c4e40ddd7fd114;hb=74d234f880ee6b3c510227ea94459b81a79755db;hp=fa084481011f62fbb8d0289c5ce9a7904064ac9d;hpb=1d2e4f3e0f545547c37833445927569e5554fc2f;p=bluechips.git diff --git a/bluechips/controllers/status.py b/bluechips/controllers/status.py index fa08448..9c0358f 100644 --- a/bluechips/controllers/status.py +++ b/bluechips/controllers/status.py @@ -7,6 +7,14 @@ import logging from bluechips.lib.base import * from bluechips.lib.totals import * +import sqlalchemy + +from datetime import date, timedelta + +from bluechips.model.types import Currency + +from pylons import request + log = logging.getLogger(__name__) class StatusController(BaseController): @@ -14,4 +22,35 @@ class StatusController(BaseController): c.debts = debts() c.settle = settle(c.debts) + c.total = self._total(sqlalchemy.text('1=1')) + + year = date.today() - timedelta(days=365) + this_year = date.today().replace(month=1, day=1) + this_month = date.today().replace(day=1) + last_month = (date.today() - timedelta(days=30)).replace(day=1) + + c.year_total, c.this_year_total, c.this_month_total = \ + [self._total(model.expenditures.c.date >= i) + for i in [year, this_year, this_month]] + + + c.last_month_total = self._total(sqlalchemy.and_( + model.expenditures.c.date >= last_month, + model.expenditures.c.date < this_month)) + + c.expenditures = meta.Session.query(model.Expenditure).\ + filter(model.Expenditure.spender==request.environ['user']).\ + limit(10).all() + c.transfers = meta.Session.query(model.Transfer).\ + filter(sqlalchemy.or_( + model.Transfer.debtor==request.environ['user'], + model.Transfer.creditor==request.environ['user'])).\ + limit(10).all() + return render('/status/index.mako') + + def _total(self, where): + return Currency(meta.Session.execute(sqlalchemy.sql.select([ + sqlalchemy.func.sum(model.expenditures.c.amount).\ + label('total')]).\ + where(where)).scalar() or 0)