Skip to content

Commit

Permalink
Chore(Reading stats):Fix reading stats.
Browse files Browse the repository at this point in the history
This commit ensures that reading stats of all users of the product are recorded.
  • Loading branch information
KicaRonaldOkello committed Jan 30, 2019
1 parent fb890c1 commit d54c78c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 58 deletions.
45 changes: 1 addition & 44 deletions authors/apps/articles/test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,4 @@ def test_read_statistics_updated_successfully(self):
'/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'])

self.assertEqual("done", res.data['result'])
20 changes: 6 additions & 14 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def delete(self, request, slug):

class ReadingView(GenericAPIView):
""" class view to enable viewing readers statistics """
serializer_class = ReadingSerializer
permission_class = (AllowAny,)

def do_math(self, article, count):
"""
Expand All @@ -530,21 +530,13 @@ def post(self, request, slug, count):
This class method updates the view counts on an article
"""
article = Article.objects.filter(slug=slug).first()
reader = Readings.objects.filter(
author=request.user.id).filter(article=article)
if not self.do_math(article, count):
return Response({"message": "read not recorded"}, status=status.HTTP_301_MOVED_PERMANENTLY)
if len(reader) < 1:
article.read_count += 1
article.save()
author = User.objects.get(id=request.user.id)
read_obj = Readings(author=author, article=article)
read_obj.save()
serializer = self.serializer_class(read_obj)
return Response(serializer.data, status=status.HTTP_200_OK)
article.view_count +=1
else:
serializer = self.serializer_class(reader.first())
return Response(serializer.data, status=status.HTTP_200_OK)
article.view_count +=1
article.read_count += 1
article.save()
return Response({"result":"done"}, status=status.HTTP_200_OK)


class BookmarkView(GenericAPIView):
Expand Down

0 comments on commit d54c78c

Please sign in to comment.