2017-03-25 11:08:46 -06:00
|
|
|
"""
|
|
|
|
Custom pagination classes for REST framework
|
|
|
|
"""
|
|
|
|
from rest_framework.pagination import PageNumberPagination
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
|
|
|
|
|
|
class LinkedHeaderPageNumberPagination(PageNumberPagination):
|
|
|
|
"""
|
|
|
|
This overrides the default PageNumberPagination so that it only
|
|
|
|
returns the results as an array, not the pagination controls
|
|
|
|
(eg number of results, etc)
|
|
|
|
"""
|
2017-03-26 07:44:43 -06:00
|
|
|
page_size = 25
|
2017-03-25 11:08:46 -06:00
|
|
|
|
|
|
|
def get_paginated_response(self, data):
|
|
|
|
next_url = self.get_next_link()
|
|
|
|
previous_url = self.get_previous_link()
|
|
|
|
|
2021-05-22 07:54:20 -06:00
|
|
|
link = ''
|
2017-03-25 11:08:46 -06:00
|
|
|
if next_url is not None and previous_url is not None:
|
|
|
|
link = '<{next_url}>; rel="next", <{previous_url}>; rel="prev"'
|
|
|
|
elif next_url is not None:
|
|
|
|
link = '<{next_url}>; rel="next"'
|
|
|
|
elif previous_url is not None:
|
|
|
|
link = '<{previous_url}>; rel="prev"'
|
|
|
|
link = link.format(next_url=next_url, previous_url=previous_url)
|
|
|
|
headers = {'Link': link} if link else {}
|
|
|
|
return Response(data, headers=headers)
|