from __future__ import with_statement import web from web import form from web.form import Validator from model import * from helper import appdir from renderer import render import datetime, decimal import itertools as iter import operator as op from sqlalchemy import sql class Form (form.Form): """Make sure we return utf-8 stuff.""" def render_utf8 (self, *args): return form.Form.render(self, *args).decode("utf-8") class Show: def GET(self, year = '', month = ''): if year: c = self.calc(year, month) return self.render([c], self.is_last(c)) 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], self.is_last(first)) def calc(self, year, month): year = int(year) month = int(month) ssum = sql.functions.sum(SingleExpense.expense) query = SingleExpense.of_month(month, year) result = query.group_by(SingleExpense.category_id).\ values(SingleExpense.category_id, ssum) exps = [CatExpense(Category.query.get(c), s, query.filter(SingleExpense.category_id == c)) for c,s in result] return MonthExpense(datetime.date(int(year), int(month), 1), exps) def is_last(self, exp): return exp.date >= datetime.date.today().replace(day = 1) def render(self, exps, is_last): return render("show", exps = exps, is_last = is_last) class Add: dformat = "%d.%m.%Y" template = "add" def GET(self): return self.render(self.form()) def POST(self): f = self.form() if f.validates(): category = Category.get_by(name = f.category.value) e = self.get_expense() e.category = category e.date = datetime.datetime.strptime(f.date.value, self.dformat) e.expense = decimal.Decimal(f.expense.value) e.description = f.description.value self.return_() else: return self.render(f) def get_expense(self): return SingleExpense() def render(self, f): return render(self.template, form = f) def return_(self): raise web.seeother("/add") def form(self): return Form( form.Textbox( "date", form.notnull, Validator("Date Format does not match", lambda v: datetime.datetime.strptime(v, self.dformat)), value = datetime.date.today().strftime(self.dformat) ), form.Textbox( "expense", form.notnull, Validator("Not a valid decimal", decimal.Decimal), ), form.Textbox( "description", form.notnull ), form.Dropdown( "category", map(op.itemgetter(0), sorted(Category.query.values(Category.name))) ) ) class Edit (Add): template = "edit" def GET(self, id): exp = SingleExpense.get(id) self.get_expense = lambda *x: exp fvalues = { "date" : exp.date.strftime(self.dformat), "expense" : str(exp.expense), "description" : exp.description, "category" : exp.category.name } f = self.form() f.fill(fvalues) return self.render(f) def POST(self, id): exp = SingleExpense.get(id) self.get_expense = lambda *x: exp return Add.POST(self) def return_(self): raise web.seeother("/") class Const: dformat = "%m.%Y" def GET(self, id=None): if id is None: d = datetime.date.today() current = ConstExpense.of_month(d.month, d.year) csub = current.subquery() # if only mysql would allow "EXCEPT" -.- old = ConstExpense.query.outerjoin((csub, csub.c.id == ConstExpense.id)).filter(csub.c.id == None) old = old.order_by(ConstExpense.start) return render("constlist", current = current.order_by(ConstExpense.start).all(), old = old.all()) else: exp = ConstExpense.get(id) return render("const", exp = exp, dformat = lambda x: x.strftime(self.dformat)) class ConstAdd: template = "constadd" dformat = "%m.%Y" def GET(self): return render(self.template, form = self.form()) def POST(self): f = self.form() if f.validates(): category = Category.get_by(name = f.category.value) e = self.get_expense() e.category = category e.start = datetime.datetime.strptime(f.start.value, self.dformat) e.end = datetime.datetime.strptime(f.end.value, self.dformat) e.months = f.months.value e.expense = decimal.Decimal(f.expense.value) e.description = f.description.value if int(f.prev.value) != -1: e.prev = ConstExpense.get(f.prev.value) raise web.seeother("/const/%s" % e.id) else: return render(self.template, form = f) def get_expense(self): return ConstExpense() def form(self): CE = ConstExpense return Form( form.Textbox( "start", form.notnull, Validator("Date Format does not match", lambda v: datetime.datetime.strptime(v, self.dformat)), value = datetime.date.today().strftime(self.dformat) ), form.Textbox( "end", form.notnull, Validator("Date Format does not match", lambda v: datetime.datetime.strptime(v, self.dformat)), value = datetime.date.today().strftime(self.dformat) ), form.Textbox( "months", form.notnull, Validator("Not an integer > 0", lambda v: int(v) > 0), value = "12", description = "Number of Months" ), form.Textbox( "expense", form.notnull, Validator("Not a valid decimal", decimal.Decimal), ), form.Textbox( "description", form.notnull ), form.Dropdown( "category", map(op.itemgetter(0), sorted(Category.query.values(Category.name))) ), form.Dropdown( "prev", iter.chain([(-1, '')], ConstExpense.query.order_by(CE.description).values(CE.id, CE.description)) ) ) class ConstEdit (ConstAdd): template = "constedit" def GET(self, id, fromId = None): if id == "from": fromC = ConstExpense.get(fromId) exp = ConstExpense() exp.from_dict(fromC.to_dict(exclude=["id", "prev"])) exp.prev = fromC session.commit() else: exp = ConstExpense.get(id) self.get_expense = lambda *x: exp fvalues = { "start" : exp.start.strftime(self.dformat), "end" : exp.end.strftime(self.dformat), "months" : str(exp.months), "expense" : str(exp.expense), "description" : exp.description, "category" : exp.category.name, "prev" : exp.prev.id if exp.prev else str(-1) } f = self.form() f.fill(fvalues) return render(self.template, form = f) def POST(self, id, fromId = None): if id == "from": id = fromId exp = ConstExpense.get(id) self.get_expense = lambda *x: exp return ConstAdd.POST(self) class Cat: def GET(self): categories = Category.query.order_by(Category.name).all() return render("cats", cats = categories) def POST(self): for id, name in web.input().iteritems(): if id.startswith("n-"): Category(name = name) else: Category.get(id).name = name raise web.seeother("/") class FourOhFour: """ 404 error page. """ def GET (self, p): raise self.catch(p) @staticmethod def catch (page = "?"): return web.notfound(render("404", level = "", page = page))