Skip to content

Commit

Permalink
Pagination complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbrewerdev committed Aug 28, 2016
1 parent 0e0f76e commit f2bb395
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
6 changes: 4 additions & 2 deletions conduit/apps/articles/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

class ArticleJSONRenderer(ConduitJSONRenderer):
object_label = 'article'
object_label_plural = 'articles'
pagination_object_label = 'articles'
pagination_count_label = 'articlesCount'


class CommentJSONRenderer(ConduitJSONRenderer):
object_label = 'comment'
object_label_plural = 'comments'
pagination_object_label = 'comments'
pagination_count_label = 'commentsCount'
6 changes: 3 additions & 3 deletions conduit/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def create(self, request):

def list(self, request):
serializer_context = {'request': request}
serializer_instances = self.queryset.all()
page = self.paginate_queryset(self.queryset)

serializer = self.serializer_class(
serializer_instances,
page,
context=serializer_context,
many=True
)

return Response(serializer.data, status=status.HTTP_200_OK)
return self.get_paginated_response(serializer.data)

def retrieve(self, request, slug):
serializer_context = {'request': request}
Expand Down
2 changes: 2 additions & 0 deletions conduit/apps/authentication/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class UserJSONRenderer(ConduitJSONRenderer):
charset = 'utf-8'
object_label = 'user'
pagination_object_label = 'users'
pagination_count_label = 'usersCount'

def render(self, data, media_type=None, renderer_context=None):
# If we recieve a `token` key as part of the response, it will by a
Expand Down
30 changes: 12 additions & 18 deletions conduit/apps/core/renderers.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
import json

from rest_framework.renderers import JSONRenderer
from rest_framework.utils.serializer_helpers import ReturnList


class ConduitJSONRenderer(JSONRenderer):
charset = 'utf-8'
object_label = 'object'
object_label_plural = 'objects'
pagination_object_label = 'objects'
pagination_object_count = 'count'

def render(self, data, media_type=None, renderer_context=None):
if isinstance(data, ReturnList):
_data = json.loads(
super(ConduitJSONRenderer, self).render(data).decode('utf-8')
)

if data.get('results', None) is not None:
return json.dumps({
self.object_label_plural: _data
self.pagination_object_label: data['results'],
self.pagination_count_label: data['count']
})
else:
# If the view throws an error (such as the user can't be authenticated
# or something similar), `data` will contain an `errors` key. We want
# the default JSONRenderer to handle rendering errors, so we need to
# check for this case.
errors = data.get('errors', None)

if errors is not None:
# As mentioned about, we will let the default JSONRenderer handle
# rendering errors.
return super(ConduitJSONRenderer, self).render(data)
# If the view throws an error (such as the user can't be authenticated
# or something similar), `data` will contain an `errors` key. We want
# the default JSONRenderer to handle rendering errors, so we need to
# check for this case.
elif data.get('errors', None) is not None:
return super(ConduitJSONRenderer, self).render(data)

else:
return json.dumps({
self.object_label: data
})
2 changes: 2 additions & 0 deletions conduit/apps/profiles/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

class ProfileJSONRenderer(ConduitJSONRenderer):
object_label = 'profile'
pagination_object_label = 'profiles'
pagination_count_label = 'profilesCount'
2 changes: 2 additions & 0 deletions conduit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@
'DEFAULT_AUTHENTICATION_CLASSES': (
'conduit.apps.authentication.backends.JWTAuthentication',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 20,
}

0 comments on commit f2bb395

Please sign in to comment.