summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--app/views/__init__.py (renamed from app/views.py)19
-rw-r--r--app/views/categories.py12
-rw-r--r--app/views/consts.py12
-rw-r--r--app/views/expenses.py76
-rw-r--r--templates/404.jinja3
-rw-r--r--templates/menu.jinja8
-rw-r--r--templates/page.jinja16
-rw-r--r--templates/pages/cats.jinja22
-rw-r--r--templates/pages/cats.mako22
-rw-r--r--templates/pages/show.jinja59
-rw-r--r--templates/pages/show.mako73
-rw-r--r--templates/root.jinja2
12 files changed, 206 insertions, 118 deletions
diff --git a/app/views.py b/app/views/__init__.py
index accbeb5..dc662ec 100644
--- a/app/views.py
+++ b/app/views/__init__.py
@@ -1,7 +1,7 @@
from flask import render_template, request, url_for
import flask
-from . import app, db
+from .. import app, db
# check for mobile visitors
mobile_checks = ["J2ME", "Opera Mini"]
@@ -12,21 +12,22 @@ def handle_mobile():
flask.g.is_mobile = any((x in ua) for x in mobile_checks)
-
@app.template_filter("static_url")
def static_url(s):
return url_for("static", filename=s)
+@app.template_filter("eur")
+def eur(s):
+ return ("%s EUR" % s)
@app.errorhandler(404)
def page_not_found (error):
return render_template("404.jinja", page = request.path), 404
-@app.route("/")
-@app.route("/index")
-def index():
- return render_template("root.jinja")
+from . import categories, consts, expenses
+
+app.register_blueprint(expenses.mod)
+app.register_blueprint(consts.mod, url_prefix="/const")
+app.register_blueprint(categories.mod, url_prefix="/cat")
-@app.route("/add")
-def addExp():
- return render_template("root.jinja")
+app.add_url_rule("/", endpoint = "index", build_only = True)
diff --git a/app/views/categories.py b/app/views/categories.py
new file mode 100644
index 0000000..c351746
--- /dev/null
+++ b/app/views/categories.py
@@ -0,0 +1,12 @@
+from flask import Blueprint
+from flask import render_template, request, url_for
+
+from ..model import Category
+
+mod = Blueprint('categories', __name__)
+
+@mod.route("/")
+def all ():
+ categories = Category.query.order_by(Category.name).all()
+
+ return render_template("pages/cats.jinja", cats = categories)
diff --git a/app/views/consts.py b/app/views/consts.py
new file mode 100644
index 0000000..6358ed1
--- /dev/null
+++ b/app/views/consts.py
@@ -0,0 +1,12 @@
+from flask import Blueprint
+from flask import render_template, request, url_for
+
+mod = Blueprint('consts', __name__)
+
+@mod.route("/")
+def all ():
+ return render_template("page.jinja")
+
+@mod.route("/<int:id>")
+def show(id):
+ return render_template("page.jinja")
diff --git a/app/views/expenses.py b/app/views/expenses.py
new file mode 100644
index 0000000..6b976de
--- /dev/null
+++ b/app/views/expenses.py
@@ -0,0 +1,76 @@
+from flask import Blueprint
+from flask import render_template, request, url_for
+
+import datetime, decimal
+from sqlalchemy import sql, func
+
+from ..model import Category, SingleExpense, CatExpense, MonthExpense
+
+mod = Blueprint('expenses', __name__)
+
+def is_last(exp):
+ return exp.date >= datetime.date.today().replace(day = 1)
+
+def calc_month_exp(year, month):
+ ssum = func.sum(SingleExpense.expense)
+ query = SingleExpense.of_month(month, year)
+
+ result = query.group_by(SingleExpense.category_id).\
+ values(SingleExpense.category_id, ssum)
+
+ exps = [CatExpense(Category.query.get(c), s, query.filter(SingleExpense.category_id == c)) for c,s in result]
+
+ return MonthExpense(datetime.date(year, month, 1), exps)
+
+def prev_date(exp):
+ if exp.date.month == 1:
+ return { "year": exp.date.year - 1, "month": 12 }
+ else:
+ return { "year": exp.date.year, "month": exp.date.month - 1 }
+
+def next_date(exps):
+ def _next_date(exp):
+ if exp.date.month == 13 - len(exps):
+ return { "year": exp.date.year + 1, "month": 1 }
+ else:
+ return { "year": exp.date.year, "month": exp.date.month + len(exps) }
+ return _next_date
+
+@mod.app_template_filter("date")
+def format_date(s):
+ if hasattr(s, "date"):
+ return "%s/%s" % (s.date.year, s.date.month)
+ else:
+ return "%(year)s/%(month)s" % s
+
+def render_show(exps, is_last):
+ return render_template("pages/show.jinja",
+ exps = exps, is_last = is_last,
+ prev_date = prev_date,
+ next_date = next_date(exps))
+
+
+@mod.route("/<int(fixed_digits=4):year>/<int(fixed_digits=2):month>")
+def show_date(year, month):
+ c = calc_month_exp(year, month)
+ return render_show([c], is_last(c))
+
+@mod.route("/")
+def show(year = None, month = None):
+ d = datetime.date.today()
+
+ first = calc_month_exp(d.year, d.month)
+ if d.month == 1:
+ second = calc_month_exp(d.year - 1, 12)
+ else:
+ second = calc_month_exp(d.year, d.month - 1)
+
+ return render_show([first, second], is_last(first))
+
+@mod.route("/edit/<int:id>")
+def edit(id):
+ return render_template("page.jinja")
+
+@mod.route("/add/")
+def add():
+ return render_template("page.jinja")
diff --git a/templates/404.jinja b/templates/404.jinja
index c802610..aeab58d 100644
--- a/templates/404.jinja
+++ b/templates/404.jinja
@@ -1,6 +1,7 @@
{% extends "page.jinja" %}
{% set title = "404 -- Seite konnte nicht gefunden werden!" %}
-{% set heading = "404 -- Seite nicht gefunden" %}
+
+{% block heading %}404 -- Seite nicht gefunden{% endblock %}
{% block content %}
<p>
diff --git a/templates/menu.jinja b/templates/menu.jinja
index b96d542..bf7045a 100644
--- a/templates/menu.jinja
+++ b/templates/menu.jinja
@@ -1,7 +1,7 @@
{% set menu = [
("index", "Kosten"),
- ("addExp", "Neu"),] %}
- {# ("/const", "Konstante Kosten"),
- ("/categories", "Kategorien")
+ ("expenses.add", "Neu"),
+ ("consts.all", "Konstante Kosten"),
+ ("categories.all", "Kategorien")
]
-#}
+%}
diff --git a/templates/page.jinja b/templates/page.jinja
index e1f8af7..671f3b2 100644
--- a/templates/page.jinja
+++ b/templates/page.jinja
@@ -1,27 +1,27 @@
{% extends "root.jinja" %}
{# functions #}
-{% macro left_arrow(target,label) -%}
- <a id="left" href="{{ url_for(target) }}">
+{% macro left_arrow(target,label) %}
+ <a id="left" href="{{target}}">
<img src="{{ "images/arrow_left.png" | static_url }}">
<span class="navdate">{{label}}</span>
</a>
-{%- endmacro %}
+{% endmacro %}
-{% macro right_arrow(target,label) -%}
- <a id="right" href="{{ url_for(target) }}">
+{% macro right_arrow(target,label) %}
+ <a id="right" href="{{target}}">
<img src="{{ "images/arrow_right.png" | static_url }}">
<span class="navdate">{{label}}</span>
</a>
-{%- endmacro %}
+{% endmacro %}
{% macro colorize(fgcolor=None, bgcolor=None, tag='span') -%}
{% if not fgcolor and not bgcolor %}
{{ caller() }}
{% else %}
{% set style = "" %}
- {% if fgcolor: %}{% set style = style + " color: " + fgcolor %}{% endif %}
- {% if bgcolor: %}{% set style = style + " background: " + bgcolor %}{% endif %}
+ {% if fgcolor: %}{% set style = style ~ " color: " ~ fgcolor %}{% endif %}
+ {% if bgcolor: %}{% set style = style ~ " background: " ~ bgcolor %}{% endif %}
<{{tag}} style="{{style}}">{{caller()}}</{{tag}}>
{% endif %}
{% endmacro %}
diff --git a/templates/pages/cats.jinja b/templates/pages/cats.jinja
new file mode 100644
index 0000000..1a3d88e
--- /dev/null
+++ b/templates/pages/cats.jinja
@@ -0,0 +1,22 @@
+{% extends "page.jinja" %}
+
+{% block heading %} Kategorien {% endblock %}
+
+{% block js %}
+ {{ super() }}
+ <script type="text/javascript" src="{{ "js/cats.js" | static_url }}"></script>
+{% endblock %}
+
+{% block content %}
+ <form name="categories" method="post">
+ <ul class="arrow">
+ {% for c in cats %}
+ <li><span>{{c.name}}</span><input name="{{c.id}}" type="text" value="{{c.name}}" style="display:none;"></li>
+ {%- endfor %}
+ <div><img id="add" src="{{ "images/add.png" | static_url }}"></div>
+ </ul>
+ <input type="submit">
+ </form>
+ <input id="new" name="n-" style="display:none;" >
+{% endblock %}
+
diff --git a/templates/pages/cats.mako b/templates/pages/cats.mako
deleted file mode 100644
index 0f71921..0000000
--- a/templates/pages/cats.mako
+++ /dev/null
@@ -1,22 +0,0 @@
-<%inherit file="/page.mako" />
-
-<form name="categories" method="post">
- <ul class="arrow">
- % for c in cats:
- <li><span>${c.name}</span><input name="${c.id}" type="text" value="${c.name}" style="display:none;"/></li>
- % endfor
- <div><img id="add" src=${"/static/images/add.png" | url} /></div>
- </ul>
-
- <input type="submit" />
-</form>
-
-<input id="new" name="n-" style="display:none;" />
-<%block name="heading">
- Kategorien
-</%block>
-
-<%block name="js">
- ${parent.js()}
- <script type="text/javascript" src=${"/static/js/cats.js" | url}></script>
-</%block>
diff --git a/templates/pages/show.jinja b/templates/pages/show.jinja
new file mode 100644
index 0000000..e7d340e
--- /dev/null
+++ b/templates/pages/show.jinja
@@ -0,0 +1,59 @@
+{% extends "page.jinja" %}
+
+{% block heading %}
+ {% if exps | length > 1 %}
+ Aktuelle Kosten
+ {% else %}
+ Kosten für {{exps[0]|date}}
+ {% endif %}
+{% endblock %}
+
+{% block js %}
+ {{ super() }}
+ <script type="text/javascript" src="{{ "js/show.js" | static_url }}"></script>
+{% endblock %}
+
+{% block content %}
+ {% for e in exps %}
+ {% if exps | length > 1 %}<h2>{{e|date}}</h2>{% endif %}
+ {% for c in e.catexps | sort(attribute="cat.name") %}
+ {% call(exp) detail(name=c.cat.name, sum=c.expense, set=c.all) %}
+ <a href="{{ url_for(".edit", id = exp.id) }}">{{exp.day}}.{{exp.month}}. -- {{exp.description}}: {{exp.expense | eur }}</a>
+ {% endcall %}
+ {% endfor %}
+
+ {% call(exp) detail(name="Constant", sum=e.constsum, set=e.consts) %}
+ <a href="{{ url_for("consts.show", id = exp.id) }}">{{exp.monthly}} -- {{exp.description}}</a>
+ {% endcall %}
+
+ {% call(exp) detail(name="In Summa", sum=e.sum, set=e.all, color="#ff2d2d") %}
+ <a href="{{ url_for(".edit", id = exp.id) }}">{{exp.day}}.{{exp.month}}. -- {{exp.description}}: {{exp.expense | eur}}</a>
+ {% endcall %}
+ <br>
+ {% endfor %}
+
+ {% set d = prev_date(exps | last) %}
+ {{ left_arrow(url_for(".show_date", **d), d | date) }}
+
+ {% if not is_last %}
+ {% set d = next_date(exps | last) %}
+ {{ right_arrow(url_for(".show_date", **d), d | date) }}
+ {% endif %}
+{% endblock content %}
+
+{% macro detail(name, sum, set, color=None) %}
+ <div class="detail">
+ <img class="mark" src="{{ "images/closed.png" | static_url }}">
+ {% call colorize(fgcolor=color) %}
+ <span class="heading">{{name}}:</span> <span class="sum">{{sum | eur}}</span><br>
+ {% endcall %}
+ <div class="details">
+ <ul>
+ {% for exp in set %}
+ <li class="expense">{{ caller(exp) }}</li>
+ {% endfor %}
+ </ul>
+ </div>
+ </div>
+{% endmacro %}
+
diff --git a/templates/pages/show.mako b/templates/pages/show.mako
deleted file mode 100644
index 06a9915..0000000
--- a/templates/pages/show.mako
+++ /dev/null
@@ -1,73 +0,0 @@
-<%inherit file="/page.mako" />
-
-% for e in exps:
- % if len(exps) > 1:
- <h2>${get_d(e)}</h2>
- % endif
- % for c in sorted(e.catexps, key = lambda c: c.cat.name):
- <%self:detail name="${c.cat.name}" sum="${c.expense}" set="${c.all}" args="exp">
- <a href=${"/edit/%s" % exp.id | url}>${exp.day}.${exp.month}. -- ${exp.description}: ${exp.expense | eur}</a>
- </%self:detail>
- % endfor
-
- <%self:detail name="Constant" sum="${e.constsum}" set="${e.consts}" args="exp">
- <a href=${"/const/%s" % exp.id | url}>${exp.monthly} -- ${exp.description}</a>
- </%self:detail>
-
- <%self:detail name="In Summa" sum="${e.sum}" set="${e.all}" args="exp" color="#ff2d2d">
- <a href=${"/edit/%s" % exp.id | url}>${exp.day}.${exp.month}. -- ${exp.description}: ${exp.expense | eur}</a>
- </%self:detail>
- <br/>
-% endfor
-
-<%
- if e.date.month == 1:
- date = "%s/12" % (e.date.year - 1)
- else:
- date = "%s/%s" % (e.date.year, e.date.month - 1)
-%>
-${self.left_arrow(date, date)}
-
-% if not is_last:
- <%
- if e.date.month == 13 - len(exps):
- date = "%s/1" % (e.date.year + 1)
- else:
- date = "%s/%s" % (e.date.year, e.date.month + len(exps))
- %>
- ${self.right_arrow(date, date)}
-% endif
-
-<%block name="heading">
- % if len(exps) > 1:
- Aktuelle Kosten
- % else:
- Kosten für ${get_d(exps[0])}
- % endif
-</%block>
-
-<%def name="get_d(e)">
- ${e.date.year}/${e.date.month}
-</%def>
-
-<%block name="js">
- ${parent.js()}
- <script type="text/javascript" src=${"/static/js/show.js" | url}></script>
-</%block>
-
-<%def name="detail(name, sum, set, color=None)">
- <div class="detail">
- <img class="mark" src=${"/static/images/closed.png" | url} />
- <%self:colorize fgcolor="${color}">
- <span class="heading">${name}:</span> <span class="sum">${sum | eur}</span><br/>
- </%self:colorize>
- <div class="details">
- <ul>
- % for exp in set:
- <li class="expense">${caller.body(exp)}</li>
- % endfor
- </ul>
- </div>
- </div>
-</%def>
-
diff --git a/templates/root.jinja b/templates/root.jinja
index b17ac92..dcddc33 100644
--- a/templates/root.jinja
+++ b/templates/root.jinja
@@ -39,7 +39,7 @@
<div id="page">
<div class="content">
- <h1 class="page_heading">{{ heading }}</h1>
+ <h1 class="page_heading">{% block heading -%}{%- endblock %}</h1>
{% block content %}{% endblock %}
</div>
<div style="clear: both;"></div>