+ ident=prefix.per_host[cat]+"."+t.strftime("%Y%m%d")+"."+("%03d" % (int(sr[0][0].split(".")[2])+1))
+ p = db.prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\", \"type\", \"identifier\", \"host\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5, $6, $7)")
+ p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat, ident, request.host)
+ return rel_redirect("/")
+
+def motion_edited(motion):
+ return rel_redirect("/?start=" + str(motion) + "#motion-" + str(motion))
+
+def validate_motion_access(privilege):
+ def decorator(f):
+ def decorated_function(motion):
+ db = get_db()
+ with db.xact():
+ rv = db.prepare("SELECT id, type, deadline < CURRENT_TIMESTAMP AS expired, canceled FROM motion WHERE identifier=$1 AND host=$2")(motion, request.host);
+ if len(rv) == 0:
+ return "Error, Not found", 404
+ id = rv[0].get("id")
+ if not may(privilege, rv[0].get("type")):
+ return "Forbidden", 403
+ if rv[0].get("canceled") is not None:
+ return "Error, motion was canceled", 403
+ if rv[0].get("expired"):
+ return "Error, out of time", 403
+ return f(motion, id)
+ decorated_function.__name__ = f.__name__
+ return decorated_function
+ return decorator
+
+@app.route("/motion/<string:motion>/cancel", methods=['POST'])
+@validate_motion_access('cancel')
+def cancel_motion(motion, id):
+ if request.form.get("reason", "none") == "none":
+ return "Error, form requires reason", 500
+ rv = get_db().prepare("UPDATE motion SET canceled=CURRENT_TIMESTAMP, cancelation_reason=$1, canceled_by=$2 WHERE identifier=$3 AND host=$4 AND canceled is NULL")(request.form.get("reason", ""), g.voter, motion, request.host)
+ return motion_edited(id)
+
+@app.route("/motion/<string:motion>/finish", methods=['POST'])
+@validate_motion_access('finish')
+def finish_motion(motion, id):
+ rv = get_db().prepare("UPDATE motion SET deadline=CURRENT_TIMESTAMP WHERE identifier=$1 AND host=$2 AND canceled is NULL")(motion, request.host)
+ return motion_edited(id)