X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Fcontrollers%2Fstatus.py;h=df0522e4a359ed8951a67dc4857afdad665767f1;hb=378790bb6dfd727bb25bb27dde22c484fc957267;hp=cba63e394a09e039ea948659f4550f3b8ebc2670;hpb=46b297349fdb6313d34a4128caf6e5b64b57bfa8;p=bluechips.git diff --git a/bluechips/controllers/status.py b/bluechips/controllers/status.py index cba63e3..df0522e 100644 --- a/bluechips/controllers/status.py +++ b/bluechips/controllers/status.py @@ -8,9 +8,11 @@ from bluechips.lib.base import * from bluechips.lib.totals import * import sqlalchemy +from sqlalchemy import orm from datetime import date, timedelta -from decimal import Decimal + +from bluechips.model.types import Currency from pylons import request @@ -20,34 +22,72 @@ class StatusController(BaseController): def index(self): c.debts = debts() c.settle = settle(c.debts) + + c.net = 0 + for from_user, to_user, amount in c.settle: + if from_user == request.environ['user']: + c.net -= amount + elif to_user == request.environ['user']: + c.net += amount - c.total = self._total(True) - - 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]] + periods = {} + periods['Total'] = (None, None) + periods['Past year'] = (date.today() - timedelta(days=365), None) + periods['Year to date'] = (date.today().replace(month=1, day=1), None) + periods['Month to date'] = (date.today().replace(day=1), None) + periods['Last month'] = ((date.today() - + timedelta(days=30)).replace(day=1), + periods['Month to date'][0]) + c.totals = {} + for period in periods.keys(): + c.totals[period] = {} + start, end = periods[period] + conds = [] + if start is not None: + conds.append(model.Expenditure.date >= start) + if end is not None: + conds.append(model.Expenditure.date < end) + if len(conds) > 1: + conds = sqlalchemy.and_(*conds) + elif len(conds) > 0: + conds = conds[0] + else: + conds = None + + for scope in ('all', 'mine'): + meth = getattr(self, '_total_%s' % scope) + c.totals[period][scope] = meth(conds) - 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']).all() + filter(sqlalchemy.or_( + model.Expenditure.spender == request.environ['user'], + model.Expenditure.splits.any( + sqlalchemy.and_( + model.Split.user == request.environ['user'], + model.Split.share != 0)))).\ + options(orm.eagerload('splits')).\ + 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'])).all() + model.Transfer.creditor==request.environ['user'])).\ + limit(10).all() + c.users = meta.Session.query(model.User.id, model.User) 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_all(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() + + def _total_mine(self, conditions=None): + q = meta.Session.query(sqlalchemy.func.SUM( + model.Split.share)).join(model.Split.expenditure).\ + filter(model.Split.user == request.environ['user']) + if conditions is not None: + q = q.filter(conditions) + return q.scalar()