summaryrefslogtreecommitdiff
path: root/app/views
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.net>2013-04-12 01:32:37 +0200
committerRené 'Necoro' Neumann <necoro@necoro.net>2013-04-12 01:32:37 +0200
commitabfc1005db0f256ca60823f61b8a904304eb9a4c (patch)
tree87c1f13512cae335759d9bf43dab7fc60c3afec0 /app/views
parent978f1a4999937a56bd3861a219bb2d9082ba5de6 (diff)
downloadkosten-abfc1005db0f256ca60823f61b8a904304eb9a4c.tar.gz
kosten-abfc1005db0f256ca60823f61b8a904304eb9a4c.tar.bz2
kosten-abfc1005db0f256ca60823f61b8a904304eb9a4c.zip
In Flask/Jinja: Show / Categories
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
4 files changed, 110 insertions, 9 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")