]> WPIA git - motion.git/blob - motion.py
add: cancel running motion (with comment)
[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 import filters
8
9 times=[3,5,14]
10
11 def get_db():
12     db = getattr(g, '_database', None)
13     if db is None:
14         db = g._database = postgresql.open(config.DATABASE, user=config.USER, password=config.PASSWORD)
15     #db.row_factory = sqlite3.Row
16     return db
17
18 app = Flask(__name__)
19 app.register_blueprint(filters.blueprint)
20
21
22
23 @app.teardown_appcontext
24 def close_connection(exception):
25     db = getattr(g, '_database', None)
26     if db is not None:
27         db.close()
28
29 def init_db():
30     with app.app_context():
31         db = get_db()
32         with app.open_resource('schema.sql', mode='r') as f:
33             db.execute(f.read())
34
35 @app.route("/")
36 def main():
37     start=int(request.args.get("start", "-1"));
38     q = "SELECT motion.*, votes.*, poser.email AS poser, canceler.email AS canceler, (motion.deadline > CURRENT_TIMESTAMP AND canceled is NULL) AS running FROM motion LEFT JOIN (SELECT motion_id, voter_id, "\
39                              + "COUNT(CASE WHEN result='yes' THEN 'yes' ELSE NULL END) as yes, "\
40                              + "COUNT(CASE WHEN result='no' THEN 'no' ELSE NULL END) as no, "\
41                              + "COUNT(CASE WHEN result='abstain' THEN 'abstain' ELSE NULL END) as abstain "\
42                              + "FROM vote GROUP BY motion_id, voter_id) as votes ON votes.motion_id=motion.id "\
43                              + "LEFT JOIN voter poser ON poser.id = motion.posed_by "\
44                              + "LEFT JOIN voter canceler ON canceler.id = motion.canceled_by "
45     prev=None
46     if start == -1:
47         p = get_db().prepare(q + "ORDER BY motion.id DESC LIMIT 11")
48         rv = p()
49     else:
50         p = get_db().prepare(q + "WHERE motion.id <= $1 ORDER BY motion.id DESC LIMIT 11")
51         rv = p(start)
52         rs = get_db().prepare("SELECT id FROM motion WHERE motion.id > $1 ORDER BY id ASC LIMIT 10")(start)
53         if len(rs) == 10:
54             prev = rs[9][0]
55         else:
56             prev = -1
57     return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times, prev=prev)
58
59 @app.route("/motion", methods=['POST'])
60 def put_motion():
61     time = int(request.form.get("days", "3"));
62     if time not in times:
63         return "Error, invalid length"
64     p = get_db().prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4)")
65     p(request.form.get("title", ""), request.form.get("content",""), time, voter)
66     return redirect("/")
67
68 voter=1
69
70 def motion_edited(motion):
71     return redirect("/?start=" + str(motion) + "#motion-" + str(motion))
72
73 @app.route("/motion/<int:id>/cancel", methods=['POST'])
74 def cancel_motion(id):
75     if request.form.get("reason", "none") == "none":
76         return "Error, form requires reason"
77     rv = get_db().prepare("UPDATE motion SET canceled=CURRENT_TIMESTAMP, cancelation_reason=$1, canceled_by=$2 WHERE id=$3 AND canceled is NULL")(request.form.get("reason", ""), voter, id)
78     print(rv)
79     return motion_edited(id)
80
81 @app.route("/motion/<int:motion>")
82 def show_motion(motion):
83     p = get_db().prepare("SELECT motion.*, poser.email AS poser, canceler.email AS canceler, (motion.deadline > CURRENT_TIMESTAMP AND canceled is NULL) AS running, vote.result FROM motion "\
84                          + "LEFT JOIN vote on vote.motion_id=motion.id AND vote.voter_id=$2 "\
85                          + "LEFT JOIN voter poser ON poser.id = motion.posed_by "\
86                          + "LEFT JOIN voter canceler ON canceler.id = motion.canceled_by "
87                          + "WHERE motion.id=$1")
88     rv = p(motion,voter)
89     if len(rv) == 0:
90         return "Error, motion not found" # TODO 404
91     return render_template('single_motion.html', motion=rv[0])
92
93 @app.route("/motion/<int:motion>/vote", methods=['POST'])
94 def vote(motion):
95     v = request.form.get("vote", "abstain")
96     db = get_db()
97     with db.xact():
98         p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE id = $1")
99         if not p(motion)[0][0]:
100             return "Error, motion deadline has passed"
101         p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2")
102         rv = p(motion, voter)
103         if len(rv) == 0:
104             db.prepare("INSERT INTO vote(motion_id, voter_id, result) VALUES($1,$2,$3)")(motion,voter,v)
105         else:
106             db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(motion,voter,v)
107     return motion_edited(motion)
108
109 # TODO authentication/user management
110 # TODO load config with flask mechanism