]> WPIA git - motion.git/blob - motion.py
initial commit for motion applicaiton
[motion.git] / motion.py
1 from flask import g
2 from flask import Flask
3 from flask import render_template, redirect
4 from flask import request
5 import postgresql
6 import config
7
8
9 def get_db():
10     db = getattr(g, '_database', None)
11     if db is None:
12         db = g._database = postgresql.open(config.DATABASE, user=config.USER, password=config.PASSWORD)
13     #db.row_factory = sqlite3.Row
14     return db
15
16 app = Flask(__name__)
17
18 @app.teardown_appcontext
19 def close_connection(exception):
20     db = getattr(g, '_database', None)
21     if db is not None:
22         db.close()
23
24 def init_db():
25     with app.app_context():
26         db = get_db()
27         with app.open_resource('schema.sql', mode='r') as f:
28             db.execute(f.read())
29
30 @app.route("/")
31 def main():
32     start=int(request.args.get("start", "-1"));
33     q = "SELECT *, motion.deadline > CURRENT_TIMESTAMP AS running FROM motion LEFT JOIN (SELECT motion_id, voter_id, "\
34                              + "COUNT(CASE WHEN result='yes' THEN 'yes' ELSE NULL END) as yes, "\
35                              + "COUNT(CASE WHEN result='no' THEN 'no' ELSE NULL END) as no, "\
36                              + "COUNT(CASE WHEN result='abstain' THEN 'abstain' ELSE NULL END) as abstain "\
37                              + "FROM vote GROUP BY motion_id, voter_id) as votes ON votes.motion_id=motion.id "
38     if start == -1:
39         p = get_db().prepare(q + "ORDER BY id DESC LIMIT 11")
40         rv = p()
41     else:
42         p = get_db().prepare(q + "WHERE id <= $1 ORDER BY id DESC LIMIT 11")
43         rv = p(start)
44     return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None)
45
46 @app.route("/motion", methods=['POST'])
47 def put_motion():
48     p = get_db().prepare("INSERT INTO motion(\"name\", \"content\") VALUES($1, $2)")
49     p(request.form.get("title", ""), request.form.get("content",""))
50     return redirect("/")
51
52 voter=1
53
54 @app.route("/motion/<int:id>")
55 def show_motion(id):
56     p = get_db().prepare("SELECT motion.*, motion.deadline > CURRENT_TIMESTAMP AS running, vote.result FROM motion LEFT JOIN vote on vote.motion_id=motion.id AND vote.voter_id=$2 WHERE id=$1")
57     rv = p(id,voter)
58     return render_template('single_motion.html', motion=rv[0])
59
60 @app.route("/motion/<int:motion>/vote", methods=['POST'])
61 def vote(motion):
62     v = request.form.get("vote", "abstain")
63     db = get_db()
64     with db.xact():
65         p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE id = $1")
66         if not p(motion)[0][0]:
67             return "Error, motion deadline has passed"
68         p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2")
69         rv = p(motion, voter)
70         if len(rv) == 0:
71             db.prepare("INSERT INTO vote (motion_id, voter_id, result) VALUES($1,$2,$3)")(motion,voter,v)
72         else:
73             db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(motion,voter,v)
74     return redirect("/motion/" + str(motion))