diff options
| -rw-r--r-- | src/mailman/rest/helpers.py | 39 | ||||
| -rw-r--r-- | src/mailman/rest/lists.py | 6 | ||||
| -rw-r--r-- | src/mailman/rest/members.py | 2 | ||||
| -rw-r--r-- | src/mailman/rest/tests/test_paginate.py | 14 | ||||
| -rw-r--r-- | src/mailman/rest/users.py | 2 |
5 files changed, 30 insertions, 33 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py index 7cacc2e6c..3b64dc0dc 100644 --- a/src/mailman/rest/helpers.py +++ b/src/mailman/rest/helpers.py @@ -107,7 +107,7 @@ def etag(resource): return json.dumps(resource, cls=ExtendedEncoder) -def paginate(default_count=None): +def paginate(method): """Method decorator to paginate through collection result lists. Use this to return only a slice of a collection, specified either @@ -119,34 +119,31 @@ def paginate(default_count=None): :type default_count: int :returns: Decorator function. """ - def dec(function): - def wrapper(*args, **kwargs): - # args[0] is self. - # restish Request object is expected as second arg. - request = args[1] - try: - count = int(request.GET['count']) - page = int(request.GET['page']) - # Wrong parameter types or no GET attribute in request object. - except (AttributeError, ValueError, TypeError): - return http.bad_request([], b'Invalid parameters') - # No count/page params: Use defaults. - except KeyError: - count = default_count - page = 1 + def wrapper(*args, **kwargs): + # args[0] is self. + # restish Request object is expected to be the second arg. + request = args[1] + # get the result + result = method(*args, **kwargs) + try: + count = int(request.GET['count']) + page = int(request.GET['page']) # Set indices list_start = 0 list_end = None - # get the result - result = function(*args, **kwargs) # slice list only if count is not None if count is not None: list_start = int((page - 1) * count) list_end = int(page * count) return result[list_start:list_end] - return result - return wrapper - return dec + # Wrong parameter types or no GET attribute in request object. + except (AttributeError, ValueError, TypeError): + return http.bad_request([], b'Invalid parameters') + # No count/page params + except KeyError: + pass + return result + return wrapper diff --git a/src/mailman/rest/lists.py b/src/mailman/rest/lists.py index 678891712..328472794 100644 --- a/src/mailman/rest/lists.py +++ b/src/mailman/rest/lists.py @@ -115,7 +115,7 @@ class _ListBase(resource.Resource, CollectionMixin): self_link=path_to('lists/{0}'.format(mlist.list_id)), ) - @paginate() + @paginate def _get_collection(self, request): """See `CollectionMixin`.""" return list(getUtility(IListManager)) @@ -230,7 +230,7 @@ class MembersOfList(MemberCollection): self._mlist = mailing_list self._role = role - @paginate() + @paginate def _get_collection(self, request): """See `CollectionMixin`.""" # Overrides _MemberBase._get_collection() because we only want to @@ -252,7 +252,7 @@ class ListsForDomain(_ListBase): resource = self._make_collection(request) return http.ok([], etag(resource)) - @paginate() + @paginate def _get_collection(self, request): """See `CollectionMixin`.""" return list(self._domain.mailing_lists) diff --git a/src/mailman/rest/members.py b/src/mailman/rest/members.py index 67400af31..bf2cdf82b 100644 --- a/src/mailman/rest/members.py +++ b/src/mailman/rest/members.py @@ -69,7 +69,7 @@ class _MemberBase(resource.Resource, CollectionMixin): delivery_mode=member.delivery_mode, ) - @paginate() + @paginate def _get_collection(self, request): """See `CollectionMixin`.""" return list(getUtility(ISubscriptionService)) diff --git a/src/mailman/rest/tests/test_paginate.py b/src/mailman/rest/tests/test_paginate.py index c6b387fe0..ae73125b5 100644 --- a/src/mailman/rest/tests/test_paginate.py +++ b/src/mailman/rest/tests/test_paginate.py @@ -59,7 +59,7 @@ class TestPaginateHelper(unittest.TestCase): def test_no_pagination(self): # No pagination params in request # Collection with 5 items. - @paginate() + @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] # Expect 5 items @@ -69,7 +69,7 @@ class TestPaginateHelper(unittest.TestCase): def test_valid_pagination_request_page_one(self): # ?count=2&page=1 is a valid GET query string. # Collection with 5 items. - @paginate() + @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] # Expect 2 items @@ -79,7 +79,7 @@ class TestPaginateHelper(unittest.TestCase): def test_valid_pagination_request_page_two(self): # ?count=2&page=2 is a valid GET query string. # Collection with 5 items. - @paginate() + @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] # Expect 2 items @@ -89,7 +89,7 @@ class TestPaginateHelper(unittest.TestCase): def test_2nd_index_larger_than_total(self): # ?count=2&page=3 is a valid GET query string. # Collection with 5 items. - @paginate() + @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] # Expect last item @@ -99,7 +99,7 @@ class TestPaginateHelper(unittest.TestCase): def test_out_of_range_returns_empty_list(self): # ?count=2&page=3 is a valid GET query string. # Collection with 5 items. - @paginate() + @paginate def get_collection(self, request): return ['one', 'two', 'three', 'four', 'five'] # Expect empty list @@ -108,7 +108,7 @@ class TestPaginateHelper(unittest.TestCase): def test_count_as_string_returns_bad_request(self): # ?count=two&page=2 are not valid values. - @paginate() + @paginate def get_collection(self, request): return [] # Expect Bad Request @@ -117,7 +117,7 @@ class TestPaginateHelper(unittest.TestCase): def test_no_get_attr_returns_bad_request(self): # ?count=two&page=2 are not valid values. - @paginate() + @paginate def get_collection(self, request): return [] request = FakeRequest() diff --git a/src/mailman/rest/users.py b/src/mailman/rest/users.py index 114451afd..21bcad202 100644 --- a/src/mailman/rest/users.py +++ b/src/mailman/rest/users.py @@ -86,7 +86,7 @@ class _UserBase(resource.Resource, CollectionMixin): resource['display_name'] = user.display_name return resource - @paginate() + @paginate def _get_collection(self, request): """See `CollectionMixin`.""" return list(getUtility(IUserManager).users) |
