]> WPIA git - motion.git/commitdiff
add: close motion on request
authorINOPIAE <m.maengel@inopiae.de>
Wed, 16 Jan 2019 06:27:38 +0000 (07:27 +0100)
committerINOPIAE <m.maengel@inopiae.de>
Mon, 17 Aug 2020 17:05:12 +0000 (19:05 +0200)
README.md
motion.py
templates/single_motion.html
tests/test_motion.py

index 20331c340317316a72e65d7faeaa9eb24007ddd4..a3fd49372415f7de6c0e0c8701ee333534670666 100644 (file)
--- a/README.md
+++ b/README.md
@@ -33,6 +33,18 @@ python -m unittest tests/test_motion.py
 
 The database schema is automatically installed when the table "schema_version" does not exist and the application is started.
 
+The following user rights can be granted:
+- create: user is able to create a new motion
+- vote: user is able to vote running motions
+- cancel: user is able to cancel a running motion
+- finish: user is able to close a running motion
+- audit: user is able to see given votes of a finished motion
+
+To grant right use the following (here with vote right as example): 
+- on all groups add "vote:*"
+- on one given group add "vote:group1"
+- on two given groups add "vote:group1 vote:group2"
+
 # Usage
 
 Within the motion content markdown can be used for formatting e.g. 
index 0314550cecba464f6103280dd4b84c335ed3f554..1c83fc997a0460005b1947d9cb91e4b2831ba816 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -2,10 +2,12 @@ from flask import g
 from flask import Flask
 from flask import render_template, redirect
 from flask import request
+from functools import wraps
 import postgresql
 import filters
 from flaskext.markdown import Markdown
 from markdown.extensions import Extension
+from datetime import date, time, datetime
 
 def get_db():
     db = getattr(g, '_database', None)
@@ -63,7 +65,6 @@ def lookup_user():
         user = env.get("USER")
         roles = env.get("ROLES")
 
-
     if user is None:
         return "Server misconfigured", 500
     roles = roles.split(" ")
@@ -203,20 +204,38 @@ def put_motion():
 
 def motion_edited(motion):
     return rel_redirect("/?start=" + str(motion) + "#motion-" + str(motion))
+    
 
-@app.route("/motion/<string:motion>/cancel", methods=['POST'])
-def cancel_motion(motion):
-    rv = get_db().prepare("SELECT id, type FROM motion WHERE identifier=$1 AND host=$2")(motion, request.host);
+def validate_access(data):
+    rv = get_db().prepare("SELECT id, type, deadline, canceled FROM motion WHERE identifier=$1 AND host=$2")(data[0], data[1]);
     if len(rv) == 0:
         return "Error, Not found", 404
     id = rv[0].get("id")
-    if not may("cancel", rv[0].get("type")):
+    if not may(data[2], rv[0].get("type")):
         return "Forbidden", 403
+    if rv[0].get("deadline") < datetime.now() or rv[0].get("canceled") is not None:
+        return "Error, out of time", 403
+    return id
+
+    
+@app.route("/motion/<string:motion>/cancel", methods=['POST'])
+def cancel_motion(motion):
+    id = validate_access([motion, request.host, 'cancel'])
+    if not isinstance(id, int):
+        return id[0], id[1]
     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 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/<string:motion>/finish", methods=['POST'])
+def finish_motion(motion):
+    id = validate_access([motion, request.host, 'finish'])
+    if not isinstance(id, int):
+        return id[0], id[1]
+    rv = get_db().prepare("UPDATE motion SET deadline=CURRENT_TIMESTAMP WHERE identifier=$1 AND host=$2 AND canceled is NULL")(motion, request.host)
+    return motion_edited(id)
+
 @app.route("/motion/<string: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 "\
@@ -230,7 +249,7 @@ def show_motion(motion):
     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, singlemotion=True)
+    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")), may_finish=may("finish", rv[0].get("type")), votes=votes, singlemotion=True)
 
 @app.route("/motion/<string:motion>/vote", methods=['POST'])
 def vote(motion):
index c265432716be7e6a117309520008103f5cb8b710..8935636a76ded2a4a9af21b484b09509a0515b0d 100644 (file)
@@ -27,7 +27,12 @@ Motion: {{motion.name}}
 {%- if may_cancel %}
 <form action="/motion/{{motion.identifier}}/cancel" method="POST" class="form-inline">
 <input type="text" placeholder="cancelation reason" name="reason" class="form-control" required="yes">
