X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=motion.py;h=f6ddf1344004ee30bd5bbb40d096eadcd33cea90;hb=fd16e7d1b4090b8466997b98ce84e5a9d9ebc4d0;hp=cef9a6bc584b03ae4cc43e4b4c1a96984f1c554e;hpb=fb14879eed07a490aab3a9263db29e4a0323ae41;p=motion.git diff --git a/motion.py b/motion.py index cef9a6b..f6ddf13 100644 --- a/motion.py +++ b/motion.py @@ -4,8 +4,8 @@ from flask import render_template, redirect from flask import request import postgresql import filters - -times=[3,5,14] +from flaskext.markdown import Markdown +from markdown.extensions import Extension def get_db(): db = getattr(g, '_database', None) @@ -17,20 +17,49 @@ def get_db(): app = Flask(__name__) app.register_blueprint(filters.blueprint) +class EscapeHtml(Extension): + def extendMarkdown(self, md, md_globals): + del md.preprocessors['html_block'] + del md.inlinePatterns['html'] + +md = Markdown(app, extensions=[EscapeHtml()]) + # Load config app.config.from_pyfile('config.py') -groups=["fellowship", "board"] +prefix=app.config.get("GROUP_PREFIX") + +times=app.config.get("DURATION") + +debuguser=app.config.get("DEBUGUSER") @app.before_request def lookup_user(): + global prefix + env = request.environ - if "USER" not in env or "ROLES" not in env: + user = None + if debuguser is not None: + parts =debuguser[request.host].split("/", 1) + user = parts[0] + roles = parts[1] + + if "USER_ROLES" in env: + parts = env.get("USER_ROLES").split("/", 1) + user = parts[0] + roles = parts[1] + + if "USER" in env and "ROLES" in env: + user = env.get("USER") + roles = env.get("ROLES") + + + if user is None: return "Server misconfigured", 500 - user = env.get("USER") - roles = env.get("ROLES").split(" ") - if roles == [""]: - roles = [] + roles = roles.split(" ") + + if user == "": + return "Access denied", 403; db = get_db() with db.xact(): @@ -39,17 +68,19 @@ def lookup_user(): db.prepare("INSERT INTO voter(\"email\") VALUES($1)")(user) rv = db.prepare("SELECT id FROM voter WHERE email=$1")(user) g.voter = rv[0].get("id"); + g.user = user g.roles = {} for r in roles: a = r.split(":", 1) - val = a[1] - if a[0] not in g.roles: - g.roles[a[0]] = [] - if val == "*": - g.roles[a[0]] = groups - else: - g.roles[a[0]].append(val) + if len(r)!=0: + val = a[1] + if a[0] not in g.roles: + g.roles[a[0]] = [] + if val == "*": + g.roles[a[0]] = [group for group in prefix[request.host]] + else: + g.roles[a[0]].append(val) return None def get_allowed_cats(action): @@ -67,8 +98,46 @@ 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()) + return + + if ver < 2: + with app.open_resource('sql/from_1.sql', mode='r') as f: + db.execute(f.read()) + ct={} + for g in [group for group in prefix[app.config.get("DEFAULT_HOST")]]: + ct[g] = {"dt": "", "c": 0} + + p = db.prepare("UPDATE \"motion\" SET \"identifier\"=$1 WHERE \"id\"=$2") + for row in db.prepare("SELECT id, \"type\", \"posed\" FROM \"motion\" ORDER BY \"id\" ASC"): + dt=row[2].strftime("%Y%m%d") + if ct[row[1]]["dt"] != dt: + ct[row[1]]["dt"] = dt + ct[row[1]]["c"] = 0 + ct[row[1]]["c"] = ct[row[1]]["c"] + 1 + name=prefix[app.config.get("DEFAULT_HOST")][row[1]]+"."+dt+"."+("%03d" % ct[row[1]]["c"]) + p(name, row[0]) + db.prepare("ALTER TABLE \"motion\" ALTER COLUMN \"identifier\" SET NOT NULL")() + db.prepare("UPDATE \"schema_version\" SET \"version\"=2")() + db.prepare("CREATE UNIQUE INDEX motion_ident ON motion (identifier)")() + + if ver < 3: + with app.open_resource('sql/from_2.sql', mode='r') as f: + db.execute(f.read()) + db.prepare("UPDATE \"motion\" SET \"host\"=$1")(app.config.get("DEFAULT_HOST")) + db.prepare("ALTER TABLE \"motion\" ALTER COLUMN \"host\" SET NOT NULL")() + db.prepare("UPDATE \"schema_version\" SET \"version\"=3")() + + +init_db() @app.route("/") def main(): @@ -82,73 +151,93 @@ def main(): + "LEFT JOIN voter canceler ON canceler.id = motion.canceled_by " prev=None if start == -1: - p = get_db().prepare(q + "ORDER BY motion.id DESC LIMIT 11") - rv = p() + p = get_db().prepare(q + "WHERE motion.host = $1 ORDER BY motion.id DESC LIMIT 11") + rv = p(request.host) else: - p = get_db().prepare(q + "WHERE motion.id <= $1 ORDER BY motion.id DESC LIMIT 11") - rv = p(start) - rs = get_db().prepare("SELECT id FROM motion WHERE motion.id > $1 ORDER BY id ASC LIMIT 10")(start) + p = get_db().prepare(q + "WHERE motion.host = $1 AND motion.id <= $2 ORDER BY motion.id DESC LIMIT 11") + rv = p(request.host, start) + rs = get_db().prepare("SELECT id FROM motion WHERE motion.host = $1 AND motion.id > $2 ORDER BY id ASC LIMIT 10")(request.host, 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, + return render_template('index.html', motions=rv[:10], more=rv[10]["id"] if len(rv) == 11 else None, times=times[request.host], 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", "") if cat not in get_allowed_cats("create"): return "Forbidden", 403 time = int(request.form.get("days", "3")); - if time not in times: + if time not in times[request.host]: 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("/") + db = get_db() + with db.xact(): + t = db.prepare("SELECT CURRENT_TIMESTAMP")()[0][0]; + s = db.prepare("SELECT MAX(\"identifier\") FROM \"motion\" WHERE \"type\"=$1 AND \"host\"=$2 AND DATE(\"posed\")=DATE(CURRENT_TIMESTAMP)") + sr = s(cat, request.host) + ident="" + if len(sr) == 0 or sr[0][0] is None: + ident=prefix[request.host][cat]+"."+t.strftime("%Y%m%d")+".001" + else: + ident=prefix[request.host][cat]+"."+t.strftime("%Y%m%d")+"."+("%03d" % (int(sr[0][0].split(".")[2])+1)) + p = db.prepare("INSERT INTO motion(\"name\", \"content\", \"deadline\", \"posed_by\", \"type\", \"identifier\", \"host\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5, $6, $7)") + p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat, ident, request.host) + 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//cancel", methods=['POST']) +@app.route("/motion//cancel", methods=['POST']) def cancel_motion(motion): - rv = get_db().prepare("SELECT type FROM motion WHERE id=$1")(motion); + rv = get_db().prepare("SELECT id, type FROM motion WHERE identifier=$1 AND host=$2")(motion, request.host); if len(rv) == 0: return "Error, Not found", 404 + id = rv[0].get("id") if not may("cancel", rv[0].get("type")): return "Forbidden", 403 if request.form.get("reason", "none") == "none": return "Error, form requires reason", 500 - 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) - return motion_edited(motion) + rv = get_db().prepare("UPDATE motion SET canceled=CURRENT_TIMESTAMP, cancelation_reason=$1, canceled_by=$2 WHERE identifier=$3 AND host=$4 AND canceled is NULL")(request.form.get("reason", ""), g.voter, motion, request.host) + return motion_edited(id) -@app.route("/motion/") +@app.route("/motion/") def show_motion(motion): 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 "\ + "LEFT JOIN vote on vote.motion_id=motion.id AND vote.voter_id=$2 "\ + "LEFT JOIN voter poser ON poser.id = motion.posed_by "\ + "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"))) - -@app.route("/motion//vote", methods=['POST']) + + "WHERE motion.identifier=$1 AND motion.host=$3") + rv = p(motion, g.voter, request.host) + 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")(rv[0].get("id")); + 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//vote", methods=['POST']) def vote(motion): v = request.form.get("vote", "abstain") db = get_db() with db.xact(): - rv = db.prepare("SELECT type FROM motion WHERE id=$1")(motion); + rv = db.prepare("SELECT id, type FROM motion WHERE identifier=$1 AND host=$2")(motion, request.host); if len(rv) == 0: return "Error, Not found", 404 if not may("vote", rv[0].get("type")): return "Forbidden", 403 - p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE id = $1") - if not p(motion)[0][0]: + p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE identifier = $1 AND host=$2") + id = rv[0].get("id") + if not p(motion, request.host)[0][0]: return "Error, motion deadline has passed", 500 p = db.prepare("SELECT * FROM vote WHERE motion_id = $1 AND voter_id = $2") - rv = p(motion, g.voter) + rv = p(id, g.voter) if len(rv) == 0: - db.prepare("INSERT INTO vote(motion_id, voter_id, result) VALUES($1,$2,$3)")(motion, g.voter, v) + db.prepare("INSERT INTO vote(motion_id, voter_id, result) VALUES($1,$2,$3)")(id, g.voter, v) else: - db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(motion, g.voter, v) - return motion_edited(motion) + db.prepare("UPDATE vote SET result=$3, entered=CURRENT_TIMESTAMP WHERE motion_id=$1 AND voter_id = $2")(id, g.voter, v) + return motion_edited(id)