diff options
Diffstat (limited to 'app/views/consts.py')
-rw-r--r-- | app/views/consts.py | 59 |
1 files changed, 42 insertions, 17 deletions
diff --git a/app/views/consts.py b/app/views/consts.py index df18188..97afad1 100644 --- a/app/views/consts.py +++ b/app/views/consts.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- from . import Blueprint, flash, db, \ current_user, login_required, \ - assert_authorisation, templated, redirect, request + assert_authorisation, templated, redirect, request, \ + today from ..model import Category, ConstExpense -from ..forms import ConstForm, today +from .. import forms as F import datetime from sqlalchemy import sql @@ -14,23 +15,47 @@ assert_authorisation = partial(assert_authorisation, ConstExpense.get) mod = Blueprint('consts', __name__) -def const_form(cur=None, obj=None): - obj = cur if obj is None else obj - form = ConstForm(obj=obj) - form.category.query = Category.of(current_user).order_by(Category.name) +class ConstForm(F.Form): + start = F.DateField(u'Beginn', F.req, + format='%m.%Y', + default=lambda: today()) - # init prev_list - CE = ConstExpense + end = F.DateField(u'Ende', F.req, + format='%m.%Y', + default=lambda: today().replace(year = today().year + 1), + description=u'(einschließlich)') - filter = (CE.next == None) + months = F.IntegerField(u'Zahlungsrythmus', F.req, + description='Monate') - 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) + expense = F.DecimalField(u'Betrag', F.req, + description=u'EUR', + places=2) - form.prev.query = CE.of(current_user).filter(filter).order_by(CE.description) + description = F.StringField(u'Beschreibung', F.req) - return form + category = F.QuerySelectField(u'Kategorie', + get_label='name') + + prev = F.QuerySelectField(u'Vorgänger', + get_label='description', + allow_blank=True) + + def __init__(self, cur=None, obj=None): + obj = cur if obj is None else obj + super(F.Form, self).__init__(obj=obj) + self.category.query = Category.of(current_user).order_by(Category.name) + + # 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) @mod.route('/') @login_required @@ -68,7 +93,7 @@ def show(id): @templated def edit(id): exp = ConstExpense.get(id) - form = const_form(exp) + form = ConstForm(exp) if form.is_submitted(): if 'deleteB' in request.form: @@ -94,7 +119,7 @@ def add_from(other): other = ConstExpense.get(other) # get form with data from other - form = const_form(obj = other) + form = ConstForm(obj = other) # replace some fields to be more meaningful start = max(form.end.data, today()) @@ -110,7 +135,7 @@ def add_from(other): def add (): exp = ConstExpense() - form = const_form() + form = ConstForm() if form.validate_on_submit(): form.populate_obj(exp) |