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')