Skip to content

Commit

Permalink
feat(pagination): display ten articles per page
Browse files Browse the repository at this point in the history
- Add a default pagination class

[Starts #162949227]
  • Loading branch information
kwanj-k committed Jan 25, 2019
1 parent afc3e87 commit 897d3fd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions authors/apps/core/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Custom pagination class """
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

class CustomPagination(PageNumberPagination):
def get_paginated_response(self, data):
return Response({
'pages': {
'next_page': self.get_next_link(),
'previous_page': self.get_previous_link()
},
'count': self.page.paginator.count,
'results': data
})
2 changes: 2 additions & 0 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
'DEFAULT_PAGINATION_CLASS': 'authors.apps.core.pagination.CustomPagination',
'PAGE_SIZE': 10
}

# Jwt configuration
Expand Down
18 changes: 18 additions & 0 deletions authors/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,25 @@ def setUp(self):
self.reset_password_invalid_email = {
"email": "kenyamoja@gmail.com"
}

def get_token(self):
"""Register and login a user"""

# register user
self.client.post(
self.user_url,
self.user_data,
format='json'
)
self.client.get(self.get_verify_url(self.user_data))
response = self.client.post(
self.login_url,
self.login_data,
format="json"
)
token = response.data['token']
return token

def get_verify_url(self, user):

self.register_user()
Expand Down
22 changes: 22 additions & 0 deletions authors/tests/test_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# python and django imports
from django.urls import reverse
from rest_framework import status

# local imports
from .base_test import TestBase


class TestPagination(TestBase):
"""This class has tests for pagination setting."""

def test_profiles_pagination(self):
token = self.get_token()
url = reverse('profiles:all_profiles')
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
response = self.client.get(url)
self.assertIn('count', response.data)
self.assertIn('results', response.data)
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertIn('next_page', response.data['pages'])
self.assertIn('previous_page', response.data['pages'])

0 comments on commit 897d3fd

Please sign in to comment.