1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
from __future__ import with_statement
import web
from web import form
from model import *
from helper import appdir
from renderer import render
import datetime, decimal
import operator as op
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):
year = int(year)
month = int(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:
dformat = "%d.%m.%Y"
form = form.Form(
form.Textbox(
"date",
form.notnull,
value = datetime.date.today().strftime(dformat)
),
form.Textbox(
"expense",
form.notnull,
),
form.Textbox(
"description",
form.notnull
),
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, Add.dformat)),
("Not a valid decimal", lambda f: decimal.Decimal(f.expense)),
))
)
def GET(self):
return render("add", form = self.form())
def POST(self):
f = self.form()
if f.validates():
e = SingleExpense(category = Category.get_by(name = f.category.value))
e.date = datetime.datetime.strptime(f.date.value, self.dformat)
e.expense = decimal.Decimal(f.expense.value)
e.description = f.description.value
raise web.seeother(web.url("/"))
else:
return render("add", form = f)
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))
|