summaryrefslogtreecommitdiff
path: root/app/views/consts.py
blob: a4980fba8c66de49a6ddca8d4c8667741b34dd23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from ..flask_extend import Blueprint
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 T(tpl):
    return "consts/%s.jinja" % tpl

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 list ():
    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(T("list"), current = current, old = old, future = future)

@mod.route("/<int:id>")
def show(id):
    return render_template(T("show"), 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(".list"))

        elif form.validate(): # change
            form.populate_obj(exp)
            db.session.commit()
            return redirect(url_for(".show", id = exp.id))

    return render_template(T("edit"), 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(T("add"), form = form)