Skip to content

Commit

Permalink
Merge 6a0d80b into b2b5fee
Browse files Browse the repository at this point in the history
  • Loading branch information
oma0256 committed Feb 11, 2019
2 parents b2b5fee + 6a0d80b commit b5a4f18
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 2 additions & 0 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def get_favorited(self, obj):
This method returns True is the logged in user favorited the article
otherwise it returns False.
"""
if self.context["request"].user.is_anonymous:
return False
if self.context["request"].user.profile in obj.favorited_by.all():
return True
return False
Expand Down
6 changes: 3 additions & 3 deletions authors/apps/articles/tests/test_article_api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_update_an_article_authorized_user(self):
"""
self.create_article_and_authenticate_test_user()
article = Article.objects.all().first()
response = self.client.put(self.article_url(article.slug),
response = self.client.patch(self.article_url(article.slug),
data=test_article_data.update_article_data,
format='json')
self.assertIn('title', response.data)
Expand All @@ -110,7 +110,7 @@ def test_update_an_article_unauthorized_user(self):
self.create_article_and_authenticate_test_user()
self.create_article_and_authenticate_test_user_2()
article = Article.objects.all().first()
response = self.client.put(reverse('articles:article-details',
response = self.client.patch(reverse('articles:article-details',
kwargs={'slug': article.slug}),
data=test_article_data.update_article_data,
format='json')
Expand All @@ -126,7 +126,7 @@ def test_update_unexisting_article_slug(self):
"""
self.create_article_and_authenticate_test_user()
article = Article.objects.all().first()
response = self.client.put(reverse('articles:article-details',
response = self.client.patch(reverse('articles:article-details',
kwargs={'slug':
test_article_data.
un_existing_slug}),
Expand Down
22 changes: 10 additions & 12 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get(self, request, slug):
'errors': 'sorry article with that slug doesnot exist'
}, status=status.HTTP_404_NOT_FOUND)

def put(self, request, slug):
def patch(self, request, slug):
data = request.data
article_data = data.get('articles') if "articles" in data else data
article = self.get_object(slug)
Expand Down Expand Up @@ -116,14 +116,14 @@ def post(self, request, slug='', action='like'):
article.disliked_by.remove(request.user)
article.liked_by.add(request.user)
return Response({
'message': 'liked'
'message': 'You liked this article!'
})

if article and action is 'dislike':
article.liked_by.remove(request.user)
article.disliked_by.add(request.user)
return Response({
'message': 'disliked'
'message': 'You disliked this article!'
})

return Response({
Expand All @@ -139,13 +139,13 @@ def delete(self, request, slug='', action='like'):
if article and action is 'like':
article.liked_by.remove(request.user)
return Response({
'message': 'like undone'
'message': 'You no longer like this article'
})

if article and action is 'dislike':
article.disliked_by.remove(request.user)
return Response({
'message': 'dislike undone'
'message': 'You no longer dislike this article'
})

return Response({
Expand Down Expand Up @@ -221,14 +221,12 @@ def post(self, request, slug):
rate = request.data.get('rate_score', {})
if rate > 5 or rate < 1:
return Response({
'errors': 'Rate score should be a value > 1 and < 5',
'status': status.HTTP_400_BAD_REQUEST},
'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},
'errors': 'Author can not rate their own article'},
status=status.HTTP_403_FORBIDDEN)

serializer = self.serializer_class(data=request.data)
Expand All @@ -237,12 +235,12 @@ def post(self, request, slug):
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'},
status=status.HTTP_422_UNPROCESSABLE_ENTITY)
except Rating.DoesNotExist:
serializer.save(user=user, article=article)
rate_data = serializer.data
message = 'You have rated this article successfully'
return Response({'articles': rate_data,
'status': status.HTTP_201_CREATED},
'message': message},
status=status.HTTP_201_CREATED)

0 comments on commit b5a4f18

Please sign in to comment.