allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [3/5] allura git commit: [#7886] ticket:789 Factor out rate_limit into general helper
Date Fri, 12 Jun 2015 19:38:24 GMT
[#7886] ticket:789 Factor out rate_limit into general helper


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/be93d3d3
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/be93d3d3
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/be93d3d3

Branch: refs/heads/master
Commit: be93d3d31a75d174e13953a7d7f620d1e53006db
Parents: 51fc93b
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Wed Jun 10 06:42:09 2015 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri Jun 12 18:44:25 2015 +0000

----------------------------------------------------------------------
 Allura/allura/lib/exceptions.py |  6 +++++-
 Allura/allura/lib/helpers.py    | 17 +++++++++++++++++
 Allura/allura/lib/plugin.py     | 18 ++++++------------
 3 files changed, 28 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/be93d3d3/Allura/allura/lib/exceptions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/exceptions.py b/Allura/allura/lib/exceptions.py
index f796d99..21dd1d2 100644
--- a/Allura/allura/lib/exceptions.py
+++ b/Allura/allura/lib/exceptions.py
@@ -39,7 +39,11 @@ class ProjectOverlimitError(ForgeError):
     pass
 
 
-class ProjectRatelimitError(ForgeError):
+class RatelimitError(ForgeError):
+    pass
+
+
+class ProjectRatelimitError(RatelimitError):
     pass
 
 

http://git-wip-us.apache.org/repos/asf/allura/blob/be93d3d3/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 396d7d6..1c562f0 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -1226,3 +1226,20 @@ def get_user_status(user):
         return 'disabled'
     elif pending:
         return 'pending'
+
+
+def rate_limit(cfg_opt, artifact_count, start_date, exception=None):
+    """
+    Check the various config-defined artifact creation rate limits, and if any
+    are exceeded, raise exception.
+    """
+    if exception is None:
+        exception = exc.RatelimitError
+    rate_limits = json.loads(tg.config.get(cfg_opt, '{}'))
+    now = datetime.utcnow()
+    for rate, count in rate_limits.items():
+        age = now - start_date
+        age = (age.microseconds +
+                (age.seconds + age.days * 24 * 3600) * 10 ** 6) / 10 ** 6
+        if age < int(rate) and artifact_count >= count:
+            raise exception()

http://git-wip-us.apache.org/repos/asf/allura/blob/be93d3d3/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 860292f..d66704e 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -44,7 +44,6 @@ import tg
 from tg import config, request, redirect, response
 from pylons import tmpl_context as c, app_globals as g
 from webob import exc
-from bson.tz_util import FixedOffset
 from paste.deploy.converters import asbool, asint
 
 from ming.utils import LazyProperty
@@ -696,18 +695,13 @@ class ProjectRegistrationProvider(object):
         """
         if security.has_access(neighborhood, 'admin', user=user)():
             return
-        # have to have the replace because, despite being UTC,
-        # the result from utcnow() is still offset-naive  :-(
-        # maybe look into making the mongo connection offset-naive?
-        now = datetime.utcnow().replace(tzinfo=FixedOffset(0, 'UTC'))
+        opt = 'project.rate_limits'
         project_count = len(list(user.my_projects()))
-        rate_limits = json.loads(config.get('project.rate_limits', '{}'))
-        for rate, count in rate_limits.items():
-            user_age = now - user._id.generation_time
-            user_age = (user_age.microseconds +
-                        (user_age.seconds + user_age.days * 24 * 3600) * 10 ** 6) / 10 **
6
-            if user_age < int(rate) and project_count >= count:
-                raise forge_exc.ProjectRatelimitError()
+        # have to have the replace because, the generation_time is offset-aware
+        # UTC and h.rate_limit uses offset-naive UTC dates
+        start_date = user._id.generation_time.replace(tzinfo=None)
+        e = forge_exc.ProjectRatelimitError
+        h.rate_limit(opt, project_count, start_date, exception=e)
 
     def phone_verified(self, user, neighborhood):
         """


Mime
View raw message