]> WPIA git - motion.git/blobdiff - motion.py
chg: crop time at second precision
[motion.git] / motion.py
index 52547ce6d374dd9387d8c2210c4b347103bf64cf..b74438d3a92aaaa89f5ae7820f1fd145c729113a 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -4,7 +4,9 @@ from flask import render_template, redirect
 from flask import request
 import postgresql
 import config
+import filters
 
+times=[3,5,14]
 
 def get_db():
     db = getattr(g, '_database', None)
@@ -14,6 +16,9 @@ def get_db():
     return db
 
 app = Flask(__name__)
+app.register_blueprint(filters.blueprint)
+
+
 
 @app.teardown_appcontext
 def close_connection(exception):
@@ -35,18 +40,27 @@ def main():
                              + "COUNT(CASE WHEN result='no' THEN 'no' ELSE NULL END) as no, "\
                              + "COUNT(CASE WHEN result='abstain' THEN 'abstain' ELSE NULL END) as abstain "\
                              + "FROM vote GROUP BY motion_id, voter_id) as votes ON votes.motion_id=motion.id "
+    prev=None
     if start == -1:
         p = get_db().prepare(q + "ORDER BY id DESC LIMIT 11")
         rv = p()
     else:
         p = get_db().prepare(q + "WHERE id <= $1 ORDER BY id DESC LIMIT 11")
         rv = p(start)
-    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None)
+        rs = get_db().prepare("SELECT id FROM motion WHERE id > $1 ORDER BY id ASC LIMIT 10")(start)
+        if len(rs) == 10:
+            prev = rs[9][0]
+        else:
+            prev = -1
+    return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times, prev=prev)
 
 @app.route("/motion", methods=['POST'])
 def put_motion():
-    p = get_db().prepare("INSERT INTO motion(\"name\", \"content\") VALUES($1, $2)")
-    p(request.form.get("title", ""), request.form.get("content",""))
+    time = int(request.form.get("days", "3"));
+    if time not in times:
+        return "Error, invalid length"
+    p = get_db().prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days')")
+    p(request.form.get("title", ""), request.form.get("content",""), time)
     return redirect("/")
 
 voter=1
@@ -71,4 +85,7 @@ def vote(motion):
             db.prepare("INSERT INTO vote (motion_id, voter_id, result) VALUES($1,$2,$3)")(motion,voter,v)
         else:
             db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(motion,voter,v)
-    return redirect("/motion/" + str(motion))
+    return redirect("/?start=" + str(motion) + "#motion-" + str(motion))
+
+# TODO cancel running motion (with comment)
+# TODO authentication/user management