-<button type="submit" class="btn btn-danger">Cancel</button>
+<button type="submit" class="btn btn-danger" name="cancel" value="cancel" id="cancel">Cancel</button></br>
+</form>
+{%- endif %}
+{%- if may_finish %}
+<form action="/motion/{{motion.identifier}}/finish" method="POST" class="form-inline">
+<button type="submit" class="btn btn-danger" name="finish" value="finish" id="finish">Finish</button></br>
 </form>
 {%- endif %}
 {%- endif %}
index c1f996c3312966439f5bb4a5572cd5637372d350..1577f3e2495623ce8f5f7bb06b90ee0e46bf3953 100644 (file)
@@ -48,6 +48,12 @@ class BasicTest(TestCase):
             data=dict(reason=reason)
         )
 
+    def finishMotion(self, user, motion):
+        return self.app.post(
+            '/motion/' + motion +'/finish',
+            environ_base={'USER_ROLES': user}
+        )
+
     def buildResultText(self, motiontext, yes, no, abstain):
         return '<p>'+motiontext+'</p></p>\n    <p>\nYes <span class=\"badge badge-pill badge-secondary\">'+str(yes)+'</span><br>'\
             + '\nNo <span class=\"badge badge-pill badge-secondary\">'+str(no)+'</span><br>'\
@@ -292,6 +298,12 @@ class VoterTests(BasicTest):
         self.assertEqual(response.status_code, 403)
         self.assertIn(str.encode('Forbidden'), response.data)
 
+    def test_finishMotion(self):
+        motion='g1.20200402.004'
+        response = self.finishMotion(user, motion)
+        self.assertEqual(response.status_code, 403)
+        self.assertIn(str.encode('Forbidden'), response.data)
+
     def test_see_old_vote(self):
         motion='g1.20200402.002'
         result = self.app.get('/motion/' + motion, environ_base={'USER_ROLES': user}, follow_redirects=True)
@@ -313,7 +325,7 @@ class CreateMotionTests(BasicTest):
     def setUp(self):
         self.init_test()
         global user
-        user='testuser/vote:* create:* cancel:*'
+        user='testuser/vote:* create:* cancel:* finish:*'
         self.db_clear()
 
     def tearDown(self):
@@ -428,18 +440,47 @@ class CreateMotionTests(BasicTest):
         self.assertEqual(response.status_code, 500)
         self.assertIn(str.encode('Error, form requires reason'), response.data)
 
-        reason='cancel test'
+        reason='cancel-test'
         response = self.cancelMotion(user, motion, reason)
         self.assertEqual(response.status_code, 302)
         result = self.app.get('/', environ_base={'USER_ROLES': user})
         self.assertIn(b'Cancelation reason: ' + str.encode(reason), result.data)
 
-        motion='g1.30190402.001'
+        motion='g1.20190402.001'
         reason="none"
         response = self.cancelMotion(user, motion, reason)
         self.assertEqual(response.status_code, 404)
         self.assertIn(str.encode('Error, Not found'), response.data)
 
+        motion='g1.30200402.001'
+        reason="cancel-test"
+        response = self.cancelMotion(user, motion, reason)
+        self.assertEqual(response.status_code, 404)
+        self.assertIn(str.encode('Error, Not found'), response.data)
+
+        motion='g1.20200402.004'
+        response = self.cancelMotion(user, motion, reason)
+        self.assertEqual(response.status_code, 403)
+        self.assertIn(str.encode('Error, out of time'), response.data)
+
+    def test_finishMotion(self):
+        self.db_sampledata()
+
+        motion='g1.20200402.004'
+        response = self.finishMotion(user, motion)
+        self.assertEqual(response.status_code, 302)
+        result = self.app.get('/', environ_base={'USER_ROLES': user})
+        self.assertIn(b'Motion D</span> (Finished)', result.data)
+
+        motion='g1.30190402.001'
+        response = self.finishMotion(user, motion)
+        self.assertEqual(response.status_code, 404)
+        self.assertIn(str.encode('Error, Not found'), response.data)
+        
+        motion='g1.20200402.001'
+        response = self.finishMotion(user, motion)
+        self.assertEqual(response.status_code, 403)
+        self.assertIn(str.encode('Error, out of time'), response.data)
 
 class AuditMotionTests(BasicTest):