Skip to content

Commit

Permalink
Merge c28cf93 into 4e92b8b
Browse files Browse the repository at this point in the history
  • Loading branch information
salma-nyagaka committed Dec 19, 2018
2 parents 4e92b8b + c28cf93 commit 0bb3d61
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Article(models.Model):
like = models.ManyToManyField(User, blank=True, related_name='like')
dislike = models.ManyToManyField(User, blank=True, related_name='dislike')


favorite = models.ManyToManyField(User, blank=True, related_name='favorite')

def __str__(self):
Expand Down
18 changes: 9 additions & 9 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,34 @@ class ArticleAuthorSerializer(serializers.ModelSerializer):
body = serializers.CharField()
author = UserSerializer(read_only = True)
tags = serializers.SerializerMethodField(method_name='show_tags')

like = serializers.SerializerMethodField(method_name='like_article')
dislike = serializers.SerializerMethodField(method_name='dislike_article')


def show_tags(self, instance):
"""
Show tag details.
"""
return instance.tags.names()


def likes(self, instance):

def like_article(self, instance):
"""method to return a user who has liked an article"""
request = self.context.get('request')
liked = False

if request is not None and request.user.is_authenticated:
user_id = request.user.id
liked = instance.likes.all().filter(id=user_id).count() == 1
return {'likes': instance.likes.count(), 'User': liked}
return {'likeCount': instance.like.count()}

def dislikes(self, instance):
def dislike_article(self, instance):
"""method to return a user who has disliked an article"""
request = self.context.get('request')
disliked = False
if request is not None and request.user.is_authenticated:
user_id = request.user.id
disliked = instance.dislikes.all().filter(id=user_id).count() == 1
return {'dislikes': instance.dislikes.count(), 'User': disliked}
return {'dislikeCount': instance.dislike.count()}

favorite = serializers.SerializerMethodField(read_only=True)

def favorite(self, instance):
"""favorite an article"""
Expand Down
16 changes: 12 additions & 4 deletions authors/apps/articles/views/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def update(self, request, slug):
return Response(message, status.HTTP_200_OK)

# if like is false, the article is liked
disliked = bool(user in article.dislike.all())
if disliked is True:
article.dislike.remove(user.id)

article.like.add(user.id)
message = {"article": "You have liked this article"}
return Response(message, status.HTTP_200_OK)
Expand All @@ -192,10 +196,10 @@ class DislikeArticle(UpdateAPIView):
"""Class for disliking and un-disliking an article"""

def update(self, request, slug):
"""This method updates the liking of an article"""
LikeArticle.update(self, request, slug)
article = Article.objects.filter(article_slug=slug).first()
if article is None:
"""This method updates the disliking of an article"""
try:
article = Article.objects.get(article_slug=slug)
except Article.DoesNotExist:
return Response({
'Error': 'Article does not exist'
}, status.HTTP_404_NOT_FOUND)
Expand All @@ -210,6 +214,10 @@ def update(self, request, slug):
return Response(message, status.HTTP_200_OK)

# if disike is false, the article is disliked
liked = bool(user in article.like.all())
if liked is True:
article.like.remove(user.id)

article.dislike.add(user.id)
message = {"article": "You have disliked this article"}
return Response(message, status.HTTP_200_OK)

0 comments on commit 0bb3d61

Please sign in to comment.