From: Felix Dörre Date: Tue, 5 Dec 2017 17:17:24 +0000 (+0100) Subject: use motion identifiers X-Git-Url: https://code.wpia.club/?p=motion.git;a=commitdiff_plain;h=224b4a836fd7588b653baf91b004a7ae62ec49c9 use motion identifiers --- diff --git a/motion.py b/motion.py index 741eb30..08dd458 100644 --- a/motion.py +++ b/motion.py @@ -21,6 +21,7 @@ app.register_blueprint(filters.blueprint) app.config.from_pyfile('config.py') groups=["fellowship", "board"] +prefix={"fellowship": "f", "board": "m"} @app.before_request def lookup_user(): @@ -83,10 +84,31 @@ def init_db(): 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()) + if ver < 2: + with app.open_resource('sql/from_1.sql', mode='r') as f: + db.execute(f.read()) + ct={} + for g in groups: + 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[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)")() + init_db() @app.route("/") @@ -127,55 +149,67 @@ def put_motion(): time = int(request.form.get("days", "3")); if time not in times: 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) + 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 DATE(\"posed\")=DATE(CURRENT_TIMESTAMP)") + sr = s(cat) + ident="" + if len(sr) == 0 or sr[0][0] is None: + ident=prefix[cat]+"."+t.strftime("%Y%m%d")+".001" + else: + ident=prefix[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\") VALUES($1, $2, CURRENT_TIMESTAMP + $3 * interval '1 days', $4, $5, $6)") + p(request.form.get("title", ""), request.form.get("content",""), time, g.voter, cat, ident) return rel_redirect("/") def motion_edited(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")(motion); 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 canceled is NULL")(request.form.get("reason", ""), g.voter, motion) + 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") + + "WHERE motion.identifier=$1") rv = p(motion, g.voter) 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")(motion); + 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']) +@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")(motion); 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") + p = db.prepare("SELECT deadline > CURRENT_TIMESTAMP FROM motion WHERE identifier = $1") + id = rv[0].get("id") if not p(motion)[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) diff --git a/sql/from_1.sql b/sql/from_1.sql new file mode 100644 index 0000000..348e6af --- /dev/null +++ b/sql/from_1.sql @@ -0,0 +1 @@ +ALTER TABLE "motion" ADD COLUMN "identifier" VARCHAR(20) DEFAULT NULL; diff --git a/sql/schema.sql b/sql/schema.sql index 116e76f..290b9b3 100644 --- a/sql/schema.sql +++ b/sql/schema.sql @@ -4,6 +4,7 @@ CREATE TABLE voter (id serial NOT NULL, email VARCHAR(255) NOT NULL, PRIMARY KEY DROP TABLE IF EXISTS motion; CREATE TABLE motion (id serial NOT NULL, + identifier VARCHAR(20) NOT NULL, name VARCHAR(250) NOT NULL, type VARCHAR(250) NOT NULL, content text NOT NULL, @@ -14,7 +15,7 @@ CREATE TABLE motion (id serial NOT NULL, cancelation_reason text NULL DEFAULT NULL, canceled_by int NULL DEFAULT NULL, PRIMARY KEY(id)); - +CREATE UNIQUE INDEX motion_ident ON motion (identifier); DROP TABLE IF EXISTS vote; DROP TYPE IF EXISTS "vote_type"; diff --git a/templates/motion.html b/templates/motion.html index 1c228f3..bcead1f 100644 --- a/templates/motion.html +++ b/templates/motion.html @@ -5,9 +5,9 @@ {%- else %} panel-default{%- endif -%} " id="motion-{{motion.id}}">
- # {{motion.name}} ({{ 'Running' if motion.running else ('Canceled' if motion.canceled != None else 'Finished') }}) {{motion.type}} +
Posed: {{motion.posed|timestamp}} by {{motion.poser}}
{%- if motion.canceled != None %} diff --git a/templates/single_motion.html b/templates/single_motion.html index a15e397..585e117 100644 --- a/templates/single_motion.html +++ b/templates/single_motion.html @@ -18,14 +18,14 @@ Motion: {{motion.name}} {%- endif %} {%- if motion.running %} {%- if may_vote %} -
+ {%- for vote in ['yes','no','abstain'] %} {%- endfor %}
{%- endif %} {%- if may_cancel %} -
+