X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;ds=sidebyside;f=bluechips%2Fcontrollers%2Fstatus.py;h=37e470cfab4ac3c1171f998ffada87383906290e;hb=ac41cefa3569ceca9ebee894d2c333360b4158fc;hp=faadfc5085b318fbaf2ed65dc74563eff14d9888;hpb=512db6643a148932d6f734972f23ddf55a2e91ac;p=bluechips.git diff --git a/bluechips/controllers/status.py b/bluechips/controllers/status.py index faadfc5..37e470c 100644 --- a/bluechips/controllers/status.py +++ b/bluechips/controllers/status.py @@ -10,7 +10,8 @@ from bluechips.lib.totals import * import sqlalchemy from datetime import date, timedelta -from decimal import Decimal + +from bluechips.model.types import Currency from pylons import request @@ -21,21 +22,21 @@ class StatusController(BaseController): c.debts = debts() c.settle = settle(c.debts) - c.total = self._total(True) + c.total = self._total() 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) + c.year_total, c.this_year_total, c.this_month_total = \ + [self._total(model.Expenditure.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)) + model.Expenditure.date >= last_month, + model.Expenditure.date < this_month)) c.expenditures = meta.Session.query(model.Expenditure).\ filter(model.Expenditure.spender==request.environ['user']).\ @@ -48,8 +49,9 @@ class StatusController(BaseController): return render('/status/index.mako') - def _total(self, where): - return (meta.Session.execute(sqlalchemy.sql.select([ - sqlalchemy.func.sum(model.expenditures.c.amount).\ - label('total')]).\ - where(where)).scalar() or Decimal("0.00")) / 100 + def _total(self, conditions=None): + q = meta.Session.query(sqlalchemy.func.SUM( + model.Expenditure.amount)) + if conditions is not None: + q = q.filter(conditions) + return q.scalar()