Skip to content

Commit

Permalink
feat(reading time): time taken to read an article
Browse files Browse the repository at this point in the history
- add time taken to read an article
- add tests for time taken

[#Delivers 161254677]
  • Loading branch information
wrotich committed Nov 14, 2018
1 parent db6e646 commit be050df
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
24 changes: 23 additions & 1 deletion authors/apps/article/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
to be converted to
native Python datatypes that can then
be easily rendered into JSON, XML or other content types.'''
import math

from rest_framework import serializers
from django.apps import apps
Expand Down Expand Up @@ -106,14 +107,35 @@ def update(self, instance, validated_data):


class ArticleCreateSerializer(serializers.ModelSerializer):
time_to_read = serializers.CharField(required=False)
images = serializers.ListField(child=serializers.CharField(
max_length=1000), min_length=None, max_length=None, required=False)

class Meta:
model = TABLE

fields = fields
fields = fields + ('images', 'time_to_read',)

def get_time_to_read(self, body, images):
"""
Calculates the time it takes to read a given article
average reading time for plain text = 200wpm
average reading time for images in articles = 15secs =0.25mins
"""

image_view_time = 0
if images:
image_view_time = (len(images) * 0.25)
time_taken = math.ceil((len(list(body)) / 250) + image_view_time)
if time_taken <= 1:
return str(time_taken) + 'min'
return str(time_taken) + 'mins'

def create(self, validated_data):
instance = TABLE.objects.create(**validated_data)
validated_data['slug'] = instance.slug
validated_data['time_to_read'] = self.get_time_to_read(
instance.body, validated_data.get('images'),)

return validated_data

Expand Down
17 changes: 17 additions & 0 deletions authors/apps/article/tests/test_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,23 @@ def setUp(self):
self.user.token())

self.namespace = 'article'
text = "Will it rain, Will it rain today, " \
"Will it rain today Will it rain today " \
"Will it rain today Will it rain today, " \
"Will it rain today Will it rain today " \
"Will it rain today Will it rain today " \
"Will it rain today Will it rain today," \
"Will it rain today Will it rain today Will it rain "
self.body = {
'title': faker.name(),
'description': faker.text(),
'body': faker.text(),
}
self.article_body = {
"title": "Test reading time 300 ",
"description": "Is a new day again?",
"body": text
}
self.create_url = reverse(self.namespace + ':create')
self.list_url = reverse(self.namespace + ':list')
self.update_url = reverse(
Expand All @@ -65,7 +77,12 @@ def setUp(self):

def test_create_article_api(self):
response = self.client.post(self.create_url, self.body, format='json')
response1 = self.client.post(
self.create_url,
self.article_body,
format='json')
self.assertEqual(201, response.status_code)
self.assertIn('2mins', str(response1.data))

def test_retrieve_article_api(self):
response = self.client.get(self.retrieve_url)
Expand Down
47 changes: 47 additions & 0 deletions authors/apps/article/tests/test_time_to_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.urls import reverse
from rest_framework.test import APITestCase, APIClient


class TestArticleComments(APITestCase):
def setUp(self):
"""
Prepare test environment for each testcase
"""
self.client = APIClient()
self.author_details = {
'user': {
'username': 'admin123',
'email': 'admin@admin.com',
'password': 'admin2018',
}
}
self.login_data = {
"user": {
'email': 'admin@admin.com',
'password': 'admin2018',
}
}

self.new_article = {
"title": "How to train your dragon",
"description": "Ever wonder how?",
"body": "You have to believe"
}

self.login_url = reverse('authentication:login')
self.signup_url = reverse('authentication:register')
self.articles_url = reverse('article:create')

self.client.post(
self.signup_url,
self.author_details,
format='json')

def login(self):
"""login user to get the token"""
response = self.client.post(
self.login_url, self.login_data, format='json')
return response.data.get('token')

def test_time_to_read(self):
"""This method tests the time taken to read an article"""

0 comments on commit be050df

Please sign in to comment.