Skip to content

Instantly share code, notes, and snippets.

@Cguilliman
Created November 15, 2019 07:42
Show Gist options
  • Save Cguilliman/fc341eca06b7599ea4b9ef27d983546d to your computer and use it in GitHub Desktop.
Save Cguilliman/fc341eca06b7599ea4b9ef27d983546d to your computer and use it in GitHub Desktop.
from typing import Dict, Type
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from ..paginator import PageNumberPagination
from .response_format import RestGetResponseMixin
__all__ = ('ListAPIViewPagination', )
class ListAPIViewPagination(RestGetResponseMixin, ListAPIView):
pagination_class: Type[PageNumberPagination]
is_paginate = True
def list(self, request, *args, **kwargs):
self.is_paginate = bool(request.GET.get('pagination', True))
if not self.is_paginate:
self.pagination_class = None
return super().list(request, *args, **kwargs)
def get_response_meta(self, response) -> Dict:
meta = super().get_response_meta(response)
if self.is_paginate:
meta['pagination'] = response.data.get('pagination', {})
return meta
def get_response_data(self, response) -> Dict:
if self.is_paginate:
return response.data.get('data')
return super().get_response_data(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment