allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [2/5] allura git commit: [#8082] don't query until you have to, avoids querying altogether if no limits in place or user is beyond any limits
Date Mon, 09 May 2016 19:13:10 GMT
[#8082] don't query until you have to, avoids querying altogether if no limits in place or
user is beyond any limits


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

Branch: refs/heads/master
Commit: dd0e7f994ed078f437c4344089ffa281f8e70a85
Parents: 1dcd816
Author: Dave Brondsema <dave@brondsema.net>
Authored: Fri Apr 29 17:21:21 2016 -0400
Committer: Dave Brondsema <dave@brondsema.net>
Committed: Mon May 2 14:23:57 2016 -0400

----------------------------------------------------------------------
 Allura/allura/lib/helpers.py    |  9 +++++++--
 Allura/allura/model/artifact.py | 26 ++++++++++++++------------
 2 files changed, 21 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/dd0e7f99/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index f54561e..c85e1e1 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -1281,6 +1281,8 @@ 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.
+
+    :param artifact_count: a number or callable (for lazy evaluation)
     """
     if exception is None:
         exception = exc.RatelimitError
@@ -1289,5 +1291,8 @@ def rate_limit(cfg_opt, artifact_count, start_date, exception=None):
     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()
+        if age < int(rate):
+            if callable(artifact_count):
+                artifact_count = artifact_count()
+            if artifact_count >= count:
+                raise exception()

http://git-wip-us.apache.org/repos/asf/allura/blob/dd0e7f99/Allura/allura/model/artifact.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/artifact.py b/Allura/allura/model/artifact.py
index e6b4b89..12eac75 100644
--- a/Allura/allura/model/artifact.py
+++ b/Allura/allura/model/artifact.py
@@ -438,7 +438,7 @@ class Artifact(MappedClass, SearchIndexable):
         """
         pkg = cls.__module__.split('.', 1)[0]
         opt = u'{}.rate_limits'.format(pkg)
-        count_in_app = cls.query.find(dict(app_config_id=app_config._id)).count()
+        count_in_app = lambda: cls.query.find(dict(app_config_id=app_config._id)).count()
         provider = plugin.ProjectRegistrationProvider.get()
         start = provider.registration_date(app_config.project)
         # need the replace because the generation_time is offset-aware UTC and h.rate_limit
uses offset-naive UTC dates
@@ -624,17 +624,19 @@ class VersionedArtifact(Artifact):
     @classmethod
     def is_limit_exceeded(cls, *args, **kwargs):
         if 'user' in kwargs:
-            # count distinct items, not total (e.g. many edits to a single wiki page doesn't
count against you)
-            HC = cls.__mongometa__.history_class
-            distinct_artifacts_by_user = HC.query.find({'author.id': kwargs['user']._id}).distinct('artifact_id')
-            """
-            # some useful debugging:
-            log.info(distinct_artifacts_by_user)
-            for art_id in distinct_artifacts_by_user:
-                art = cls.query.get(_id=art_id)
-                log.info('   ' + art.url())
-            """
-            kwargs['count_by_user'] = len(distinct_artifacts_by_user)
+            def distinct_artifacts_by_user():
+                # count distinct items, not total (e.g. many edits to a single wiki page
doesn't count against you)
+                HC = cls.__mongometa__.history_class
+                artifacts = HC.query.find({'author.id': kwargs['user']._id}).distinct('artifact_id')
+                """
+                # some useful debugging:
+                log.info(artifacts)
+                for art_id in artifacts:
+                    art = cls.query.get(_id=art_id)
+                    log.info('   ' + art.url())
+                """
+                return len(artifacts)
+            kwargs['count_by_user'] = distinct_artifacts_by_user
         return super(VersionedArtifact, cls).is_limit_exceeded(*args, **kwargs)
 
 


Mime
View raw message