From 21c591327c8c4c8bbf34831a23f4439fec32f3b2 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sun, 14 Apr 2013 18:54:50 +0200 Subject: Add expense -- in Flask --- app/forms.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ app/views/expenses.py | 23 +++++++++++++++--- 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 app/forms.py (limited to 'app') diff --git a/app/forms.py b/app/forms.py new file mode 100644 index 0000000..da5fc9e --- /dev/null +++ b/app/forms.py @@ -0,0 +1,67 @@ +# -*- encoding: utf-8 -*- + +from flask.ext.wtf import Form +from wtforms.fields import DateField, IntegerField, StringField, HiddenField +from wtforms import validators as v +from wtforms import fields + +from wtforms.ext.sqlalchemy.fields import QuerySelectField + +import datetime + +from . import app + +today = datetime.date.today + +def is_hidden_field(f): + return isinstance(f, HiddenField) + +app.jinja_env.tests["hidden"] = is_hidden_field + +class DecimalField(fields.DecimalField): + def process_formdata(self, valuelist): + if valuelist: + value = valuelist[0].replace(',','.') + super(DecimalField, self).process_formdata([value]) + +req = [v.input_required()] + +class ExpenseForm(Form): + date = DateField("Datum", req, + format="%d.%m.%Y", + default=today()) + + expense = DecimalField("Betrag", req, + description="EUR", + places=2) + + description = StringField("Beschreibung", req) + + category = QuerySelectField("Kategorie", + get_label="name") + +class ConstForm(Form): + start = DateField("Beginn", req, + format="%m.%Y", + default=today()) + + end = DateField("Ende", req, + format="%m.%Y", + default=today(), + description="(einschließlich)") + + months = IntegerField("Zahlungsrythmus", req, + description="Monate") + + expense = DecimalField("Betrag", req, + description="EUR", + places=2) + + desc = StringField("Beschreibung", req) + + cat = QuerySelectField("Kategorie", + get_label="name") + + prev = QuerySelectField("Vorgänger", + get_label="description", + allow_blank=True) diff --git a/app/views/expenses.py b/app/views/expenses.py index 6b976de..da01308 100644 --- a/app/views/expenses.py +++ b/app/views/expenses.py @@ -1,10 +1,12 @@ from flask import Blueprint -from flask import render_template, request, url_for +from flask import render_template, request, url_for, redirect import datetime, decimal from sqlalchemy import sql, func -from ..model import Category, SingleExpense, CatExpense, MonthExpense +from ..model import db, Category, SingleExpense, CatExpense, MonthExpense + +from ..forms import ExpenseForm mod = Blueprint('expenses', __name__) @@ -71,6 +73,19 @@ def show(year = None, month = None): def edit(id): return render_template("page.jinja") -@mod.route("/add/") +@mod.route("/add/", methods=("GET", "POST")) def add(): - return render_template("page.jinja") + form = ExpenseForm() + form.category.query = Category.query.order_by("name") + + if form.validate_on_submit(): + se = SingleExpense() + + form.populate_obj(se) + + db.session.add(se) + db.session.commit() + + return redirect(url_for("index")) + + return render_template("pages/add.jinja", form=form) -- cgit v1.2.3-70-g09d2