summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--app/forms.py6
-rw-r--r--app/views/consts.py97
-rw-r--r--templates/pages/const.jinja39
-rw-r--r--templates/pages/const.mako38
-rw-r--r--templates/pages/constadd.jinja12
-rw-r--r--templates/pages/constadd.mako16
-rw-r--r--templates/pages/constedit.jinja13
-rw-r--r--templates/pages/constedit.mako10
-rw-r--r--templates/pages/constlist.jinja23
-rw-r--r--templates/pages/constlist.mako32
10 files changed, 184 insertions, 102 deletions
diff --git a/app/forms.py b/app/forms.py
index a7b9fcd..1ab3c72 100644
--- a/app/forms.py
+++ b/app/forms.py
@@ -46,7 +46,7 @@ class ConstForm(Form):
end = DateField(u"Ende", req,
format="%m.%Y",
- default=today(),
+ default=today().replace(year = today().year + 1),
description=u"(einschließlich)")
months = IntegerField(u"Zahlungsrythmus", req,
@@ -56,9 +56,9 @@ class ConstForm(Form):
description=u"EUR",
places=2)
- desc = StringField(u"Beschreibung", req)
+ description = StringField(u"Beschreibung", req)
- cat = QuerySelectField(u"Kategorie",
+ category = QuerySelectField(u"Kategorie",
get_label="name")
prev = QuerySelectField(u"Vorgänger",
diff --git a/app/views/consts.py b/app/views/consts.py
index bda6067..f1e4ebd 100644
--- a/app/views/consts.py
+++ b/app/views/consts.py
@@ -1,12 +1,103 @@
from ..flask_extend import Blueprint
-from flask import render_template, request, url_for
+from flask import render_template, request, url_for, redirect
+
+import datetime
+from sqlalchemy import sql
+
+from ..model import db, Category, ConstExpense
+from ..forms import ConstForm, today
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.query.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)
+
+ form.prev.query = CE.query.filter(filter).order_by(CE.description)
+
+ return form
+
+
@mod.route("/")
def all ():
- return render_template("page.jinja")
+ d = today()
+
+ expenses = ConstExpense.query.order_by(ConstExpense.start).all()
+
+ current = []
+ old = []
+ future = []
+
+ for e in expenses:
+ if e.start <= d:
+ if e.end >= d:
+ current.append(e)
+ else:
+ old.append(e)
+ else:
+ future.append(e)
+
+ return render_template("pages/constlist.jinja", current = current, old = old, future = future)
@mod.route("/<int:id>")
def show(id):
- return render_template("page.jinja")
+ return render_template("pages/const.jinja", exp = ConstExpense.get(id))
+
+@mod.route("/edit/<int:id>", methods=("GET", "POST"))
+def edit(id):
+ exp = ConstExpense.get(id)
+ form = const_form(exp)
+
+ if request.method == "POST":
+ if "deleteB" in request.form:
+ db.session.delete(exp)
+ db.session.commit()
+ return redirect(url_for(".all"))
+
+ elif form.validate(): # change
+ form.populate_obj(exp)
+ db.session.commit()
+ return redirect(url_for(".show", id = exp.id))
+
+ return render_template("pages/constedit.jinja", form=form)
+
+@mod.route("/add/from/<int:other>")
+def add_from(other):
+ exp = ConstExpense() # needed to initialize 'CE.next'
+
+ other = ConstExpense.get(other)
+
+ # get form with data from other
+ form = const_form(obj = other)
+
+ # replace some fields to be more meaningful
+ start = max(form.end.data, today())
+ form.start.data = start
+ form.end.data = start.replace(year = start.year + 1)
+ if not other.next: form.prev.data = other
+
+ return add(form)
+
+@mod.route("/add/", methods=("GET", "POST"))
+def add (form=None):
+ exp = ConstExpense()
+
+ form = const_form() if form is None else form
+
+ if form.validate_on_submit():
+ form.populate_obj(exp)
+ db.session.add(exp)
+ db.session.commit()
+ return redirect(url_for(".show", id = exp.id))
+
+ return render_template("pages/constadd.jinja", form = form)
diff --git a/templates/pages/const.jinja b/templates/pages/const.jinja
new file mode 100644
index 0000000..13e8008
--- /dev/null
+++ b/templates/pages/const.jinja
@@ -0,0 +1,39 @@
+{% extends "page.jinja" %}
+
+{% set fmt="%m.%Y" %}
+
+{% block heading %}
+ Konstante Kosten
+{% endblock %}
+
+{% block content %}
+ <ul class="arrow">
+ <li><span class="heading">Beschreibung:</span> {{exp.description}}</li>
+ <li><span class="heading">Kategorie:</span> {{exp.category.name}}</li>
+ <li><span class="heading">Betrag:</span> {{exp.expense | eur}}</li>
+ <li><span class="heading">Betrag pro Monat:</span> {{exp.monthly | eur}}</li>
+ <li><span class="heading">Start:</span> {{exp.start | date(format=fmt)}}</li>
+ <li><span class="heading">Ende:</span> {{exp.end | date(format=fmt)}}</li>
+ <li><span class="heading">Zahlungsrhythmus:</span>
+ {% if exp.months == 1 %}
+ monatlich
+ {% elif exp.months == 6 %}
+ halbjährlich
+ {% elif exp.months == 12 %}
+ jährlich
+ {% else %}
+ alle {{exp.months}} Monate
+ {% endif %}
+ </ul>
+ <p>
+ <a href="{{ url_for(".edit", id = exp.id) }}">Bearbeiten</a>&nbsp;
+ <a href="{{ url_for(".add_from", other = exp.id) }}">Erstelle neuen auf dem jetzigen basierenden Eintrag</a>
+ </p>
+ {% if exp.prev %} {{ left_arrow(url_for(".show", id = exp.prev.id), target(exp.prev)) }} {% endif %}
+ {% if exp.next %} {{ right_arrow(url_for(".show", id = exp.next.id), target(exp.next)) }} {% endif %}
+{% endblock %}
+
+{% macro target(p) -%}
+ {{ p.description }} ({{ p.start | date(fmt) ~ "-" ~ p.end | date(fmt) }})<br>
+ {{ p.expense | eur }}
+{%- endmacro %}
diff --git a/templates/pages/const.mako b/templates/pages/const.mako
deleted file mode 100644
index 95098ea..0000000
--- a/templates/pages/const.mako
+++ /dev/null
@@ -1,38 +0,0 @@
-<%inherit file="/page.mako" />
-
-<ul class="arrow">
- <li><span class="heading">Beschreibung:</span> ${exp.description}</li>
- <li><span class="heading">Kategorie:</span> ${exp.category.name}</li>
- <li><span class="heading">Betrag:</span> ${exp.expense | eur}</li>
- <li><span class="heading">Betrag pro Monat:</span> ${exp.monthly | eur}</li>
- <li><span class="heading">Start:</span> ${dformat(exp.start)}</li>
- <li><span class="heading">Ende:</span> ${dformat(exp.end)}</li>
- <li><span class="heading">Zahlungsrhythmus:</span>
- % if exp.months == 1:
- monatlich
- % elif exp.months == 6:
- halbjährlich
- % elif exp.months == 12:
- jährlich
- % else:
- alle ${exp.months} Monate
- % endif
-</ul>
-<p><a href=${"/const/edit/%s" % exp.id | url}>Bearbeiten</a> <a href=${"/const/add/from/%s" % exp.id | url}>Erstelle neuen auf dem jetzigen basierenden Eintrag</a></p>
-
-<%
- def create(p):
- return ("const/%s" % p.id, "%s (%s-%s)<br>%s" % (p.description, dformat(p.start), dformat(p.end), eur(p.expense)))
-
- p = exp.prev
- if p:
- context.write(self.left_arrow(*create(p)))
-
- p = exp.next
- if p:
- context.write(self.right_arrow(*create(p)))
-%>
-
-<%block name="heading">
- Konstante Kosten
-</%block>
diff --git a/templates/pages/constadd.jinja b/templates/pages/constadd.jinja
new file mode 100644
index 0000000..8f878b4
--- /dev/null
+++ b/templates/pages/constadd.jinja
@@ -0,0 +1,12 @@
+{% extends "page.jinja" %}
+
+{% block heading %}
+ Füge neue konstante Ausgabe hinzu
+{% endblock %}
+
+{% block content %}
+ <form name="add_const_expense" method="post" action="{{ url_for(".add") }}">
+ {{ render_form(form) }}
+ <input type="submit" name="changeB">
+ </form>
+{% endblock %}
diff --git a/templates/pages/constadd.mako b/templates/pages/constadd.mako
deleted file mode 100644
index 2c16544..0000000
--- a/templates/pages/constadd.mako
+++ /dev/null
@@ -1,16 +0,0 @@
-<%inherit file="/page.mako" />
-
-% if not form.valid:
- FEHLER!
-% endif
-
-<form name="add_const_expense" method="post">
- ${form.render()}
- <%block name="form_buttons">
- <input type="submit" name="changeB"/>
- </%block>
-</form>
-
-<%block name="heading">
- Füge neue konstante Ausgabe hinzu
-</%block>
diff --git a/templates/pages/constedit.jinja b/templates/pages/constedit.jinja
new file mode 100644
index 0000000..c440ecb
--- /dev/null
+++ b/templates/pages/constedit.jinja
@@ -0,0 +1,13 @@
+{% extends "page.jinja" %}
+
+{% block heading %}
+ Konstante Ausgabe bearbeiten
+{% endblock %}
+
+{% block content %}
+ <form name="edit_const_expense" method="post">
+ {{ render_form(form) }}
+ <input type="submit" name="changeB">
+ <input type="submit" name="deleteB" value="Eintrag löschen" />
+ </form>
+{% endblock %}
diff --git a/templates/pages/constedit.mako b/templates/pages/constedit.mako
deleted file mode 100644
index 7507bf9..0000000
--- a/templates/pages/constedit.mako
+++ /dev/null
@@ -1,10 +0,0 @@
-<%inherit file="constadd.mako" />
-
-<%block name="heading">
- Konstante Ausgabe bearbeiten
-</%block>
-
-<%block name="form_buttons">
- <input type="submit" name="changeB" />
- <input type="submit" name="deleteB" value="Eintrag löschen" />
-</%block>
diff --git a/templates/pages/constlist.jinja b/templates/pages/constlist.jinja
new file mode 100644
index 0000000..ffe121f
--- /dev/null
+++ b/templates/pages/constlist.jinja
@@ -0,0 +1,23 @@
+{% extends "page.jinja" %}
+
+{% block heading %}
+ Konstante Kosten
+{% endblock %}
+
+{% block content %}
+ <p><a href="{{ url_for(".add") }}">Neuen Eintrag hinzufügen</a></p>
+
+ {{ list(current, "Aktuell") }}
+
+ {% if future %} {{ list(future, "Zukünftige") }} {% endif %}
+ {% if old %} {{ list(old, "Veraltet") }} {% endif %}
+{% endblock %}
+
+{% macro list(list, h) %}
+ <h2>{{ h }}</h2>
+ <ul class="arrow">
+ {% for c in list -%}
+ <li><a href="{{ url_for(".show", id = c.id) }}">{{c.description}} ({{c.expense | eur}})</a></li>
+ {% endfor %}
+ </ul>
+{% endmacro %}
diff --git a/templates/pages/constlist.mako b/templates/pages/constlist.mako
deleted file mode 100644
index f870b80..0000000
--- a/templates/pages/constlist.mako
+++ /dev/null
@@ -1,32 +0,0 @@
-<%inherit file="/page.mako" />
-
-<p><a href=${"/const/add" | url}>Neuen Eintrag hinzufügen</a></p>
-
-<h2>Aktuell</h2>
-<ul class="arrow">
- % for c in current:
- <li><a href=${"/const/%s" % c.id | url}>${c.description} (${c.expense | eur})</a></li>
- % endfor
-</ul>
-
-% if future:
- <h2>Zukünftige</h2>
- <ul class="arrow">
- % for c in future:
- <li><a href=${"/const/%s" % c.id | url}>${c.description} (${c.expense | eur})</a></li>
- % endfor
- </ul>
-% endif
-
-% if old:
- <h2>Veraltet</h2>
- <ul class="arrow">
- % for c in old:
- <li><a href=${"/const/%s" % c.id | url}>${c.description} (${c.expense | eur})</a></li>
- % endfor
- </ul>
-% endif
-
-<%block name="heading">
- Konstante Kosten
-</%block>