Skip to content

Commit

Permalink
feat: Implement user can see readtime of an article (#35)
Browse files Browse the repository at this point in the history
- write tests to calculate read time of an article
- add function tp calculate read time of an article in utils
- add readtime to article serializer
- move test data for readtime to test data file

[Starts #163383193]
  • Loading branch information
missvicki authored and malep2007 committed Feb 13, 2019
1 parent e953fed commit 5e030e5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
10 changes: 8 additions & 2 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import models
from ..profiles import serializers as ProfileSerializers
from .utils.utils import get_article_read_time


class ArticleSerializer (serializers.ModelSerializer):
Expand All @@ -14,6 +15,7 @@ class ArticleSerializer (serializers.ModelSerializer):
author = ProfileSerializers.ProfileSerializer(read_only=True)
favorited = serializers.SerializerMethodField()
average_ratings = serializers.IntegerField(required=False)
read_time = serializers.SerializerMethodField()

class Meta:
model = models.Article
Expand All @@ -32,15 +34,16 @@ class Meta:
"favoritesCount",
"favorited",
"average_ratings",
"tag_list"
"tag_list",
"read_time",
)
read_only_fields = (
'author',
'slug',
'created_at',
'updated_at'
'like_count',
'dislike_count'
'dislike_count',
)

def get_favorited(self, obj):
Expand All @@ -58,6 +61,9 @@ def get_favorited(self, obj):
"slug": {"read_only": True}
}

def get_read_time(self, obj):
return get_article_read_time(obj.body)


class ArticleRatingSerializer(serializers.ModelSerializer):
title = serializers.SerializerMethodField()
Expand Down
20 changes: 20 additions & 0 deletions authors/apps/articles/tests/test_article_readtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rest_framework import status
from authors.apps.articles.tests import base_class
from ..utils.utils import get_article_read_time
from .test_data import test_article_data


class TestArticleReadTime(base_class.BaseTest):
"""Class tests calculation of read time of an article"""

def test_calculate_readtime(self):
read_time = get_article_read_time(
test_article_data.one_min_read_text
)
self.assertIn("1 min read", read_time)

def test_calculate_readtime_empty_body(self):
read_time = get_article_read_time(
test_article_data.zero_min_read_text
)
self.assertIn("0 min read", read_time)
6 changes: 6 additions & 0 deletions authors/apps/articles/tests/test_data/test_article_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
}
}
un_existing_slug = 'allInvalidHomie-jk23ll'

one_min_read_text = "Hey there, how are you doing? \
Just wanted to let you know that Summer is almost here\
you should plan on getting your vacation in order. Thanks."

zero_min_read_text = ""
24 changes: 24 additions & 0 deletions authors/apps/articles/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import string
import readtime

from django.utils.text import slugify
from django.db.models import Avg
Expand All @@ -22,3 +23,26 @@ def get_average_rate(**kwargs):
return 0
else:
return int(average_ratings)


def get_article_read_time(body):
"""
Calculates the time some article takes the average human to read,
based on Medium’s read time formula.
Based on research, people are able to read English at 200 WPM
on paper, and 180 WPM on a monitor.
Read time is based on the average reading speed of an adult
(roughly 275 WPM). We take the total word count of a post
and translate it into minutes. Then, we add 12 seconds
for each inline image.
Parameters: Body of the article
Returns: average read time of an article
"""
if body:
result = readtime.of_text(body)
return str(result)
else:
return "0 min read"
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ six==1.12.0
uritemplate==3.0.0
urllib3==1.24.1
whitenoise==4.1.2
beautifulsoup4==4.7.1
cssselect==1.0.3
lxml==4.3.1
markdown2==2.3.7
pyquery==1.4.0
readtime==1.0.6
soupsieve==1.7.3

0 comments on commit 5e030e5

Please sign in to comment.