From a98445a75c3ac2a46540eee146129f7c77e005d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sun, 14 Apr 2013 20:16:23 +0200 Subject: Simplify some things --- app/views/expenses.py | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'app') diff --git a/app/views/expenses.py b/app/views/expenses.py index 312d0c2..400876c 100644 --- a/app/views/expenses.py +++ b/app/views/expenses.py @@ -15,9 +15,6 @@ def expense_form(obj=None): form.category.query = Category.query.order_by("name") return form -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) @@ -29,19 +26,25 @@ def calc_month_exp(year, month): 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 } +@mod.context_processor +def inject_funs(): + 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): + def next_date(exp): + if exp.date.month == 12: return { "year": exp.date.year + 1, "month": 1 } else: - return { "year": exp.date.year, "month": exp.date.month + len(exps) } - return _next_date + return { "year": exp.date.year, "month": exp.date.month + 1} + + def is_last(exp): + return exp.date >= datetime.date.today().replace(day = 1) + + + return { 'prev_date' : prev_date, 'next_date' : next_date, 'is_last': is_last } @mod.app_template_filter("date") def format_date(s): @@ -50,17 +53,10 @@ def format_date(s): 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)) + return render_template("pages/show.jinja", exps = [c]) @mod.route("/") def show(year = None, month = None): @@ -72,7 +68,7 @@ def show(year = None, month = None): else: second = calc_month_exp(d.year, d.month - 1) - return render_show([first, second], is_last(first)) + return render_template("pages/show.jinja", exps = [first, second]) @mod.route("/edit/", methods=("GET", "POST")) def edit(id): -- cgit v1.2.3