diff options
| author | Barry Warsaw | 2010-02-25 21:07:27 -0500 |
|---|---|---|
| committer | Barry Warsaw | 2010-02-25 21:07:27 -0500 |
| commit | c231eb4a8c1bd593804a3a2f05f07966dcd73f18 (patch) | |
| tree | bcfbf3f0a66f1e55fffc3289aec7389ee9c79227 /src/mailman/rest/helpers.py | |
| parent | 70dd8a96cc0e01b42737b46e30cf37b5c8fa0747 (diff) | |
| download | mailman-c231eb4a8c1bd593804a3a2f05f07966dcd73f18.tar.gz mailman-c231eb4a8c1bd593804a3a2f05f07966dcd73f18.tar.zst mailman-c231eb4a8c1bd593804a3a2f05f07966dcd73f18.zip | |
Diffstat (limited to 'src/mailman/rest/helpers.py')
| -rw-r--r-- | src/mailman/rest/helpers.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py index fac4ae4f1..87614d1f0 100644 --- a/src/mailman/rest/helpers.py +++ b/src/mailman/rest/helpers.py @@ -21,6 +21,7 @@ from __future__ import absolute_import, unicode_literals __metaclass__ = type __all__ = [ + 'ContainerMixin', 'etag', 'path_to', ] @@ -71,3 +72,52 @@ def etag(resource): etag = hashlib.sha1(repr(resource)).hexdigest() resource['http_etag'] = '"{0}"'.format(etag) return json.dumps(resource) + + + +class CollectionMixin: + """Mixin class for common collection-ish things.""" + + def _resource_as_dict(self, resource): + """Return the dictionary representation of a resource. + + This must be implemented by subclasses. + + :param resource: The resource object. + :type resource: object + :return: The representation of the resource. + :rtype: dict + """ + raise NotImplementedError + + def _resource_as_json(self, resource): + """Return the JSON formatted representation of the resource.""" + return etag(self._resource_as_dict(resource)) + + def _get_collection(self, request): + """Return the collection as a concrete list. + + This must be implemented by subclasses. + + :param request: A restish request. + :return: The collection + :rtype: list + """ + raise NotImplementedError + + def _make_collection(self, request): + """Provide the collection to restish.""" + collection = self._get_collection(request) + if len(collection) == 0: + return dict(start=0, total_size=0) + else: + 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, + ) |
