diff --git a/gracedb/superevents/api/paginators.py b/gracedb/superevents/api/paginators.py index ff55859fb04deac07396755f8f60912c1b5e93a3..c2dd316b79babbc880012f16763fbb4b07980d68 100644 --- a/gracedb/superevents/api/paginators.py +++ b/gracedb/superevents/api/paginators.py @@ -1,11 +1,18 @@ 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) diff --git a/gracedb/superevents/api/views.py b/gracedb/superevents/api/views.py index 0fb990030d264f50791d4c79048481d6abafc7e3..8465abcffa34eea40e8ea793126407216f71c5cf 100644 --- a/gracedb/superevents/api/views.py +++ b/gracedb/superevents/api/views.py @@ -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