diff options
Diffstat (limited to '')
-rw-r--r-- | app/views/expenses.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/app/views/expenses.py b/app/views/expenses.py index da01308..312d0c2 100644 --- a/app/views/expenses.py +++ b/app/views/expenses.py @@ -10,6 +10,11 @@ from ..forms import ExpenseForm mod = Blueprint('expenses', __name__) +def expense_form(obj=None): + form = ExpenseForm(obj=obj) + form.category.query = Category.query.order_by("name") + return form + def is_last(exp): return exp.date >= datetime.date.today().replace(day = 1) @@ -69,21 +74,38 @@ def show(year = None, month = None): return render_show([first, second], is_last(first)) -@mod.route("/edit/<int:id>") +@mod.route("/edit/<int:id>", methods=("GET", "POST")) def edit(id): - return render_template("page.jinja") + exp = SingleExpense.get(id) + form = expense_form(exp) + + ret = lambda: render_template("pages/edit.jinja", form=form) + + if request.method == "POST": + if "deleteB" in request.form: + db.session.delete(exp) + + elif form.validate(): # change + form.populate_obj(exp) + + else: + return ret() + + db.session.commit() + return redirect(url_for("index")) + + return ret() @mod.route("/add/", methods=("GET", "POST")) def add(): - form = ExpenseForm() - form.category.query = Category.query.order_by("name") + form = expense_form() if form.validate_on_submit(): - se = SingleExpense() + exp = SingleExpense() - form.populate_obj(se) + form.populate_obj(exp) - db.session.add(se) + db.session.add(exp) db.session.commit() return redirect(url_for("index")) |