-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(like/dislike articles): Like and dislike articles by users
- user can like article - user can dislike article - return liked/disliked article after like/dislike - user can undo a like /dislike - test the feature works [Finishes #164047068]
- Loading branch information
Showing
6 changed files
with
295 additions
and
28 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
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,25 +1,77 @@ | ||
from rest_framework import serializers | ||
from .models import Article | ||
|
||
from authors.apps.authentication.serializers import UserSerializer | ||
from .models import Article, ArticleLikes | ||
|
||
|
||
class LikesSerializer(serializers.ModelSerializer): | ||
""" | ||
This class serializes data from ArticleLikes model | ||
""" | ||
user = UserSerializer() | ||
|
||
class Meta: | ||
model = ArticleLikes | ||
fields = ('user', ) | ||
|
||
|
||
class ArticleSerializer(serializers.ModelSerializer): | ||
""" | ||
converts the model into JSON format | ||
""" | ||
|
||
title = serializers.CharField(required=True) | ||
slug = serializers.SlugField(required=False) | ||
description = serializers.CharField(required=True) | ||
body = serializers.CharField(required=True) | ||
created_at = serializers.DateTimeField(read_only=True) | ||
updated_at = serializers.DateTimeField(read_only=True) | ||
favourited = serializers.BooleanField(required=False) | ||
author = UserSerializer(read_only=True) | ||
likes = serializers.SerializerMethodField() | ||
dislikes = serializers.SerializerMethodField() | ||
likes_count = serializers.SerializerMethodField() | ||
dislikes_count = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Article | ||
fields = ['title', 'description', | ||
'body', 'created_at', 'updated_at', 'slug', 'favourited', 'author'] | ||
fields = [ | ||
'title', | ||
'description', | ||
'body', | ||
'created_at', | ||
'updated_at', | ||
'slug', | ||
'favourited', | ||
'author', | ||
'likes_count', | ||
'dislikes_count', | ||
'likes', | ||
'dislikes', | ||
] | ||
|
||
def get_likes(self, obj): | ||
""" | ||
This method returns users who liked an Article | ||
:param obj: This is the Article object | ||
:return: users who liked an article | ||
""" | ||
query = obj.liked.filter(likes=1) | ||
return LikesSerializer(query, many=True).data | ||
|
||
def get_dislikes(self, obj): | ||
""" | ||
This method returns users who disliked an Article | ||
:param obj: This is the Article object | ||
:return: users who liked an article | ||
""" | ||
query = obj.liked.filter(dislikes=-1) | ||
return LikesSerializer(query, many=True).data | ||
|
||
def get_likes_count(self, obj): | ||
""" | ||
This method returns number of users who liked an Article | ||
:param obj: This is the Article object | ||
:return: count of users who liked an article | ||
""" | ||
return obj.liked.filter(likes=1).count() | ||
|
||
def get_dislikes_count(self, obj): | ||
""" | ||
This method returns number of users who disliked an Article | ||
:param obj: This is the Article object | ||
:return: count of users who disliked an article | ||
""" | ||
return obj.liked.filter(dislikes=-1).count() |
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,112 @@ | ||
import json | ||
|
||
from rest_framework.test import APIClient, APITestCase | ||
from rest_framework.views import status | ||
|
||
|
||
class TestArticleLikes(APITestCase): | ||
""" This class tests the Article like and dislike feature | ||
""" | ||
|
||
client = APIClient() | ||
|
||
def setUp(self): | ||
""" This is the set-up method for all tests | ||
""" | ||
self.user = {"user": { | ||
"username": "kibet", | ||
"email": "kibet@olympians.com", | ||
"password": "qwerty12" | ||
}} | ||
|
||
self.profile = { | ||
"bio": "am fantastic", | ||
"interests": "football", | ||
"favorite_quote": "Yes we can", | ||
"mailing_address": "P.O BOX 1080", | ||
"website": "http://www.web.com", | ||
"active_profile": True | ||
} | ||
|
||
self.article = { | ||
"title": "Andela", | ||
"description": "sdsd", | ||
"body": "dsd", | ||
"images": "" | ||
} | ||
|
||
# create user | ||
self.client.post( | ||
'/api/users/', self.user, format='json') | ||
|
||
response = self.client.post( | ||
'/api/users/login/', self.user, format='json') | ||
|
||
result = json.loads(response.content) | ||
|
||
self.client.credentials( | ||
HTTP_AUTHORIZATION='Token ' + result["user"]["token"]) | ||
|
||
profile = self.client.post( | ||
'/api/profile/create_profile/', self.profile, format='json') | ||
prof_result = json.loads(profile.content) | ||
|
||
article = self.client.post('/api/articles/', self.article, | ||
format='json') | ||
|
||
article_result = json.loads(article.content) | ||
self.slug = article_result["article"]["slug"] | ||
|
||
def test_like_article(self): | ||
""" | ||
Test like an article | ||
""" | ||
# | ||
response = self.client.post('/api/articles/' + self.slug + '/like', | ||
format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertIn('Successfully liked: {} article'.format(self.slug), | ||
str(result)) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_dislike_article(self): | ||
""" | ||
Test like an article | ||
""" | ||
# | ||
response = self.client.post('/api/articles/' + self.slug + '/dislike', | ||
format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertIn('Successfully disliked: {} article'.format(self.slug), | ||
str(result)) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_like_non_existing_article(self): | ||
""" | ||
Test like an article | ||
""" | ||
# | ||
response = self.client.post('/api/articles/random/like', | ||
format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertIn('Article requested does not exist'.format(self.slug), | ||
str(result)) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
def test_unlike_article(self): | ||
""" | ||
Test like an article | ||
""" | ||
# | ||
self.client.post('/api/articles/' + self.slug + '/like', | ||
format='json') | ||
response = self.client.post('/api/articles/' + self.slug + '/like', | ||
format='json') | ||
result = json.loads(response.content) | ||
|
||
self.assertIn('Successfully undid (dis)like on {} article'.format(self.slug), | ||
str(result)) | ||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) |
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,10 +1,12 @@ | ||
from django.urls import path | ||
|
||
from .views import (ArticlesAPIView, RetrieveArticleAPIView) | ||
from .views import (ArticlesAPIView, RetrieveArticleAPIView, LikeAPIView, DislikeAPIView) | ||
|
||
app_name = "articles" | ||
|
||
urlpatterns = [ | ||
path('articles/', ArticlesAPIView.as_view()), | ||
path('articles/<slug>', RetrieveArticleAPIView.as_view()), | ||
path('articles/<slug>/like', LikeAPIView.as_view()), | ||
path('articles/<slug>/dislike', DislikeAPIView.as_view()), | ||
] |
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