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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
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
# get the list of 'previous' expenses
filter = (CE.next == None)
cur = self.get_expense()
if cur.id is None: # empty
session.expunge(cur)
else:
filter = sql.or_(CE.next == cur, filter)
filter = sql.and_(filter, CE.id != cur.id)
prev_list = CE.query.filter(filter).order_by(CE.description).values(CE.id, CE.description)
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, '')], prev_list)
)
)
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))
|