Skip to content

Commit

Permalink
Added the collection_name option to Resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeecardona authored and toastdriven committed Feb 28, 2012
1 parent 656de4b commit 0fc8f9b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -36,6 +36,7 @@ Contributors:
* Ed Summers (edsu) for a setup.py patch.
* Sébastien Fievet (zyegfryed) for the initial OAuth implementation.
* Jacob Kaplan-Moss (jacobkm) for the PATCH patch.
* jorgeecardona for a patch in renaming the ``objects`` name of the response.


Thanks to Tav for providing validate_jsonp.py, placed in public domain.
Expand Down
6 changes: 6 additions & 0 deletions docs/resources.rst
Expand Up @@ -642,6 +642,12 @@ The inner ``Meta`` class allows for class-level configuration of how the
If ``True``, ``HttpAccepted`` (202) is returned on ``POST/PUT``
with a body containing all the data in a serialized form.

``collection_name``
------------~~~~~~~

Specifies the collection of objects returned in the ``GET`` list will be
named. Default is ``objects``.


Basic Filtering
===============
Expand Down
5 changes: 3 additions & 2 deletions tastypie/paginator.py
Expand Up @@ -15,7 +15,7 @@ class Paginator(object):
``total_count`` of resources seen and convenience links to the
``previous``/``next`` pages of data as available.
"""
def __init__(self, request_data, objects, resource_uri=None, limit=None, offset=0, max_limit=1000):
def __init__(self, request_data, objects, resource_uri=None, limit=None, offset=0, max_limit=1000, collection_name='objects'):
"""
Instantiates the ``Paginator`` and allows for some configuration.
Expand Down Expand Up @@ -43,6 +43,7 @@ def __init__(self, request_data, objects, resource_uri=None, limit=None, offset=
self.max_limit = max_limit
self.offset = offset
self.resource_uri = resource_uri
self.collection_name = collection_name

def get_limit(self):
"""
Expand Down Expand Up @@ -175,6 +176,6 @@ def page(self):
meta['next'] = self.get_next(limit, offset, count)

return {
'objects': objects,
self.collection_name: objects,
'meta': meta,
}
3 changes: 2 additions & 1 deletion tastypie/resources.py
Expand Up @@ -78,6 +78,7 @@ class ResourceOptions(object):
include_resource_uri = True
include_absolute_url = False
always_return_data = False
collection_name = 'objects'

def __new__(cls, meta=None):
overrides = {}
Expand Down Expand Up @@ -1040,7 +1041,7 @@ def get_list(self, request, **kwargs):
objects = self.obj_get_list(request=request, **self.remove_api_resource_names(kwargs))
sorted_objects = self.apply_sorting(objects, options=request.GET)

paginator = self._meta.paginator_class(request.GET, sorted_objects, resource_uri=self.get_resource_list_uri(), limit=self._meta.limit, max_limit=self._meta.max_limit)
paginator = self._meta.paginator_class(request.GET, sorted_objects, resource_uri=self.get_resource_list_uri(), limit=self._meta.limit, max_limit=self._meta.max_limit, collection_name=self._meta.collection_name)
to_be_serialized = paginator.page()

# Dehydrate the bundles in preparation for serialization.
Expand Down
10 changes: 10 additions & 0 deletions tests/core/tests/paginator.py
Expand Up @@ -166,3 +166,13 @@ def test_unicode_request(self):
self.assertEqual(meta['previous'], '/api/v1/notes/?slug__startswith=%E2%98%83&offset=0&limit=2&format=json')
self.assertEqual(meta['next'], u'/api/v1/notes/?slug__startswith=%E2%98%83&offset=4&limit=2&format=json')
self.assertEqual(meta['total_count'], 6)

def test_custom_collection_name(self):
paginator = Paginator({}, self.data_set, resource_uri='/api/v1/notes/', limit=20, offset=0, collection_name='notes')
meta = paginator.page()['meta']
self.assertEqual(meta['limit'], 20)
self.assertEqual(meta['offset'], 0)
self.assertEqual(meta['previous'], None)
self.assertEqual(meta['next'], None)
self.assertEqual(meta['total_count'], 6)
self.assertEqual(len(paginator.page()['notes']), 6)

0 comments on commit 0fc8f9b

Please sign in to comment.