summaryrefslogtreecommitdiff
path: root/kosten/app/views/stats.py
diff options
context:
space:
mode:
Diffstat (limited to 'kosten/app/views/stats.py')
-rw-r--r--kosten/app/views/stats.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/kosten/app/views/stats.py b/kosten/app/views/stats.py
new file mode 100644
index 0000000..9ff81a1
--- /dev/null
+++ b/kosten/app/views/stats.py
@@ -0,0 +1,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 }