summaryrefslogtreecommitdiff
path: root/kosten/app/views/categories.py
blob: 47379ba9f02c9e95755728afb353bf854d4861c3 (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
from . import Blueprint, db, \
        current_user, login_required, \
        templated, redirect, request

from ..model import Category

mod = Blueprint('categories', __name__)

@mod.route('/', methods=('GET', 'POST'))
@login_required
@templated
def manage():
    """Workhorse: List and edit/create. For historic reasons,
    everything is done in JavaScript.

    NB: No deletion possible yet.
    """
    if request.method == 'GET':
        categories = Category.of(current_user).order_by(Category.name).all()

        return { 'cats' : categories }
    else:
        for id, name in request.form.items():
            if id.startswith('n-'):
                db.session.add(Category(name = name, user = current_user))
            else:
                Category.get(id).name = name

        db.session.commit()

        return redirect('.manage')