Commit 80ad7573 authored by Jean-Baptiste's avatar Jean-Baptiste

update: use Link Headers for pagination

parent 4849b1e3
Pipeline #4824 passed with stage
in 1 minute and 21 seconds
......@@ -233,7 +233,7 @@ To enable pagination feature just add this configuration to the server `settings
```
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'DEFAULT_PAGINATION_CLASS': 'djangoldp.pagination.LDPPagination',
'PAGE_SIZE': 20
}
```
......
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response
class LDPPagination(LimitOffsetPagination):
def get_paginated_response(self, data):
next_url = self.get_next_link()
previous_url = self.get_previous_link()
links = []
for url, label in ((previous_url, 'prev'), (next_url, 'next')):
if url is not None:
links.append('<{}>; rel="{}"'.format(url, label))
headers = {'Link': ', '.join(links)} if links else {}
return Response(data, headers=headers)
......@@ -49,6 +49,10 @@ settings.configure(DEBUG=False,
'djangoldp.tests',
),
SITE_URL='http://happy-dev.fr',
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'djangoldp.pagination.LDPPagination',
'PAGE_SIZE': 5
},
)
django.setup()
......@@ -66,6 +70,7 @@ failures = test_runner.run_tests([
'djangoldp.tests.tests_get',
'djangoldp.tests.tests_delete',
'djangoldp.tests.tests_sources',
'djangoldp.tests.tests_pagination',
# 'djangoldp.tests.tests_temp'
])
......
from rest_framework.test import APIRequestFactory, APIClient, APITestCase
from djangoldp.tests.models import Post
class TestPagination(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.client = APIClient()
for i in range(0, 10):
Post.objects.create(content="content {}".format(i))
def tearDown(self):
pass
def test_next(self):
response = self.client.get('/posts/', content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
self.assertIn('link', response._headers)
self.assertEquals(response._headers['link'][1], '<http://testserver/posts/?limit=5&offset=5>; rel="next"')
def test_previous(self):
response = self.client.get('/posts/?offset=2&limit=2', content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
self.assertIn('link', response._headers)
self.assertEquals(response._headers['link'][1],
'<http://testserver/posts/?limit=2>; rel="prev", <http://testserver/posts/?limit=2&offset=4>; rel="next"')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment