summaryrefslogtreecommitdiff
path: root/app/views/categories.py
blob: ca9b82898ed85a180d4e5f9805a649404eb60bf4 (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.iteritems():
            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')