Skip to content
Snippets Groups Projects
Commit 468cdac9 authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB
Browse files

updating pagination

parent 6033463e
No related branches found
No related merge requests found
from rest_framework import pagination
from rest_framework.response import Response
import urllib
from collections import OrderedDict
import logging
logger = logging.getLogger(__name__)
# TP (30 Apr 2018):
# This module has a bunch of hacked together pagination which is intended
# to match the existing "pagination" of the events API. We should definitely
# redo both APIs to do something reasonable and consistent when we have a
# chance.
def BasePaginationFactory(links_dict=True, results_name='results'):
class CustomBasePagination(pagination.PageNumberPagination):
......@@ -53,3 +60,37 @@ class CustomLogTagPagination(pagination.PageNumberPagination):
])
return Response(output)
class CustomSupereventPagination(pagination.LimitOffsetPagination):
default_limit = 3
limit_query_param = 'count'
offset_query_param = 'start'
def get_paginated_response(self, data):
numRows = self.count
# Get base URI
base_uri = self.request.build_absolute_uri(self.request.path)
# Construct custom link for "last" page
last = max(0, (numRows / self.limit)) * self.limit
param_dict = {
'start': last,
self.limit_query_param: self.limit,
}
last_uri = base_uri + '?' + urllib.urlencode(param_dict)
output = OrderedDict([
('numRows', numRows),
('superevents', data),
('links',
OrderedDict([
('self', self.request.build_absolute_uri()),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('first', base_uri),
('last', last_uri),
])),
])
return Response(output)
......@@ -24,7 +24,7 @@ from events.api.backends import LigoAuthentication
from .mixins import GetParentSupereventMixin
from .paginators import BasePaginationFactory, CustomLabelPagination, \
CustomLogTagPagination
CustomLogTagPagination, CustomSupereventPagination
from .serializers import SupereventSerializer, SupereventUpdateSerializer, \
SupereventEventSerializer, SupereventLabelSerializer, \
SupereventLogSerializer, SupereventLogTagSerializer, \
......@@ -44,7 +44,7 @@ class SupereventViewSet(viewsets.ModelViewSet):
"""
queryset = Superevent.objects.all()
serializer_class = SupereventSerializer
pagination_class = BasePaginationFactory(results_name='superevents')
pagination_class = CustomSupereventPagination
lookup_field = SUPEREVENT_LOOKUP_FIELD
lookup_value_regex = SUPEREVENT_LOOKUP_REGEX
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment