Skip to content

Commit

Permalink
feat(articles-pagination): Modify articles get endpoint
Browse files Browse the repository at this point in the history
  - configure the default pagination class in settings file
  - write unit tests to approve the return statements for paginating are returned
  - that is the count, next and previous attributes
  - modify the articles get end point to return paginated data
[Delivers #161967024]
  • Loading branch information
fabzer0 committed Dec 13, 2018
1 parent fde9892 commit 8dd30a1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
3 changes: 3 additions & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ class Articles(models.Model):
def __str__(self):
""" String representation of db object """
return ' {}: {}'.format(self.id, self.slug)

class Meta:
ordering = ["-createdAt", "-updatedAt"]
20 changes: 11 additions & 9 deletions authors/apps/articles/tests/endpoints/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ def test_getArticle_status(self):
def test_getArticle_content(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articles')
response = self.client.get(url)

response.render()
self.assertIn(b"life_love_death", response.content)
self.assertIn(b"Life Love and Death", response.content)
self.assertIn(b"What is life?", response.content)
self.assertIn(b"This is the real life body.", response.content)
self.assertIn(b"[\"life\",\"love\",\"death\"]", response.content)
self.assertIn(b"4", response.content)
response = self.client.get(url).render()
self.assertIn(b"1", response.content) # count
self.assertIn(b"null", response.content) # next
self.assertIn(b"null", response.content) # previous
self.assertIn(b"1", response.content) # article id
self.assertIn(b"life_love_death", response.content) # slug
self.assertIn(b"Life Love and Death", response.content) # title
self.assertIn(b"What is life?", response.content) # description
self.assertIn(b"This is the real life body.", response.content) # body
self.assertIn(b"life,love,death", response.content) # taglist
self.assertIn(b"4", response.content) # favoritesCount

def test_get_specific_article(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
Expand Down
8 changes: 8 additions & 0 deletions authors/apps/articles/tests/endpoints/test_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def test_putArticle_status(self):
response.render()
self.assertIn(b'Update was successful', response.content)

def test_putArticle_no_token_provided(self):
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
response = self.client.put(url, self.data, format='json')
response.render()
self.assertIn(
b'Authentication credentials were not provided.',
response.content)

def test_get_specific_article(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
Expand Down
49 changes: 42 additions & 7 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
from rest_framework import views, status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.settings import api_settings

from .serializers import ArticleSerializer
from .models import Articles


class ArticleView(views.APIView):

permission_classes = (IsAuthenticated,)
serializer_class = ArticleSerializer
"""Posts and gets articles from the model"""
queryset = Articles.objects.all()
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS

"""
Posts and gets articles from the model
"""

def get(self, request):
"""The get request function"""
serializer = self.serializer_class
result = serializer.get_all_objects()
length = len(result)
return Response(
{"articles": result, "articlesCount": length}, status.HTTP_200_OK)

# Overriding to achieve pagination
page = self.paginate_queryset(self.queryset)
if page is not None:
serializer = self.serializer_class(page, many=True)
result = self.get_paginated_response(serializer.data)
return result

@property
def paginator(self):
# paginator instance associated with this view
if not hasattr(self, '_paginator'):
if self.pagination_class is None:
self._paginator = None
else:
self._paginator = self.pagination_class()
return self._paginator

def paginate_queryset(self, queryset):

# counters return of a single page of articles or None if disabled
if self.paginator is None:
return None
return self.paginator.paginate_queryset(
queryset,
self.request,
view=self)

def get_paginated_response(self, data):

# return paginated style for given output data
assert self.paginator is not None
return self.paginator.get_paginated_response(data)

@staticmethod
def post(request, **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@
# specifies a local custom authentication class
'authors.apps.authentication.backends.JWTAuthentication',
),

'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 3,

}

LOG_LEVEL = 'DEBUG'
Expand Down
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ DJANGO_SETTINGS_MODULE = authors.settings
python_files = test_*.py *migrations/*
# add pytest CLI commands to specify civerage and reuse of test db. This wil speed up tests
addopts = --nomigrations --cov=authors/apps/ -p no:warnings

0 comments on commit 8dd30a1

Please sign in to comment.