summaryrefslogtreecommitdiff
path: root/app/views/consts.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/views/consts.py')
-rw-r--r--app/views/consts.py64
1 files changed, 43 insertions, 21 deletions
diff --git a/app/views/consts.py b/app/views/consts.py
index c76cf17..dfc61a2 100644
--- a/app/views/consts.py
+++ b/app/views/consts.py
@@ -4,7 +4,7 @@ from . import Blueprint, flash, db, \
assert_authorisation, templated, redirect, request, \
today
-from ..model import Category, ConstExpense
+from ..model import Category, ConstExpense, ConstExpenseGroup
from .. import forms as F
from sqlalchemy import sql
@@ -21,6 +21,19 @@ def one_year(d):
else:
return d.replace(month = d.month - 1, year = d.year + 1)
+def check_group(exp):
+ if exp.group is None:
+ group = ConstExpenseGroup(
+ description = exp.description,
+ start = exp.start,
+ end = exp.end,
+ category = exp.category,
+ user = current_user)
+
+ exp.description = None
+ exp.group = group
+ db.session.add(group)
+
#
# Form
#
@@ -46,7 +59,7 @@ class ConstForm(F.Form):
category = F.QuerySelectField('Kategorie',
get_label='name')
- prev = F.QuerySelectField('Vorgänger',
+ group = F.QuerySelectField('Gruppe',
get_label='description',
allow_blank=True)
@@ -54,17 +67,11 @@ class ConstForm(F.Form):
obj = cur if obj is None else obj
super().__init__(obj=obj)
self.category.query = Category.of(current_user).order_by(Category.name)
+ self.group.query = ConstExpenseGroup.of(current_user).order_by(ConstExpenseGroup.description)
- # init prev_list
- CE = ConstExpense
-
- filter = (CE.next == None)
- if cur and cur.id is not None: # not empty
- filter = sql.or_(CE.next == cur, filter)
- filter = sql.and_(filter, CE.id != cur.id)
-
- self.prev.query = CE.of(current_user).filter(filter).order_by(CE.description)
+ def get_category_data(self):
+ return dict((c.id, c.category_id) for c in ConstExpenseGroup.of(current_user))
#
# Views
@@ -76,25 +83,25 @@ def list ():
"""List all constant expenses."""
d = today()
- expenses = ConstExpense.of(current_user).order_by(ConstExpense.description).all()
+ groups = ConstExpenseGroup.of(current_user).order_by(ConstExpenseGroup.description).all()
current = []
old = []
future = []
last_month = []
- for e in expenses:
- if e.start <= d:
- if e.end >= d:
- current.append(e)
+ for g in groups:
+ if g.start <= d:
+ if g.end >= d:
+ current.append(g)
else:
- if (d.month == 1 and e.end.month == 12 and e.end.year == d.year - 1) \
- or (e.end.year == d.year and e.end.month == d.month - 1):
- last_month.append(e)
+ if (d.month == 1 and g.end.month == 12 and g.end.year == d.year - 1) \
+ or (g.end.year == d.year and g.end.month == d.month - 1):
+ last_month.append(g)
else:
- old.append(e)
+ old.append(g)
else:
- future.append(e)
+ future.append(g)
return { 'current': current, 'old': old, 'future': future, 'last_month': last_month }
@@ -119,12 +126,25 @@ def edit(id):
if form.is_submitted():
if 'deleteB' in request.form:
+ group = exp.group
db.session.delete(exp)
+
+ if not group.expenses:
+ db.session.delete(group)
+
db.session.commit()
return redirect('.list')
elif form.flash_validate(): # change
+ old_group = exp.group
form.populate_obj(exp)
+
+ if exp.group != old_group:
+ if not old_group.expenses:
+ db.session.delete(old_group)
+
+ check_group(exp)
+
db.session.commit()
flash("Eintrag geändert.")
return redirect('.show', id = id)
@@ -144,6 +164,8 @@ def add():
if form.validate_on_submit():
form.populate_obj(exp)
exp.user = current_user
+ check_group(exp)
+
db.session.add(exp)
db.session.commit()
flash("Eintrag hinzugefügt.")