Handling request values with multiple values per key #86

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+18 −1
Split
View
@@ -138,9 +138,16 @@ def _generate_uri(self, limit, offset):
request_params = self.request_data.copy()
request_params.update({'limit': limit, 'offset': offset})
+
+ try:
+ # QueryDict has a urlencode method that can handle multiple values for the same key
+ encoded_params = request_params.urlencode()
+ except AttributeError:
+ encoded_params = urlencode(request_params)
+
return '%s?%s' % (
self.resource_uri,
- urlencode(request_params)
+ encoded_params
)
def page(self):
@@ -5,6 +5,7 @@
from core.models import Note
from core.tests.resources import NoteResource
from django.db import reset_queries
+from django.http import QueryDict
class PaginatorTestCase(TestCase):
@@ -142,3 +143,12 @@ def test_regression_nonqueryset(self):
# differently.
page = paginator.page()
self.assertEqual(page['objects'], ['foo', 'bar'])
+
+ def test_multiple(self):
+ request = QueryDict('a=1&a=2')
+ paginator = Paginator(request, self.data_set, resource_uri='/api/v1/notes/', limit=2, offset=2)
+ meta = paginator.page()['meta']
+ self.assertEqual(meta['limit'], 2)
+ self.assertEqual(meta['offset'], 2)
+ self.assertEqual(meta['previous'], '/api/v1/notes/?a=1&a=2&limit=2&offset=0')
+ self.assertEqual(meta['next'], '/api/v1/notes/?a=1&a=2&limit=2&offset=4')