You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2015/03/06 13:35:10 UTC
[03/26] allura git commit: [#7832] ticket:731 Add limits to list API
[#7832] ticket:731 Add limits to list API
Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/37384e99
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/37384e99
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/37384e99
Branch: refs/heads/ib/7830
Commit: 37384e9966c8528249f12fa00a895c40cff28900
Parents: 804d86d
Author: Igor Bondarenko <je...@gmail.com>
Authored: Fri Feb 20 11:58:19 2015 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Fri Feb 27 22:40:51 2015 +0000
----------------------------------------------------------------------
Allura/allura/lib/repository.py | 13 ++++++++++++-
Allura/allura/model/webhook.py | 7 +++++++
Allura/allura/tests/test_webhooks.py | 6 +++++-
Allura/allura/webhooks.py | 5 ++---
4 files changed, 26 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/allura/blob/37384e99/Allura/allura/lib/repository.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/repository.py b/Allura/allura/lib/repository.py
index 41e671d..07b1b06 100644
--- a/Allura/allura/lib/repository.py
+++ b/Allura/allura/lib/repository.py
@@ -22,6 +22,7 @@ from pylons import tmpl_context as c, app_globals as g
from pylons import request
from tg import expose, redirect, flash, validate
from tg.decorators import with_trailing_slash, without_trailing_slash
+from webob import exc
from bson import ObjectId
from ming.utils import LazyProperty
@@ -297,4 +298,14 @@ class RestWebhooksLookup(BaseController):
'type': {'$in': [wh.type for wh in webhooks]},
'app_config_id': self.app.config._id}
).sort('_id', 1).all()
- return {'webhooks': [hook.__json__() for hook in configured_hooks]}
+ limits = {
+ wh.type: {
+ 'max': M.Webhook.max_hooks(wh.type, self.app.config.tool_name),
+ 'used': M.Webhook.query.find({
+ 'type': wh.type,
+ 'app_config_id': self.app.config._id,
+ }).count(),
+ } for wh in webhooks
+ }
+ return {'webhooks': [hook.__json__() for hook in configured_hooks],
+ 'limits': limits}
http://git-wip-us.apache.org/repos/asf/allura/blob/37384e99/Allura/allura/model/webhook.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/webhook.py b/Allura/allura/model/webhook.py
index 0d5140e..bb36e5f 100644
--- a/Allura/allura/model/webhook.py
+++ b/Allura/allura/model/webhook.py
@@ -16,6 +16,7 @@
# under the License.
import datetime as dt
+import json
from ming.odm import FieldProperty, session
from paste.deploy.converters import asint
@@ -55,6 +56,12 @@ class Webhook(Artifact):
self.last_sent = dt.datetime.utcnow()
session(self).flush(self)
+ @classmethod
+ def max_hooks(self, type, tool_name):
+ type = type.replace('-', '_')
+ limits = json.loads(config.get('webhook.%s.max_hooks' % type, '{}'))
+ return limits.get(tool_name.lower(), 3)
+
def __json__(self):
return {
'_id': unicode(self._id),
http://git-wip-us.apache.org/repos/asf/allura/blob/37384e99/Allura/allura/tests/test_webhooks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_webhooks.py b/Allura/allura/tests/test_webhooks.py
index d750d9d..c758df1 100644
--- a/Allura/allura/tests/test_webhooks.py
+++ b/Allura/allura/tests/test_webhooks.py
@@ -708,4 +708,8 @@ class TestWebhookRestController(TestRestApiBase):
'hook_url': 'http://httpbin.org/post/{}'.format(n),
'mod_date': unicode(wh.mod_date),
} for n, wh in enumerate(self.webhooks)]
- dd.assert_equal(r.json, {'webhooks': webhooks})
+ expected = {
+ 'webhooks': webhooks,
+ 'limits': {'repo-push': {'max': 3, 'used': 3}},
+ }
+ dd.assert_equal(r.json, expected)
http://git-wip-us.apache.org/repos/asf/allura/blob/37384e99/Allura/allura/webhooks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/webhooks.py b/Allura/allura/webhooks.py
index fb37820..2fa411f 100644
--- a/Allura/allura/webhooks.py
+++ b/Allura/allura/webhooks.py
@@ -313,13 +313,12 @@ class WebhookSender(object):
Checks if limit of webhooks created for given project/app is reached.
Returns False if limit is reached, True otherwise.
'''
- _type = self.type.replace('-', '_')
- limits = json.loads(config.get('webhook.%s.max_hooks' % _type, '{}'))
count = M.Webhook.query.find(dict(
app_config_id=app.config._id,
type=self.type,
)).count()
- return count < limits.get(app.config.tool_name.lower(), 3)
+ limit = M.Webhook.max_hooks(self.type, app.config.tool_name)
+ return count < limit
class RepoPushWebhookSender(WebhookSender):