Skip to content

Commit

Permalink
Merge 7603bb8 into 60902c8
Browse files Browse the repository at this point in the history
  • Loading branch information
Kibetchirchir committed Mar 22, 2019
2 parents 60902c8 + 7603bb8 commit bfd8b43
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
17 changes: 14 additions & 3 deletions authors/apps/article/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from .models import Article, ArticleComment
from ..profiles.serializers import ProfileSerializer
import readtime
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db.models import Avg
from rest_framework import serializers

from rest_framework import response

from authors.apps.authentication.serializers import UserSerializer
Expand Down Expand Up @@ -51,6 +49,7 @@ class ArticleSerializer(serializers.ModelSerializer):
likes_count = serializers.SerializerMethodField()
dislikes_count = serializers.SerializerMethodField()
tag_list = TagSerializer(default=[], required=False)
read_time = serializers.SerializerMethodField()

def get_rates(self, obj):
"""
Expand Down Expand Up @@ -128,6 +127,7 @@ class Meta:
'updated_at',
'slug',
'favourited',
'read_time',
'author',
'likes_count',
'dislikes_count',
Expand Down Expand Up @@ -198,6 +198,17 @@ def get_favourited(self, obj):
except ArticleFavourite.DoesNotExist:
return False

def get_read_time(self, obj):
"""
this method calculates the readtime of an article
:param obj: this is the article instance
:return: the time taken in minutes
"""
body = obj.body
result = readtime.of_text(body)
read_time = result.minutes
return str(read_time) + " minute(s)"


class RateSerializer(serializers.ModelSerializer):
"""
Expand Down
61 changes: 61 additions & 0 deletions authors/apps/article/tests/test_readtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json

from rest_framework.test import APITestCase, APIClient
from rest_framework.views import status


class TestArticleReadTime(APITestCase):
"""
Class tests for article .
"""
client = APIClient()

def setUp(self):
""" Creates user and user dictionary for testing."""
self.article = {
"title": "Andela",
"description": "sdsd",
"body": "dsd",
"images": ""
}
self.user = {
"user": {
"email": "chirchir@olympians.com",
"username": "chirchir",
"password": "test1234"
}
}
create_user = self.client.post(
'/api/users/', self.user, format='json')

self.request_tkn = self.client.post(
'/api/users/login/', self.user, format='json')

token_request = json.loads(self.request_tkn.content)
self.token = token_request["user"]["token"]

create_profile = self.client.post('/api/profile/create_profile/', self.user,
HTTP_AUTHORIZATION='Token ' + self.token,
format='json')

def test_view_readtime_one_article(self):
"""
test to see one article
:return:
"""
token_request = json.loads(self.request_tkn.content)
token = token_request["user"]["token"]
response = self.client.post('/api/articles/', self.article,
HTTP_AUTHORIZATION='Token ' + self.token,
format='json')
result = json.loads(response.content)
slug = result["article"]["slug"]

response_article = self.client.get('/api/articles/' + slug,
HTTP_AUTHORIZATION='Token ' + self.token,
format='json')
article = json.loads(response_article.content)

self.assertIn(
'read_time', str(result))
self.assertEqual(response_article.status_code, status.HTTP_200_OK)
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
astroid==2.1.0
atomicwrites==1.3.0
attrs==18.2.0
beautifulsoup4==4.7.1
certifi==2018.11.29
chardet==3.0.4
cloudinary==1.15.0
coreapi==2.3.3
coreschema==0.0.4
coverage==4.5.2
cssselect==1.0.3
defusedxml==0.5.0
dj-database-url==0.5.0
Django==2.1.7
Expand All @@ -28,6 +30,8 @@ isort==4.3.9
itypes==1.1.0
Jinja2==2.10
lazy-object-proxy==1.3.1
lxml==4.3.2
markdown2==2.3.7
MarkupSafe==1.1.1
mccabe==0.6.1
mock==2.0.0
Expand All @@ -41,11 +45,13 @@ psycopg2-binary==2.7.7
py==1.8.0
PyJWT==1.7.1
pylint==2.2.2
pyquery==1.4.0
pytest==4.3.0
python-http-client==3.1.0
python-social-auth==0.3.6
python3-openid==3.1.0
pytz==2018.9
readtime==1.1.0
requests==2.21.0
requests-oauthlib==1.2.0
ruamel.yaml==0.15.89
Expand All @@ -54,6 +60,7 @@ simplejson==3.16.0
six==1.12.0
social-auth-app-django==3.1.0
social-auth-core==3.1.0
soupsieve==1.8
uritemplate==3.0.0
urllib3==1.24.1
whitenoise==4.1.2
Expand Down

0 comments on commit bfd8b43

Please sign in to comment.