2 from flask import Flask
3 from flask import render_template, redirect
4 from flask import request
11 db = getattr(g, '_database', None)
13 db = g._database = postgresql.open(app.config.get("DATABASE"), user=app.config.get("USER"), password=app.config.get("PASSWORD"))
14 #db.row_factory = sqlite3.Row
18 app.register_blueprint(filters.blueprint)
21 app.config.from_pyfile('config.py')
23 groups=["fellowship", "board"]
29 if "USER_ROLES" in env:
30 parts = env.get("USER_ROLES").split("/", 1)
34 if "USER" in env and "ROLES" in env:
35 user = env.get("USER")
36 roles = env.get("ROLES")
39 return "Server misconfigured", 500
40 roles = roles.split(" ")
42 if user == "<invalid>":
43 return "Access denied", 403;
47 rv = db.prepare("SELECT id FROM voter WHERE email=$1")(user)
49 db.prepare("INSERT INTO voter(\"email\") VALUES($1)")(user)
50 rv = db.prepare("SELECT id FROM voter WHERE email=$1")(user)
51 g.voter = rv[0].get("id");
57 if a[0] not in g.roles:
60 g.roles[a[0]] = groups
62 g.roles[a[0]].append(val)
65 def get_allowed_cats(action):
66 return g.roles.get(action, []);
68 def may(action, motion):
69 return motion in get_allowed_cats(action)
71 @app.teardown_appcontext
72 def close_connection(exception):
73 db = getattr(g, '_database', None)
78 with app.app_context():
80 with app.open_resource('schema.sql', mode='r') as f:
85 start=int(request.args.get("start", "-1"));
86 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, "\
87 + "COUNT(CASE WHEN result='yes' THEN 'yes' ELSE NULL END) as yes, "\
88 + "COUNT(CASE WHEN result='no' THEN 'no' ELSE NULL END) as no, "\
89 + "COUNT(CASE WHEN result='abstain' THEN 'abstain' ELSE NULL END) as abstain "\
90 + "FROM vote GROUP BY motion_id) as votes ON votes.motion_id=motion.id "\
91 + "LEFT JOIN voter poser ON poser.id = motion.posed_by "\
92 + "LEFT JOIN voter canceler ON canceler.id = motion.canceled_by "
95 p = get_db().prepare(q + "ORDER BY motion.id DESC LIMIT 11")
98 p = get_db().prepare(q + "WHERE motion.id <= $1 ORDER BY motion.id DESC LIMIT 11")
100 rs = get_db().prepare("SELECT id FROM motion WHERE motion.id > $1 ORDER BY id ASC LIMIT 10")(start)
105 return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times, prev=prev,
106 categories=get_allowed_cats("create"))
108 @app.route("/motion", methods=['POST'])
110 cat=request.form.get("category", "")
111 if cat not in get_allowed_cats("create"):
112 return "Forbidden", 403
113 time = int(request.form.get("days", "3"));
114 if time not in times:
115 return "Error, invalid length", 500
116 p = get_db().prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\", \"type\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5)")
117 p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat)
120 def motion_edited(motion):
121 return redirect("/?start=" + str(motion) + "#motion-" + str(motion))
123 @app.route("/motion/<int:motion>/cancel", methods=['POST'])
124 def cancel_motion(motion):
125 rv = get_db().prepare("SELECT type FROM motion WHERE id=$1")(motion);
127 return "Error, Not found", 404
128 if not may("cancel", rv[0].get("type")):
129 return "Forbidden", 403
130 if request.form.get("reason", "none") == "none":
131 return "Error, form requires reason", 500
132 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", ""), g.voter, motion)
133 return motion_edited(motion)
135 @app.route("/motion/<int:motion>")
136 def show_motion(motion):
137 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 "\
138 + "LEFT JOIN vote on vote.motion_id=motion.id AND vote.voter_id=$2 "\
139 + "LEFT JOIN voter poser ON poser.id = motion.posed_by "\
140 + "LEFT JOIN voter canceler ON canceler.id = motion.canceled_by "
141 + "WHERE motion.id=$1")
142 rv = p(motion, g.voter)
143 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")))
145 @app.route("/motion/<int:motion>/vote", methods=['POST'])
147 v = request.form.get("vote", "abstain")
150 rv = db.prepare("SELECT type FROM motion WHERE id=$1")(motion);
152 return "Error, Not found", 404
153 if not may("vote", rv[0].get("type")):
154 return "Forbidden", 403
155 p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE id = $1")
156 if not p(motion)[0][0]:
157 return "Error, motion deadline has passed", 500
158 p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2")
159 rv = p(motion, g.voter)
161 db.prepare("INSERT INTO vote(motion_id, voter_id, result) VALUES($1,$2,$3)")(motion, g.voter, v)
163 db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(motion, g.voter, v)
164 return motion_edited(motion)