-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
from rest_framework.exceptions import NotFound | ||
|
||
class ArticleNotFound(NotFound): | ||
default_detail = 'Article with this slug not found' | ||
default_detail = 'Article with this slug not found' | ||
|
||
class CommentNotFound(NotFound): | ||
""" | ||
comment id not found exception | ||
""" | ||
|
||
default_detail = 'Comment not found.' |
25 changes: 25 additions & 0 deletions
25
authors/apps/articles/migrations/0005_auto_20181219_1816.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Generated by Django 2.1 on 2018-12-19 18:16 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('articles', '0004_article_tags'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='comment', | ||
name='dislikes', | ||
field=models.ManyToManyField(blank=True, related_name='dislikecomment', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AddField( | ||
model_name='comment', | ||
name='likes', | ||
field=models.ManyToManyField(blank=True, related_name='likecomment', to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
authors/apps/articles/tests/test_liking_and_disliking_comments.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from rest_framework import status | ||
from .base_test import BaseTestCase | ||
from django.urls import reverse | ||
|
||
|
||
class TestLikingComments(BaseTestCase): | ||
"class to test liking and disliking of articles" | ||
|
||
def test_like_comment(self): | ||
"""test for liking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.like_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': 'You have liked this comment'}) | ||
|
||
def test_dislike_comment(self): | ||
"""test for liking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.dislike_comment_url(comment_id) , format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': "You have disliked this comment"}) | ||
|
||
def test_like_un_like_comment(self): | ||
"""test for liking then disliking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.like_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.data, {'message': 'You have liked this comment'}) | ||
response = self.client.put(self.like_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': "You have unliked this comment"}) | ||
|
||
def test_dislike_un_dislike_comment(self): | ||
"""test for liking then unliking a comment""" | ||
comment_id, token = self.get_comment_id() | ||
response = self.client.put(self.dislike_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {'message': "You have disliked this comment"}) | ||
response = self.client.put(self.dislike_comment_url(comment_id), format='json', HTTP_AUTHORIZATION=token) | ||
self.assertEqual(response.data, {'message': 'This comment has been un-disliked'}) | ||
|
||
def test_like_nonexisting_comment(self): | ||
"""test for liking an comment""" | ||
self.user_signup() | ||
token = self.user_login() | ||
non_existing_id = 5 # not existing comment ID | ||
response = self.client.put(self.like_comment_url(non_existing_id), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {'detail': "Comment not found."}) | ||
|
||
def test_dislike_nonexisting_comment(self): | ||
"""test for liking an comment""" | ||
self.user_signup() | ||
token = self.user_login() | ||
non_existing_id = 5 # not existing comment ID | ||
response = self.client.put(self.dislike_comment_url(non_existing_id), format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.data, {'detail': "Comment not found."}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from rest_framework.generics import UpdateAPIView | ||
from authors.apps.articles.models import Comment | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from authors.apps.articles.exceptions import CommentNotFound | ||
|
||
class LikeComment(UpdateAPIView): | ||
"""Class for liking and un-liking an article""" | ||
|
||
def update(self, request, comment_pk): | ||
"""This method updates the liking of an article""" | ||
try: | ||
comment = Comment.objects.get(id=comment_pk) | ||
except Comment.DoesNotExist: | ||
raise CommentNotFound | ||
|
||
# gets the user of that specific session | ||
user = request.user | ||
# checks for the boolean value of liking an comments | ||
liked = bool(user in comment.likes.all()) | ||
if liked is True: | ||
comment.likes.remove(user.id) | ||
message = {"message": "You have unliked this comment"} | ||
return Response(message, status.HTTP_200_OK) | ||
|
||
# if like is false, the comment is liked | ||
disliked = bool(user in comment.dislikes.all()) | ||
if disliked is True: | ||
comment.dislikes.remove(user.id) # un-dislike if user had disliked | ||
comment.likes.add(user.id) # now like the comment | ||
message = {"message": "You have liked this comment"} | ||
return Response(message, status.HTTP_200_OK) | ||
|
||
|
||
class DislikeComment(UpdateAPIView): | ||
"""Class for disliking and un-disliking an comment""" | ||
|
||
def update(self, request, comment_pk): | ||
"""This method updates the liking of an comment""" | ||
comment = Comment.objects.filter(id=comment_pk).first() | ||
if comment is None: | ||
raise CommentNotFound | ||
|
||
# gets the user of that specific session | ||
user = request.user | ||
# checks for the boolean value of disliking an comment | ||
disliked = bool(user in comment.dislikes.all()) | ||
if disliked is True: | ||
comment.dislikes.remove(user.id) | ||
message = {"message": "This comment has been un-disliked"} | ||
return Response(message, status.HTTP_200_OK) | ||
|
||
# if dislike is false, the comment is disliked | ||
liked = bool(user in comment.likes.all()) | ||
if liked is True: | ||
comment.likes.remove(user.id) # unlike if user had liked | ||
comment.dislikes.add(user.id) # now dislike the comment | ||
message = {"message": "You have disliked this comment"} | ||
return Response(message, status.HTTP_200_OK) |