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
|
from . import Blueprint, flash, db, \
current_user, login_required, \
assert_authorisation, templated, redirect, request, \
today
from .. import forms as F
from ..model import ConstExpense, SingleExpense
import sqlalchemy as sql
import calendar
from collections import defaultdict
from datetime import date
from flask import jsonify
mod = Blueprint('stats', __name__)
def next_date(d):
if d.month == 12:
return d.replace(year = d.year + 1, month = 1)
else:
return d.replace(month = d.month + 1)
def date_to_ms(d):
return calendar.timegm(d.timetuple()) * 1000
@mod.route('/_const/<int(fixed_digits=4):year>/<int(fixed_digits=2):month>')
@login_required
@templated
def const_dialog(year,month):
consts = ConstExpense.of_month(current_user, month, year).order_by(ConstExpense.description)
return { 'consts': consts }
@mod.route('/')
@login_required
@templated
def show():
# easy way: fetch them all and then do some computation
consts = defaultdict(int)
t = today().replace(day = 1)
for e in ConstExpense.of(current_user):
cur = e.start
end = min(e.end, t)
while cur <= end:
consts[date_to_ms(cur)] += e.monthly
cur = next_date(cur)
consts = list(sorted(consts.items()))
expQuery = SingleExpense.of(current_user)\
.group_by(SingleExpense.year, SingleExpense.month)\
.values(SingleExpense.year, SingleExpense.month, sql.func.sum(SingleExpense.expense))
expenses = list(sorted((date_to_ms(date(year,month,1)), exp) for (year, month, exp) in expQuery))
return { 'consts': consts, 'expenses' : expenses }
|