-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(article-readtime): Add article readtime feature (#32)
- Add calculate readtime functionality - Add interprete readtime functionality [Delivers #165273489]
- Loading branch information
1 parent
c44d44f
commit 6614de1
Showing
7 changed files
with
10,020 additions
and
3 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
authors/apps/articles/migrations/0005_article_read_time.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.1 on 2019-05-08 18:38 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('articles', '0004_auto_20190508_0526'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='article', | ||
name='read_time', | ||
field=models.TextField(default='less than a minute'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import re | ||
|
||
|
||
class ArticleTimeEngine: | ||
""" | ||
Class for estimating the time it takes to | ||
read an article | ||
""" | ||
def __init__(self, article_body): | ||
self.article_body = article_body | ||
|
||
def filter_words(self): | ||
""" | ||
Method filters out words from an article body | ||
""" | ||
filtered_words = re.findall(r'\w+', self.article_body) | ||
return filtered_words | ||
|
||
def total_word_count(self): | ||
""" | ||
Method returns total number of filtered words | ||
""" | ||
return len(self.filter_words()) | ||
|
||
def calculate_read_time(self): | ||
""" | ||
Method calculates minutes it takes to read an article | ||
""" | ||
# Average reading time for a normal person in words per minute | ||
WPM = 250 | ||
time = self.total_word_count()/WPM | ||
actual_time = round(time) | ||
return actual_time | ||
|
||
def read_time(self): | ||
# minutes in an hour | ||
hour = 60 | ||
# hours a day | ||
day = 24 | ||
# get calculated time | ||
time = self.calculate_read_time() | ||
# check if time is less than a minute | ||
if time < 1: | ||
return "less than a minute read" | ||
# check if time is greater than 1 hour | ||
if time >= hour: | ||
# convert minutes to hours | ||
hours = round(time/hour) | ||
# check if time is greater than a day | ||
if hours >= day: | ||
# convert hours to days | ||
days = round(hours/day) | ||
return f"{days} day read" | ||
return f"{hours} hour read" | ||
return f"{time} minute read" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9,884 changes: 9,884 additions & 0 deletions
9,884
authors/apps/articles/tests/readtime_test_data.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from django.urls import reverse | ||
from .base import BaseTestCase | ||
from .readtime_test_data import less_than_a_minute, one_hour_read,\ | ||
days_read, minutes_read | ||
|
||
|
||
class ArticleReadTimeTestCase(BaseTestCase): | ||
""" | ||
Class for testing the article read time | ||
""" | ||
|
||
def test_api_estimates_articles_of_less_than_a_minute(self): | ||
""" | ||
Method tests if the api returns readtime of less then a minute | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, less_than_a_minute, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual( | ||
response.data["article"]["read_time"], "less than a minute read") | ||
|
||
def test_api_estimates_minute_long_article_read_time(self): | ||
""" | ||
Method tests if the api returns readtime in minutes if the article | ||
takes minutes to read | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, minutes_read, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, 201) | ||
self.assertIn("minute read", str(response.data)) | ||
|
||
def test_api_estimates_hours_long_article_read_time(self): | ||
""" | ||
Method tests if the api returns readtime in hours if the article | ||
takes hours to read | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, one_hour_read, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, 201) | ||
self.assertIn("hour read", str(response.data)) | ||
|
||
def test_api_estimates_days_long_article_read_time(self): | ||
""" | ||
Method tests if api returns readtime in days if the article | ||
takes days to read | ||
""" | ||
url = reverse('articles-list-create') | ||
response = self.client.post( | ||
url, days_read, HTTP_AUTHORIZATION=self.auth_header, format="json") | ||
self.assertEqual(response.status_code, 201) | ||
self.assertIn("day read", str(response.data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters