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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
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 operator as op
from sqlalchemy import sql
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 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.date = datetime.datetime.strptime(f.date.value, self.dformat)
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 SingleExpense()
def form(self):
return form.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 render(self.template, form = f)
def POST(self, id):
exp = SingleExpense.get(id)
self.get_expense = lambda *x: exp
return Add.POST(self)
class Const:
def GET(self):
return "Const"
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
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 (ConstAdd):
template = "constedit"
def GET(self, 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):
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))
|