Skip to content

Commit

Permalink
Merge 29fcda8 into fad5127
Browse files Browse the repository at this point in the history
  • Loading branch information
KicaRonaldOkello committed Jan 31, 2019
2 parents fad5127 + 29fcda8 commit a5c8feb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 383 deletions.
134 changes: 0 additions & 134 deletions authors/apps/articles/migrations/0001_initial.py

This file was deleted.

124 changes: 0 additions & 124 deletions authors/apps/articles/migrations/0002_auto_20190130_0645.py

This file was deleted.

23 changes: 23 additions & 0 deletions authors/apps/articles/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ def setUp(self):
]
}
}
self.article2 = {

"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"
},
{
"image_url": "https://imgur.comhenry/",
"description": "image is cool"
},
{
"image_url": "https://imgur.comhenry/",
"description": "image is cool"
}
]
}
}

self.comment = {
"comment": {
Expand Down
46 changes: 7 additions & 39 deletions authors/apps/articles/test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,15 @@ 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'])
self.assertEqual("done", res.data['result'])

def test_user_count_for_a_read_is_only_one(self):
def test_view_count_updated_successfully(self):
self.client.credentials(
HTTP_AUTHORIZATION='Bearer ' + self.login_user())
response = self.client.post(
'/api/articles/', data=self.article2, format='json')
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),
'/api/articles/{}/1'.format(self.create_article()),
format='json')
self.assertTrue(res.status_code, 301)
self.assertEqual('read not recorded', res.data['message'])

self.assertTrue(res.status_code, 200)
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 @@ -505,7 +505,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 @@ -531,21 +531,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
Loading

0 comments on commit a5c8feb

Please sign in to comment.