summaryrefslogtreecommitdiff
path: root/app/views/consts.py
blob: 97afad1b7d8da73f245f0427289cbac4a0874d36 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
from . import Blueprint, flash, db, \
        current_user, login_required, \
        assert_authorisation, templated, redirect, request, \
        today

from ..model import Category, ConstExpense
from .. import forms as F

import datetime
from sqlalchemy import sql
from functools import partial

assert_authorisation = partial(assert_authorisation, ConstExpense.get)

mod = Blueprint('consts', __name__)

class ConstForm(F.Form):
    start = F.DateField(u'Beginn', F.req,
            format='%m.%Y',
            default=lambda: today())

    end = F.DateField(u'Ende', F.req,
            format='%m.%Y',
            default=lambda: today().replace(year = today().year + 1),
            description=u'(einschließlich)')

    months = F.IntegerField(u'Zahlungsrythmus', F.req,
            description='Monate')

    expense = F.DecimalField(u'Betrag', F.req,
            description=u'EUR',
            places=2)

    description = F.StringField(u'Beschreibung', F.req)

    category = F.QuerySelectField(u'Kategorie',
            get_label='name')

    prev = F.QuerySelectField(u'Vorgänger',
            get_label='description',
            allow_blank=True)

    def __init__(self, cur=None, obj=None):
        obj = cur if obj is None else obj
        super(F.Form, self).__init__(obj=obj)
        self.category.query = Category.of(current_user).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)

        self.prev.query = CE.of(current_user).filter(filter).order_by(CE.description)

@mod.route('/')
@login_required
@templated
def list ():
    d = today()

    expenses = ConstExpense.of(current_user).order_by(ConstExpense.description).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 { 'current': current, 'old': old, 'future': future }

@mod.route('/<int:id>')
@login_required
@assert_authorisation('id')
@templated
def show(id):
    return { 'exp': ConstExpense.get(id) }

@mod.route('/edit/<int:id>', methods=('GET', 'POST'))
@login_required
@assert_authorisation('id')
@templated
def edit(id):
    exp = ConstExpense.get(id)
    form = ConstForm(exp)

    if form.is_submitted():
        if 'deleteB' in request.form:
            db.session.delete(exp)
            db.session.commit()
            return redirect('.list')

        elif form.flash_validate(): # change
            form.populate_obj(exp)
            db.session.commit()
            flash(u"Eintrag geändert.")
            return redirect('.show', id = id)

    return { 'form': form }

@mod.route('/add/from/<int:other>')
@login_required
@assert_authorisation('other')
@templated('.add')
def add_from(other):
    exp = ConstExpense() # needed to initialize 'CE.next'

    other = ConstExpense.get(other)

    # get form with data from other
    form = ConstForm(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 { 'form': form }

@mod.route('/add/', methods=('GET', 'POST'))
@login_required
@templated
def add ():
    exp = ConstExpense()

    form = ConstForm()

    if form.validate_on_submit():
        form.populate_obj(exp)
        exp.user = current_user
        db.session.add(exp)
        db.session.commit()
        flash(u"Eintrag hinzugefügt.")
        return redirect('.show', id = exp.id)

    return { 'form': form }