]> WPIA git - motion.git/commitdiff
add: configuration of timespan between entering of two motions
authorINOPIAE <m.maengel@inopiae.de>
Tue, 22 Dec 2020 20:25:26 +0000 (21:25 +0100)
committerINOPIAE <m.maengel@inopiae.de>
Sun, 3 Jan 2021 03:50:11 +0000 (04:50 +0100)
Change-Id: I255f871095479d12a535595647cf8ae6a693f02e

config.py.example
motion.py
tests/test_basics.py
tests/test_motion.py

index 56ce4bc6c275e21445c0f4bb3b31d1e5f8e83458..e56352d876f4cbbea6174887060cb02cb7cb0ad6 100644 (file)
@@ -15,6 +15,8 @@ GROUP_PREFIX={'hostname': {'group1': 'g1', 'group2': 'g2'}} # motion types and t
 
 MAX_PROXY=2  # user is allowed to hold up to MAX_PROXY votes
 
+MOTION_WAIT_MINUTES={'hostname':'1'}  # timespan between the entering of two motions
+
 DURATION={'hostname':[3, 7, 14]} # duration period for motions
 
 #DEBUGUSER={'hostname':'username/create:* vote:*'} # remove # at beginning of line to use local debuguser
index 99daaf68d46d252feb0e4f321041b98704f1543d..21e3e1605a39e9aa70c70e63613208a1acc47aa9 100644 (file)
--- a/motion.py
+++ b/motion.py
@@ -61,6 +61,7 @@ class ConfigProxy:
 prefix = ConfigProxy("GROUP_PREFIX")
 times = ConfigProxy("DURATION")
 debuguser = ConfigProxy("DEBUGUSER")
+motion_wait_minutes = ConfigProxy("MOTION_WAIT_MINUTES")
 
 max_proxy=app.config.get("MAX_PROXY")
 
@@ -262,6 +263,17 @@ def init_db():
 
 init_db()
 
+def is_in_ratelimit(group):
+    rv = get_db().prepare("SELECT EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - posed)) AS timedifference FROM motion WHERE type=$1 AND host=$2 ORDER BY posed DESC LIMIT 1")(group, request.host)
+    if len(rv) == 0:
+        return True
+    rate_limit = motion_wait_minutes.per_host
+    if rate_limit is None:
+        rate_limit = 0
+    if rv[0]['timedifference'] > rate_limit*60:
+        return True
+    else:
+        return _('Error, time between last motion to short. The current setting is %s minute(s).') % (str(rate_limit))
 
 @app.route("/")
 def main():
@@ -309,6 +321,9 @@ def put_motion():
     content=content.strip()
     if content =='':
         return _('Error, missing content'), 400
+    ratelimit = is_in_ratelimit(cat)
+    if ratelimit is not True:
+        return ratelimit, 400
 
     db = get_db()
     with db.xact():
index f886c34d708a77e08b0245c5ac3988c1a6727c07..b6f111cb6660c49a4dae7d136ac6911574b501b2 100644 (file)
@@ -17,7 +17,6 @@ app.config.update(
 app.config['TESTING'] = True
 app.config['DEBUG'] = False
 
-
 class BasicTest(TestCase):
 
     def init_test(self):
index 769deb131896a029710f670bad96e0dcdcd80cca..7ef5ceac11c309943e8fdb0e910d999c8e051bef 100644 (file)
@@ -1,6 +1,5 @@
 from datetime import datetime
 from tests.test_basics import BasicTest
-import postgresql
 from motion import app
 
 # no specific rights required
@@ -479,6 +478,24 @@ class CreateMotionTests(BasicTest):
         self.assertEqual(response.status_code, 403)
         self.assertIn(str.encode('Error, out of time'), response.data)
 
+    def test_createMotionWait(self):
+        # test no limit given
+        self.db_sampledata()
+        title='My Motion'
+        content='My body'
+        response = self.createMotion(user, title, content, '3', 'group1')
+        self.assertEqual(response.status_code, 302)
+
+        # test different host
+        app.config.update(MOTION_WAIT_MINUTES={'127.0.0.1:5001':1})
+        response = self.createMotion(user, title, content, '3', 'group1')
+        self.assertEqual(response.status_code, 302)
+
+        # test 3 minutes
+        app.config.update(MOTION_WAIT_MINUTES={'127.0.0.1:5000':3})
+        response = self.createMotion(user, title, content, '3', 'group1')
+        self.assertIn(str.encode('Error, time between last motion to short. The current setting is 3 minute(s).'), response.data)
+
 class AuditMotionTests(BasicTest):
 
     def setUp(self):