From abfc1005db0f256ca60823f61b8a904304eb9a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 12 Apr 2013 01:32:37 +0200 Subject: In Flask/Jinja: Show / Categories --- app/views.py | 32 ------------------- app/views/__init__.py | 33 ++++++++++++++++++++ app/views/categories.py | 12 ++++++++ app/views/consts.py | 12 ++++++++ app/views/expenses.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++ templates/404.jinja | 3 +- templates/menu.jinja | 8 ++--- templates/page.jinja | 16 +++++----- templates/pages/cats.jinja | 22 ++++++++++++++ templates/pages/cats.mako | 22 -------------- templates/pages/show.jinja | 59 +++++++++++++++++++++++++++++++++++ templates/pages/show.mako | 73 -------------------------------------------- templates/root.jinja | 2 +- 13 files changed, 229 insertions(+), 141 deletions(-) delete mode 100644 app/views.py create mode 100644 app/views/__init__.py create mode 100644 app/views/categories.py create mode 100644 app/views/consts.py create mode 100644 app/views/expenses.py create mode 100644 templates/pages/cats.jinja delete mode 100644 templates/pages/cats.mako create mode 100644 templates/pages/show.jinja delete mode 100644 templates/pages/show.mako diff --git a/app/views.py b/app/views.py deleted file mode 100644 index accbeb5..0000000 --- a/app/views.py +++ /dev/null @@ -1,32 +0,0 @@ -from flask import render_template, request, url_for -import flask - -from . import app, db - -# check for mobile visitors -mobile_checks = ["J2ME", "Opera Mini"] - -@app.before_request -def handle_mobile(): - ua = request.environ.get("HTTP_USER_AGENT", "") - - 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.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") - -@app.route("/add") -def addExp(): - return render_template("root.jinja") diff --git a/app/views/__init__.py b/app/views/__init__.py new file mode 100644 index 0000000..dc662ec --- /dev/null +++ b/app/views/__init__.py @@ -0,0 +1,33 @@ +from flask import render_template, request, url_for +import flask + +from .. import app, db + +# check for mobile visitors +mobile_checks = ["J2ME", "Opera Mini"] + +@app.before_request +def handle_mobile(): + ua = request.environ.get("HTTP_USER_AGENT", "") + + 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 + +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.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("/") +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("//") +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/") +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 %}

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) -%} - +{% macro left_arrow(target,label) %} + {{label}} -{%- endmacro %} +{% endmacro %} -{% macro right_arrow(target,label) -%} - +{% macro right_arrow(target,label) %} + {{label}} -{%- 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()}} {% 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() }} + +{% endblock %} + +{% block content %} +

+
    + {% for c in cats %} +
  • {{c.name}}
  • + {%- endfor %} +
    +
+ +
+ +{% 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" /> - -
-
    - % for c in cats: -
  • ${c.name}
  • - % endfor -
    -
- - -
- - -<%block name="heading"> - Kategorien - - -<%block name="js"> - ${parent.js()} - - 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() }} + +{% endblock %} + +{% block content %} + {% for e in exps %} + {% if exps | length > 1 %}

{{e|date}}

{% endif %} + {% for c in e.catexps | sort(attribute="cat.name") %} + {% call(exp) detail(name=c.cat.name, sum=c.expense, set=c.all) %} + {{exp.day}}.{{exp.month}}. -- {{exp.description}}: {{exp.expense | eur }} + {% endcall %} + {% endfor %} + + {% call(exp) detail(name="Constant", sum=e.constsum, set=e.consts) %} + {{exp.monthly}} -- {{exp.description}} + {% endcall %} + + {% call(exp) detail(name="In Summa", sum=e.sum, set=e.all, color="#ff2d2d") %} + {{exp.day}}.{{exp.month}}. -- {{exp.description}}: {{exp.expense | eur}} + {% endcall %} +
+ {% 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) %} +
+ + {% call colorize(fgcolor=color) %} + {{name}}: {{sum | eur}}
+ {% endcall %} +
+
    + {% for exp in set %} +
  • {{ caller(exp) }}
  • + {% endfor %} +
+
+
+{% 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: -

${get_d(e)}

- % 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"> - ${exp.day}.${exp.month}. -- ${exp.description}: ${exp.expense | eur} - - % endfor - - <%self:detail name="Constant" sum="${e.constsum}" set="${e.consts}" args="exp"> - ${exp.monthly} -- ${exp.description} - - - <%self:detail name="In Summa" sum="${e.sum}" set="${e.all}" args="exp" color="#ff2d2d"> - ${exp.day}.${exp.month}. -- ${exp.description}: ${exp.expense | eur} - -
-% 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 - - -<%def name="get_d(e)"> - ${e.date.year}/${e.date.month} - - -<%block name="js"> - ${parent.js()} - - - -<%def name="detail(name, sum, set, color=None)"> -
- - <%self:colorize fgcolor="${color}"> - ${name}: ${sum | eur}
- -
-
    - % for exp in set: -
  • ${caller.body(exp)}
  • - % endfor -
-
-
- - 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 @@
-

{{ heading }}

+

{% block heading -%}{%- endblock %}

{% block content %}{% endblock %}
-- cgit v1.2.3