]> WPIA git - motion.git/blobdiff - motion.py
adding automatic schema migration mechanism
[motion.git] / motion.py
index 783cf476fe7eca1b95766acedf4117b9534b217e..741eb302d0d37f6cebc8f859ed8fe5d655260527 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -78,8 +78,16 @@ def close_connection(exception):
 def init_db():
     with app.app_context():
         db = get_db()
-        with app.open_resource('schema.sql', mode='r') as f:
-            db.execute(f.read())
+        try:
+            ver = db.prepare("SELECT version FROM schema_version")()[0][0];
+            print("Database Schema version: ", ver)
+        except postgresql.exceptions.UndefinedTableError:
+            ver = 0
+        if ver < 1:
+            with app.open_resource('sql/schema.sql', mode='r') as f:
+                db.execute(f.read())
+
+init_db()
 
 @app.route("/")
 def main():
@@ -106,6 +114,11 @@ def main():
     return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times, prev=prev,
                            categories=get_allowed_cats("create"))
 
+def rel_redirect(loc):
+    r = redirect(loc)
+    r.autocorrect_location_header = False
+    return r
+
 @app.route("/motion", methods=['POST'])
 def put_motion():
     cat=request.form.get("category", "")
@@ -116,10 +129,10 @@ def put_motion():
         return "Error, invalid length", 500
     p = get_db().prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\", \"type\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5)")
     p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat)
-    return redirect("/")
+    return rel_redirect("/")
 
 def motion_edited(motion):
-    return redirect("/?start=" + str(motion) + "#motion-" + str(motion))
+    return rel_redirect("/?start=" + str(motion) + "#motion-" + str(motion))
 
 @app.route("/motion/<int:motion>/cancel", methods=['POST'])
 def cancel_motion(motion):
@@ -141,7 +154,10 @@ def show_motion(motion):
                          + "LEFT JOIN voter canceler ON canceler.id = motion.canceled_by "
                          + "WHERE motion.id=$1")
     rv = p(motion, g.voter)
-    return render_template('single_motion.html', motion=rv[0], may_vote=may("vote", rv[0].get("type")), may_cancel=may("cancel", rv[0].get("type")))
+    votes = None
+    if may("audit", rv[0].get("type")) and not rv[0].get("running") and not rv[0].get("canceled"):
+        votes = get_db().prepare("SELECT vote.result, voter.email FROM vote INNER JOIN voter ON voter.id = vote.voter_id WHERE vote.motion_id=$1")(motion);
+    return render_template('single_motion.html', motion=rv[0], may_vote=may("vote", rv[0].get("type")), may_cancel=may("cancel", rv[0].get("type")), votes=votes)
 
 @app.route("/motion/<int:motion>/vote", methods=['POST'])
 def vote(motion):