summaryrefslogtreecommitdiff
path: root/src/mailman/rest/helpers.py
diff options
context:
space:
mode:
authorAurélien Bompard2015-11-04 11:33:07 +0100
committerAurélien Bompard2015-11-04 11:33:07 +0100
commita5b6568179b9b17b2dd14b43bcccded15bf5025d (patch)
treec89851754cf87a1fefd0980f98613568d1f83589 /src/mailman/rest/helpers.py
parent8f8fd7f4544d6a0759aa27b971acc65ecf8c291a (diff)
downloadmailman-a5b6568179b9b17b2dd14b43bcccded15bf5025d.tar.gz
mailman-a5b6568179b9b17b2dd14b43bcccded15bf5025d.tar.zst
mailman-a5b6568179b9b17b2dd14b43bcccded15bf5025d.zip
Diffstat (limited to 'src/mailman/rest/helpers.py')
-rw-r--r--src/mailman/rest/helpers.py59
1 files changed, 25 insertions, 34 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py
index 0bba86521..a78f5b379 100644
--- a/src/mailman/rest/helpers.py
+++ b/src/mailman/rest/helpers.py
@@ -114,31 +114,6 @@ def etag(resource):
return json.dumps(resource, cls=ExtendedEncoder)
-def paginate(method):
- """Method decorator to paginate through collection result lists.
-
- Use this to return only a slice of a collection, specified in the request
- itself. The request should use query parameters `count` and `page` to
- specify the slice they want. The slice will start at index
- ``(page - 1) * count`` and end (exclusive) at ``(page * count)``.
-
- Decorated methods must take ``self`` and ``request`` as the first two
- arguments.
- """
- def wrapper(self, request, *args, **kwargs):
- # Allow falcon's HTTPBadRequest exceptions to percolate up. They'll
- # get turned into HTTP 400 errors.
- count = request.get_param_as_int('count', min=0)
- page = request.get_param_as_int('page', min=1)
- result = method(self, request, *args, **kwargs)
- if count is None and page is None:
- return result
- list_start = (page - 1) * count
- list_end = page * count
- return result[list_start:list_end]
- return wrapper
-
-
class CollectionMixin:
"""Mixin class for common collection-ish things."""
@@ -170,22 +145,38 @@ class CollectionMixin:
"""
raise NotImplementedError
+ def _paginate(self, request, collection):
+ """Method to paginate through collection result lists.
+
+ Use this to return only a slice of a collection, specified in the request
+ itself. The request should use query parameters `count` and `page` to
+ specify the slice they want. The slice will start at index
+ ``(page - 1) * count`` and end (exclusive) at ``(page * count)``.
+ """
+ # Allow falcon's HTTPBadRequest exceptions to percolate up. They'll
+ # get turned into HTTP 400 errors.
+ count = request.get_param_as_int('count', min=0)
+ page = request.get_param_as_int('page', min=1)
+ total_size = len(collection)
+ if count is None and page is None:
+ return 0, total_size, collection
+ list_start = (page - 1) * count
+ list_end = page * count
+ return list_start, total_size, collection[list_start:list_end]
+
def _make_collection(self, request):
"""Provide the collection to the REST layer."""
- collection = self._get_collection(request)
- if len(collection) == 0:
- return dict(start=0, total_size=0)
- else:
+ start, total_size, collection = self._paginate(
+ request, self._get_collection(request))
+ result = dict(start=start, total_size=total_size)
+ if len(collection) != 0:
entries = [self._resource_as_dict(resource)
for resource in collection]
# Tag the resources but use the dictionaries.
[etag(resource) for resource in entries]
# Create the collection resource
- return dict(
- start=0,
- total_size=len(collection),
- entries=entries,
- )
+ result['entries'] = entries
+ return result
def path_to(self, resource):
return path_to(resource, self.api_version)