コード例 #1
0
ファイル: app.py プロジェクト: nicopineda/somewebapp
def category():
    loc = "nav-category"
    if request.method == "POST" and request.form["action"] == "insert":
        #insert new category
        description = request.form["description"]
        category_type = request.form["categorytype"]
        success = False

        category = Category(g.user.id, description, category_type)
        if not category.isExists():
            db.session.add(category)
            db.session.commit()
            success = (True if category.id != -1 else False)
        return render_template("category.html", success = success, locid = loc)
    elif request.method == "POST" and request.form["action"] == "add":
        #show add category
        return render_template("category.html", action = "add", locid = loc)
    elif request.method == "POST" and request.form["action"] == "edit":
        #show edit category
        category_id = request.form["categoryid"]
        return render_template("category.html", action = "edit", category = Category.query.filter_by(id=category_id).one(), locid = loc)
    elif request.method == "POST" and request.form["action"] == "fin-edit":
        #show finalise edit category
        category_id = request.form["categoryid"]
        description = request.form["description"]
        category_type = request.form["categorytype"]
        success = False

        category = Category(g.user.id, description, category_type)
        if not category.isExists():
            category = Category.query.get(category_id)
            category.description = description
            category.category_type = category_type
            db.session.commit()
            success = True

        return render_template("category.html", action = "edit-fin", success = success, locid = loc)
    elif request.method == "POST" and request.form["action"] == "delete":
        #delete category
        category_id = request.form["categoryid"]
        category = Category.query.get(category_id)
        db.session.delete(category)
        db.session.commit()
        return render_template("category.html", action = "delete-fin", locid = loc)
    else:
        #show category list
        rs = db.session.query(Category).filter_by(user_id=g.user.id)
        return render_template("category.html", action = "show", category = rs, locid = loc)