Skip to content

Commit

Permalink
Merge c2e763b into 5c323ee
Browse files Browse the repository at this point in the history
  • Loading branch information
huxaiphaer committed Sep 13, 2018
2 parents 5c323ee + c2e763b commit e8f59f5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
10 changes: 5 additions & 5 deletions authors/apps/articles/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
:param renderer_context:
:return:
"""
if isinstance(data, list) and len(data) > 1:
val = data.get("results", None)
if val and isinstance(val, list) and len(val) > 1:
return json.dumps({
'articles': data
})

if isinstance(data, list) and len(data) is 1:
data = data[0]
if val and isinstance(val, list) and len(val) is 1:
val = val[0]

if isinstance(data, list) and len(data) < 1:
data = {}
data.update({"results": val})

errors = data.get('errors', None)

Expand Down
25 changes: 25 additions & 0 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Serializer classes for articles
"""
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination

from authors.apps.articles.exceptions import NotFoundException
from authors.apps.articles.models import Article
Expand Down Expand Up @@ -75,3 +76,27 @@ class behaviours
# noinspection SpellCheckingInspection
fields = ('slug', 'title', 'description', 'body', 'created_at',
'updated_at', 'favorited', 'favorites_count', 'photo_url', 'author')


class PaginatedArticleSerializer(PageNumberPagination):
"""
Pagination class
Inherits from PageNumberPagination
Paginates articles
"""
page_size = 4

def get_paginated_response(self, data):
"""
Formats response to include page links
:param data:
:return:
"""
return {
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'count': self.page.paginator.count,
'results': data
}
10 changes: 5 additions & 5 deletions authors/apps/articles/tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_list_specific_article(self):
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("slug")
slug = response.json().get("article").get("results").get("slug")

response = self.client.get(
"/api/articles/{0}/".format(slug), content_type='application/json')
Expand All @@ -137,7 +137,7 @@ def test_delete_specific_article(self):
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("slug")
slug = response.json().get("article").get("results").get("slug")

response = self.client.delete(
"/api/articles/{0}/".format(slug), content_type='application/json')
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_list_many_articles(self):
response = self.client.get(
"/api/articles/", content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIsInstance(response.json().get("articles"), list)
self.assertIsInstance(response.json().get("articles").get("results"), list)
self.assertEqual(len(response.json().get("articles")), 3)

def test_update_article(self):
Expand All @@ -183,7 +183,7 @@ def test_update_article(self):
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("slug")
slug = response.json().get("article").get("results").get("slug")

response = self.client.put(
"/api/articles/{0}/".format(slug), data=json.dumps(
Expand All @@ -206,7 +206,7 @@ def test_update_article_missing_data_and_not_exist(self):
self.assertIn('article', response.json())
self.assertIsInstance(response.json().get("article"), dict)

slug = response.json().get("article").get("slug")
slug = response.json().get("article").get("results").get("slug")

response = self.client.put(
"/api/articles/{0}/".format(slug), data=json.dumps(
Expand Down
6 changes: 5 additions & 1 deletion authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from authors.apps.articles.exceptions import NotFoundException, InvalidQueryParameterException
from authors.apps.articles.models import Article
from authors.apps.articles.renderer import ArticleJSONRenderer
from authors.apps.articles.serializers import ArticleSerializer
from authors.apps.articles.serializers import ArticleSerializer, PaginatedArticleSerializer


# noinspection PyUnusedLocal,PyMethodMayBeStatic
Expand Down Expand Up @@ -56,6 +56,10 @@ def to_int(val):
serializer = self.serializer_class(queryset, many=True)
data = serializer.data

pager_class = PaginatedArticleSerializer()
data = pager_class.paginate_queryset(data, request)
data = pager_class.get_paginated_response(data)

return Response(data)

def retrieve(self, request, slug=None):
Expand Down

0 comments on commit e8f59f5

Please sign in to comment.