importdjango_filtersfromrest_framework.responseimportResponsefromrest_frameworkimportpaginationclassPaginationDjangoRestTable(pagination.LimitOffsetPagination):
final=0defget_current(self):
def_divide_with_ceil(a, b):
''' Returns 'a' divided by 'b', with any remainder rounded up. '''ifa%b:
return (a//b) +1returna//bfinal=Nonebase_url=self.request.build_absolute_uri()
ifself.limit:
current=_divide_with_ceil(self.offset, self.limit) +1# The number of pages is a little bit fiddly.# We need to sum both the number of pages from current offset to end# plus the number of pages up to the current offset.# When offset is not strictly divisible by the limit then we may# end up introducing an extra page as an artifact.final= (
_divide_with_ceil(self.count-self.offset, self.limit) +_divide_with_ceil(self.offset, self.limit)
)
iffinal<1:
final=1else:
current=1final=1ifcurrent>final:
current=finalself.final=finalreturncurrentdefget_paginated_response(self, data):
returnResponse({
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'count': self.count,
'limit' : self.get_limit(self.request),
'current' : self.get_current(),
'final' : self.final,
'results': data
})