Skip to content

Commit

Permalink
feat: Articles pagination (#36)
Browse files Browse the repository at this point in the history
- Create a paginators file
- Add ArticleLimitOffsetPagination class to paginators
- set default_limit per page for Articles
- Add pagination_class to Articles view class

[Starts #163383191]
  • Loading branch information
codjoero authored and malep2007 committed Feb 14, 2019
1 parent 62dd1bb commit 791c72f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 deletions.
10 changes: 10 additions & 0 deletions authors/apps/articles/paginators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework.pagination import LimitOffsetPagination


class ArticleLimitOffsetPagination(LimitOffsetPagination):
""" Class that will set article endpoint to only
only 10 articles per page
"""

default_limit = 10
offset_query_param = "page"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.urls import reverse

from rest_framework import status

from authors.apps.articles.tests import base_class
from ..test_data import test_article_data
from authors.apps.articles.models import Article


class TestArticleView(base_class.BaseTest):
"""
This Test class tests the api endpoint of get all articles
"""

def test_paginate_list_articles(self):
"""
This test method tests whether the endpoint returns paginated articles
"""
self.create_article_and_authenticate_test_user()
response = self.client.get(self.articles_url)
self.assertIn('count', response.data)
self.assertIn('results', response.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
34 changes: 17 additions & 17 deletions authors/apps/articles/tests/test_article_api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_retrieve_get_articles_if_there_is_no_article_in_db(self):
try getting articles when the db (Database) is empty
"""
response = self.client.get(self.articles_url)
self.assertEqual([], response.data)
self.assertEqual([], response.data['results'])
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_retrieve_all_articles_if_the_db_is_not_empty(self):
Expand All @@ -58,8 +58,8 @@ def test_retrieve_all_articles_if_the_db_is_not_empty(self):
"""
self.create_article_and_authenticate_test_user()
response = self.client.get(self.articles_url)
self.assertIn('title', response.data[0])
self.assertIn('slug', response.data[0])
self.assertIn('title', response.data['results'][0])
self.assertIn('slug', response.data['results'][0])
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_retrieve_single_article(self):
Expand Down Expand Up @@ -96,10 +96,10 @@ def test_update_an_article_authorized_user(self):
"""
self.create_article_and_authenticate_test_user()
article = Article.objects.all().first()
response = self.client.patch(self.article_url(article.slug),
data=test_article_data.
update_article_data,
format='json')
response = self.client.patch(
self.article_url(article.slug),
data=test_article_data.update_article_data,
format='json')
self.assertIn('title', response.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)

Expand All @@ -111,11 +111,11 @@ def test_update_an_article_unauthorized_user(self):
self.create_article_and_authenticate_test_user()
self.create_article_and_authenticate_test_user_2()
article = Article.objects.all().first()
response = self.client.patch(reverse('articles:article-details',
kwargs={'slug': article.slug}),
data=test_article_data.
update_article_data,
format='json')
response = self.client.patch(
reverse('articles:article-details',
kwargs={'slug': article.slug}),
data=test_article_data.update_article_data,
format='json')
expected_dict_reponse = {
'detail': 'This article does not belong to you. Access denied'}
self.assertDictEqual(expected_dict_reponse, response.data)
Expand All @@ -128,11 +128,11 @@ def test_update_unexisting_article_slug(self):
"""
self.create_article_and_authenticate_test_user()
article = Article.objects.all().first()
response = self.client.patch(reverse(
'articles:article-details',
kwargs={'slug':
test_article_data.
un_existing_slug}),
response = self.client.patch(
reverse('articles:article-details',
kwargs={'slug':
test_article_data.
un_existing_slug}),
data=test_article_data.update_article_data,
format='json')
expected_dict = {
Expand Down
2 changes: 1 addition & 1 deletion authors/apps/articles/tests/test_rate_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ def test_get_rate_average(self):
format='json')
self.assertEqual(response.status_code,
status.HTTP_200_OK)
self.assertEqual(response.data[0]['average_ratings'], 2)
self.assertEqual(response.data['results'][0]['average_ratings'], 2)
2 changes: 2 additions & 0 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..profiles import models as profile_model

from .models import Rating, Article
from .paginators import ArticleLimitOffsetPagination


class ArticlesApiView (generics.ListCreateAPIView):
Expand All @@ -23,6 +24,7 @@ class ArticlesApiView (generics.ListCreateAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
serializer_class = serializers.ArticleSerializer
renderer_classes = (ArticleJSONRenderer,)
pagination_class = ArticleLimitOffsetPagination

def post(self, request):
data = request.data
Expand Down
6 changes: 3 additions & 3 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,6 @@ def post(self, request, token):
serializer.is_valid(raise_exception=True)
new_password = data.get("new_password")
PasswordResetManager(request).update_password(email, new_password)
return Response(
{"message": "Your password has been reset"},
status=status.HTTP_200_OK)
return Response({
"message": "Your password has been reset"
}, status=status.HTTP_200_OK)
4 changes: 2 additions & 2 deletions authors/apps/core/password_reset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def prepare_password_reset_email(self, email):
)
self.context = {
'username': user.username,
'reset_link': self.password_reset_url + self.encoded_token.decode(
'utf-8') + '/'
'reset_link': self.password_reset_url +
self.encoded_token.decode('utf-8') + '/'
}
self.email_body = render_to_string(
'password_reset_email.txt', self.context)
Expand Down

0 comments on commit 791c72f

Please sign in to comment.