Skip to content

Commit

Permalink
Merge efdbee5 into bbe35e9
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasulejoseph committed Dec 19, 2018
2 parents bbe35e9 + efdbee5 commit 0419d37
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 253 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,3 @@ db.sqlite3
.vscode/
.DS_Store
*.swp
migrations/
78 changes: 0 additions & 78 deletions authors/apps/articles/migrations/0001_initial.py

This file was deleted.

64 changes: 0 additions & 64 deletions authors/apps/articles/migrations/0002_auto_20181218_0735.py

This file was deleted.

18 changes: 0 additions & 18 deletions authors/apps/articles/migrations/0003_article_readtime.py

This file was deleted.

16 changes: 12 additions & 4 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ class Article(models.Model):
this field shows how many likes an article has
"""
likes_count = models.IntegerField(default=0)
"""
this field shows the readtime of an article
"""
readTime = models.IntegerField(default=0)
read_time = models.IntegerField(default=0)
view_count = models.IntegerField(default=0)
read_count = models.IntegerField(default=0)

objects = models.Manager()

Expand Down Expand Up @@ -149,3 +148,12 @@ class ComentLikes(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)

like = models.BooleanField()
class Readings(models.Model):
""" model for reading stats """
author = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
read_count = models.IntegerField(default=0)

def __str__(self):
return "article_id: {}, author: {}, views: {}".format(
self.article, self.author, self.read_count)
29 changes: 23 additions & 6 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
Article,
ArticleImg,
Tag,
Favourites, Likes
Favourites, Likes,
Readings
)


Expand All @@ -28,11 +29,9 @@ class Meta:
List all of the fields that could possibly be included in a request
or response, this includes fields specified explicitly above.
"""
fields = [
'title', 'body', 'description', 'tagList',
'author', 'slug', 'published', 'created_at', 'updated_at',
'favourited','favouriteCount','readTime',
]
fields = ['title', 'body', 'description', 'tagList',
'author', 'slug', 'published', 'created_at', 'favourited','favouriteCount',
'updated_at', 'read_time', 'view_count', 'likes_count', 'read_count' ]

def create(self, validated_data):
tags = validated_data.pop('tags', [])
Expand Down Expand Up @@ -111,3 +110,21 @@ class LikeArticleViewSerializer(serializers.ModelSerializer):
class Meta:
model = Likes
fields = ['id', 'article', 'profile', 'like']

class ReadingSerializer(serializers.ModelSerializer):
read_time = serializers.IntegerField(read_only=True)
likes_count = serializers.IntegerField(read_only=True)
view_count = serializers.IntegerField(read_only=True)

def to_representation(self, instance):
response = super().to_representation(instance)
article = Article.objects.all().filter().values()[0]
response['read_count'] = article['read_count']
response['read_time'] = article['read_time']
response['view_count'] = article['view_count']
response['likes_count'] = article['likes_count']
response['article'] = article['title']
return response
class Meta:
model = Readings
fields = ['read_time', 'article', 'likes_count', 'view_count', 'read_count']
59 changes: 59 additions & 0 deletions authors/apps/articles/test/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.test import TestCase

import json
from ..test.base import BaseTestArticles

class TestStats(BaseTestArticles):
def test_read_statistics_updated_successfully(self):
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + self.login_user())
response = self.client.post(
'/api/articles/', data=self.article, format='json')
res = self.client.post(
'/api/articles/{}/3'.format(self.create_article()),
format='json')
self.assertTrue(res.status_code, 200)
self.assertEqual(1, res.data['read_count'])

def test_user_count_for_a_read_is_only_one(self):
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + self.login_user())
res = self.client.post(
'/api/articles/', data=self.article, format='json')
slug = res.data['slug']
self.client.post(
'/api/articles/{}/3'.format(slug),
format='json')
resp = self.client.post(
'/api/articles/{}/3'.format(slug),
format='json')
self.assertTrue(resp.status_code, 200)
self.assertEqual(1, resp.data['read_count'])

def test_a_read_cannot_be_recorded_when_user_hasnot_read_the_article(self):
data = {

"article": {
"title": "How to train your dragon added on the titlt",
"description": "Ever wonder how?",
"body": "You have to believe this body has beeb updated " * 100,
"tagList": ["Rails", "Golang", "magic!"],
"images": [
{
"image_url": "https://imgur.comhenry/",
"description": "image is cool"
}
]
}
}
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + self.login_user())
resp = self.client.post(
'/api/articles/', data=data, format='json')
slug = resp.data['slug']
res = self.client.post(
'/api/articles/{}/2'.format(slug),
format='json')
self.assertTrue(res.status_code, 301)
self.assertEqual('read not recorded', res.data['message'])

4 changes: 3 additions & 1 deletion authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
ArticleTagsAPIView,
ArticleDeleteAPIView,
FavouritesView,
LikeArticleView
LikeArticleView,
ReadingView
)

urlpatterns = [
path('articles', RetrieveArticlesAPIView.as_view(),),
path('articles/<str:slug>/like/',
LikeArticleView.as_view(), name='article-like'),
path('articles/<str:slug>/', CreateArticleView.as_view()),
path('articles/<slug>/<count>', ReadingView.as_view(), name='reading'),
path('articles/', CreateArticleView.as_view(), name='article-create'),
path('<slug>/tags/', ArticleTagsAPIView.as_view(), name='article-tags'),
path('<slug>/tags/<tag>/', ArticleDeleteAPIView.as_view(), name='delete-tag'),
Expand Down
Loading

0 comments on commit 0419d37

Please sign in to comment.