diff options
Diffstat (limited to '')
-rw-r--r-- | controller.py | 95 |
1 files changed, 87 insertions, 8 deletions
diff --git a/controller.py b/controller.py index e194d22..6db7b31 100644 --- a/controller.py +++ b/controller.py @@ -2,6 +2,7 @@ from __future__ import with_statement import web from web import form +from web.form import Validator from model import * from helper import appdir @@ -79,11 +80,13 @@ class Add: 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", @@ -92,11 +95,7 @@ class Add: form.Dropdown( "category", map(op.itemgetter(0), sorted(Category.query.values(Category.name))) - ), - validators = map(lambda x: form.Validator(*x), ( - ("Date Format does not match", lambda f: datetime.datetime.strptime(f.date, self.dformat)), - ("Not a valid decimal", lambda f: decimal.Decimal(f.expense)), - )) + ) ) class Edit (Add): @@ -123,12 +122,92 @@ class Const: return "Const" class ConstAdd: + template = "constadd" + dformat = "%m.%Y" + def GET(self): - return "Add new const" + 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 + + raise web.seeother("/") + else: + return render(self.template, form = f) + + def get_expense(self): + return ConstExpense() + + def form(self): + return form.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))) + ) + ) -class ConstEdit: +class ConstEdit (ConstAdd): + template = "constedit" + def GET(self, id): - return "Const Edit " + id + 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 + } + f = self.form() + f.fill(fvalues) + + return render(self.template, form = f) + + def POST(self, id): + exp = ConstExpense.get(id) + self.get_expense = lambda *x: exp + return ConstAdd.POST(self) class Cat: def GET(self, id = '/'): |