37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
# Copyright © 2017 Tom Hacohen
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, version 3.
|
|
#
|
|
# This library is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from rest_framework import pagination
|
|
from rest_framework.response import Response
|
|
|
|
|
|
class LinkHeaderPagination(pagination.LimitOffsetPagination):
|
|
def get_paginated_response(self, data):
|
|
next_url = self.get_next_link()
|
|
previous_url = self.get_previous_link()
|
|
|
|
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"'
|
|
else:
|
|
link = ''
|
|
|
|
link = link.format(next_url=next_url, previous_url=previous_url)
|
|
headers = {'Link': link} if link else {}
|
|
|
|
return Response(data, headers=headers)
|