Skip to content

Commit

Permalink
Merge pull request #56 from andela/bg-fix-rating-163383181
Browse files Browse the repository at this point in the history
163383181 Implement Bug Fix User can rate an article
  • Loading branch information
missvicki committed Mar 21, 2019
2 parents d134bb6 + 2216a29 commit 3e8d1eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion authors/apps/articles/tests/test_rate_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_rate_article_author(self):
format='json')
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertIn("Author can not rate their own article",
response.data.get('errors'))
response.data.get('message'))

# integration tests for all modules under rating articles start here

Expand Down
30 changes: 21 additions & 9 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,37 @@ def post(self, request, slug):
article = get_single_article_using_slug(slug)
user = request.user

serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)

rate = request.data.get('rate_score', {})
if rate > 5 or rate < 1:
return Response({
'errors': 'Rate score should be between 1 and 5'},
status=status.HTTP_400_BAD_REQUEST)

if article.author.pk == user.pk:
return Response({
'errors': 'Author can not rate their own article'},
status=status.HTTP_403_FORBIDDEN)

serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
message = 'Author can not rate their own article'
data = {
"title": article.title,
"author": article.author.username,
"average_ratings": article.average_ratings
}
return Response({'articles': data,
'message': message},
status=status.HTTP_403_FORBIDDEN)

try:
Rating.objects.get(user=user.pk, article_id=article.pk)
return Response(
{'message': 'You already rated this article'},
status=status.HTTP_422_UNPROCESSABLE_ENTITY)
message = 'You already rated this article'
data = {
"title": article.title,
"author": article.author.username,
"average_ratings": article.average_ratings
}
return Response({'articles': data,
'message': message},
status=status.HTTP_422_UNPROCESSABLE_ENTITY)
except Rating.DoesNotExist:
serializer.save(user=user, article=article)
rate_data = serializer.data
Expand Down

0 comments on commit 3e8d1eb

Please sign in to comment.