diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2013-11-01 14:19:07 +0100 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2013-11-01 14:19:07 +0100 |
commit | b36ec721ab97b80b1d070426c6f3f09b62ac5c7e (patch) | |
tree | ebc4016760e062d2a27072934b32980e2debf366 /app | |
parent | 6e6e436d5c76399b567a1e9e5ae16d77d0052f4f (diff) | |
download | kosten-b36ec721ab97b80b1d070426c6f3f09b62ac5c7e.tar.gz kosten-b36ec721ab97b80b1d070426c6f3f09b62ac5c7e.tar.bz2 kosten-b36ec721ab97b80b1d070426c6f3f09b62ac5c7e.zip |
Some reorganization
Diffstat (limited to 'app')
-rw-r--r-- | app/forms.py | 82 | ||||
-rw-r--r-- | app/utils.py | 3 | ||||
-rw-r--r-- | app/views/__init__.py | 4 | ||||
-rw-r--r-- | app/views/consts.py | 59 | ||||
-rw-r--r-- | app/views/expenses.py | 35 | ||||
-rw-r--r-- | app/views/user.py | 37 |
6 files changed, 108 insertions, 112 deletions
diff --git a/app/forms.py b/app/forms.py index 6c5782a..b4d7427 100644 --- a/app/forms.py +++ b/app/forms.py @@ -2,18 +2,13 @@ import flask from flask.ext.wtf import Form as _Form from wtforms.fields import DateField, IntegerField, StringField, HiddenField, PasswordField -from wtforms import validators as v +from wtforms import validators from wtforms import fields, ValidationError from wtforms.ext.sqlalchemy.fields import QuerySelectField from wtforms.ext.i18n.form import Form as i18nForm -import datetime - from . import app -from . import login - -today = datetime.date.today @app.template_test('hidden') def is_hidden_field(f): @@ -25,7 +20,7 @@ class DecimalField(fields.DecimalField): value = valuelist[0].replace(',','.') super(DecimalField, self).process_formdata([value]) -req = [v.input_required()] +req = [validators.input_required()] class Form(_Form, i18nForm): # docs say LOCALES, code says LANGUAGES ... use both :) @@ -53,76 +48,3 @@ class Form(_Form, i18nForm): # stuff added by flask-wtf # FIXME: remove this, if flask-babel is used in the app return i18nForm._get_translations(self) - -class ExpenseForm(Form): - date = DateField(u'Datum', req, - format="%d.%m.%Y", - default=lambda: today()) - - expense = DecimalField(u'Betrag', req, - description=u'EUR', - places=2) - - description = StringField(u'Beschreibung', req) - - category = QuerySelectField(u'Kategorie', - get_label='name') - -class ConstForm(Form): - start = DateField(u'Beginn', req, - format='%m.%Y', - default=lambda: today()) - - end = DateField(u'Ende', req, - format='%m.%Y', - default=lambda: today().replace(year = today().year + 1), - description=u'(einschließlich)') - - months = IntegerField(u'Zahlungsrythmus', req, - description='Monate') - - expense = DecimalField(u'Betrag', req, - description=u'EUR', - places=2) - - description = StringField(u'Beschreibung', req) - - category = QuerySelectField(u'Kategorie', - get_label='name') - - prev = QuerySelectField(u'Vorgänger', - get_label='description', - allow_blank=True) - -class LoginForm(Form): - username = StringField(u'Username', req) - pwd = PasswordField(u'Passwort', req) - - def __init__(self, *args, **kwargs): - Form.__init__(self,*args, **kwargs) - self.user = None - - def validate(self): - rv = Form.validate(self) - if not rv: - return False - - user = login.User.get_by(name = self.username.data) - if user is None or not user.check_password(self.pwd.data): - return False - - self.user = user - return True - -class ChangePwdForm(Form): - old = PasswordField(u'Passwort', req) - new = PasswordField(u'Neues Passwort', req + [v.EqualTo('confirm', u'Passwörter stimmen nicht überein')]) - confirm = PasswordField(u'Wdh. neues Passwort', req) - - def validate_old(self, field): - if not login.current_user.check_password(field.data): - raise ValidationError(u"Falsches Passwort") - - @property - def newpwd(self): - return self.new.data diff --git a/app/utils.py b/app/utils.py index 1d5f73c..2180282 100644 --- a/app/utils.py +++ b/app/utils.py @@ -4,6 +4,9 @@ from flask import redirect as _redirect from .login import current_user +import datetime +today = datetime.date.today + def _gen_tpl(endpoint): return endpoint.replace('.', '/') + '.jinja' diff --git a/app/views/__init__.py b/app/views/__init__.py index 7d344ad..7e0a6a4 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -7,14 +7,14 @@ from .. import app, db # Some general imports # from ..login import current_user, login_required -from ..utils import templated, redirect, assert_authorisation +from ..utils import today, templated, redirect, assert_authorisation from ..flask_extend import Blueprint from flask import flash __all__ = [ 'db', 'app', 'current_user', 'login_required', - 'assert_authorisation', 'templated', 'redirect', + 'assert_authorisation', 'templated', 'today', 'redirect', 'Blueprint', 'flash', 'request', 'url_for' ] 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) diff --git a/app/views/expenses.py b/app/views/expenses.py index 87e9c0f..870b45f 100644 --- a/app/views/expenses.py +++ b/app/views/expenses.py @@ -1,24 +1,37 @@ # -*- coding: utf-8 -*- from . import Blueprint, flash, db, \ current_user, login_required, \ - assert_authorisation, templated, redirect, request, url_for + assert_authorisation, templated, redirect, request, url_for, today from flask import Markup from ..model import Category, SingleExpense, CatExpense, MonthExpense -from ..forms import ExpenseForm +from .. import forms as F -import datetime, decimal +import datetime from sqlalchemy import sql, func from functools import partial assert_authorisation = partial(assert_authorisation, SingleExpense.get) mod = Blueprint('expenses', __name__) -def expense_form(obj=None): - form = ExpenseForm(obj=obj) - form.category.query = Category.of(current_user).order_by(Category.name) - return form +class ExpenseForm(F.Form): + date = F.DateField(u'Datum', F.req, + format="%d.%m.%Y", + default=lambda: today()) + + expense = F.DecimalField(u'Betrag', F.req, + description=u'EUR', + places=2) + + description = F.StringField(u'Beschreibung', F.req) + + category = F.QuerySelectField(u'Kategorie', + get_label='name') + + def __init__(self, obj = None): + super(F.Form, self).__init__(obj = obj) + self.category.query = Category.of(current_user).order_by(Category.name) def calc_month_exp(year, month): ssum = func.sum(SingleExpense.expense) @@ -65,7 +78,7 @@ def next_date(exp): @mod.app_template_test('last_date') def is_last(exp): - return exp.date >= datetime.date.today().replace(day = 1) + return exp.date >= today().replace(day = 1) @mod.route('/<int(fixed_digits=4):year>/<int(fixed_digits=2):month>') @login_required @@ -80,7 +93,7 @@ mod.add_url_rule('/<path:p>', endpoint = 'show_date_str', build_only = True) @login_required @templated def show(): - d = datetime.date.today() + d = today() first, pfirst = calc_month_and_pie(d.year, d.month) if d.month == 1: @@ -96,7 +109,7 @@ def show(): @templated def edit(id): exp = SingleExpense.get(id) - form = expense_form(exp) + form = ExpenseForm(exp) if form.is_submitted(): if 'deleteB' in request.form: @@ -118,7 +131,7 @@ def edit(id): @login_required @templated def add(): - form = expense_form() + form = ExpenseForm() if form.validate_on_submit(): exp = SingleExpense() diff --git a/app/views/user.py b/app/views/user.py index a532c45..7f6f998 100644 --- a/app/views/user.py +++ b/app/views/user.py @@ -3,13 +3,46 @@ from . import Blueprint, flash, db, \ current_user, login_required, \ templated, redirect, request, url_for -from ..forms import LoginForm, ChangePwdForm -from ..login import login_user, logout_user, login_manager +from .. import forms as F +from ..login import login_user, logout_user, login_manager, User import flask mod = Blueprint('user', __name__) +class LoginForm(F.Form): + username = F.StringField(u'Username', F.req) + pwd = F.PasswordField(u'Passwort', F.req) + + def __init__(self, *args, **kwargs): + super(F.Form, self).__init__(*args, **kwargs) + self.user = None + + def validate(self): + rv = super(F.Form, self).validate() + if not rv: + return False + + user = User.get_by(name = self.username.data) + if user is None or not user.check_password(self.pwd.data): + return False + + self.user = user + return True + +class ChangePwdForm(F.Form): + old = F.PasswordField(u'Passwort', F.req) + new = F.PasswordField(u'Neues Passwort', F.req + [F.validators.EqualTo('confirm', u'Passwörter stimmen nicht überein')]) + confirm = F.PasswordField(u'Wdh. neues Passwort', F.req) + + def validate_old(self, field): + if not current_user.check_password(field.data): + raise F.ValidationError(u"Falsches Passwort") + + @property + def newpwd(self): + return self.new.data + @mod.route('/login', methods=('GET', 'POST')) @templated def login(): |