from __future__ import with_statement import web from model import * from helper import appdir from renderer import render import datetime from sqlalchemy import sql class Show: def GET(self, year = '', month = ''): if year: return self.render([self.calc(year, month)]) else: d = datetime.date.today() first = self.calc(d.year, d.month) if d.month == 1: second = self.calc(d.year - 1, 12) else: second = self.calc(d.year, d.month - 1) return self.render([first, second]) def calc(self, year, month): ssum = sql.functions.sum(SingleExpense.expense) csum = sql.functions.sum(ConstExpense.monthly) query = SingleExpense.of_month(month, year).\ group_by(SingleExpense.category_id).\ values(SingleExpense.category_id, ssum) exps = [CatExpense(Category.query.get(c), s) for c,s in query] consts = ConstExpense.of_month(month, year).value(csum) if consts is None: consts = 0 return MonthExpense(datetime.date(int(year), int(month), 1), consts, exps) def render(self, exps): return render("show", exps = exps) class Add: def GET(self): return "Add new" class Edit: def GET(self, id): return "Edit " + id class Const: def GET(self): return "Const" class ConstAdd: def GET(self): return "Add new const" class ConstEdit: def GET(self, id): return "Const Edit " + id class Cat: def GET(self, id = '/'): if id: id = id[1:] if not id: return "Add new cat" else: return "Edit cat " + id class FourOhFour: """ 404 error page. """ def GET (self, p): raise self.catch(p) @staticmethod def catch (page = "?"): return web.notfound(render("404", page = page))