-
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.
Merge pull request #26 from andela/ft-rate-article-160617674
#160617674 rating articles
- Loading branch information
Showing
8 changed files
with
200 additions
and
29 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,3 +1,3 @@ | ||
from django.contrib import admin | ||
from authors.apps.articles.models import Article | ||
from authors.apps.articles.models import Article, RateArticle | ||
admin.site.register(Article) |
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 |
---|---|---|
@@ -1,15 +1,38 @@ | ||
from authors.apps.articles.models import Article,RateArticle | ||
from authors.apps.authentication.models import User | ||
from rest_framework import serializers | ||
from authors.apps.articles.models import Article | ||
|
||
|
||
class ArticleSerializer(serializers.ModelSerializer): | ||
"""Serializer for articles.""" | ||
author = serializers.ReadOnlyField(source='author.username') | ||
read_time=serializers.ReadOnlyField(source='read') | ||
read_time = serializers.ReadOnlyField(source='read') | ||
|
||
class Meta: | ||
model = Article | ||
""" List all of the fields that could possibly be included in a request | ||
or response, including fields specified explicitly above.""" | ||
|
||
fields = ('author','title','slug','description','body','created_at','updated_at','read_time') | ||
read_only_fields = ('slug','author_id',) | ||
|
||
fields = ('author', 'title', 'slug', 'description', | ||
'body', 'created_at', 'updated_at', 'read_time','average_rating') | ||
read_only_fields = ('slug', 'author_id',) | ||
|
||
|
||
class RateArticleSerializer(serializers.ModelSerializer): | ||
rated_by = serializers.ReadOnlyField(source='rated_by.username') | ||
article = serializers.ReadOnlyField(source='article.slug') | ||
|
||
class Meta: | ||
model = RateArticle | ||
fields = ['rated_by', 'date_created', 'rating', 'article'] | ||
|
||
def validate(self, data): | ||
rating = data.get('rating') | ||
if not rating in range(1, 6): | ||
raise serializers.ValidationError( | ||
"Your rating should be in range of 1 to 5." | ||
) | ||
|
||
return { | ||
"rating": rating, | ||
} |
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,11 @@ | ||
from django.urls import path | ||
|
||
from .views import ArticleAPIView, ArticleAPIDetailsView | ||
from .views import ArticleAPIView, ArticleAPIDetailsView, RateArticleView | ||
|
||
app_name = "articles" | ||
|
||
urlpatterns = [ | ||
path("articles/", ArticleAPIView.as_view()), | ||
path('articles/<str:slug>/', ArticleAPIDetailsView.as_view(),name='retrieveUpdateDelete'), | ||
path('articles/<str:slug>/rating/', RateArticleView.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
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,64 @@ | ||
from rest_framework import status | ||
from rest_framework.test import APIClient, APITestCase | ||
from rest_framework import status | ||
from ..test_authentication.test_base import BaseTest | ||
from authors.apps.articles.models import Article | ||
from authors.apps.authentication.models import User | ||
|
||
|
||
class ArticlesTest(APITestCase, BaseTest): | ||
def setUp(self): | ||
BaseTest.__init__(self) | ||
self.client = APIClient() | ||
|
||
def create_login_user(self): | ||
user = User.objects.create_user( | ||
self.username, self.email, self.password) | ||
User.is_verified = True | ||
token = str(user.token(1)) | ||
self.loginresponse = self.client.post( | ||
"/api/users/login/", self.user_login, format="json") | ||
self.addcredentials(token) | ||
self.client.post('/api/articles/', self.create_article, format="json") | ||
|
||
def create_login_user2(self): | ||
user = User.objects.create_user('mim', 'mim@gmail.com', 'Mim123@ghjo') | ||
User.is_verified = True | ||
token = str(user.token(1)) | ||
self.loginresponse = self.client.post( | ||
"/api/users/login/", self.user_login, format="json") | ||
self.addcredentials(token) | ||
|
||
def addcredentials(self, response): | ||
self.client.credentials( | ||
HTTP_AUTHORIZATION='Token ' + response) | ||
|
||
def test_valid_rating(self): | ||
self.create_login_user() | ||
self.create_login_user2() | ||
response = self.client.post( | ||
f'/api/articles/{self.slug}/rating/', self.rate_data, format="json") | ||
self.assertEqual(response.status_code, 201) | ||
|
||
def test_rating_own_article(self): | ||
self.create_login_user() | ||
response = self.client.post( | ||
f'/api/articles/{self.slug}/rating/', self.rate_data, format="json") | ||
self.assertIn("can not rate your own article", str(response.data)) | ||
|
||
def test_invalid_rating(self): | ||
self.create_login_user() | ||
self.create_login_user2() | ||
self.rate_data['rate']['rating'] = 9 | ||
response = self.client.post( | ||
f'/api/articles/{self.slug}/rating/', self.rate_data, format="json") | ||
self.assertIn("range of 1 to 5.", str(response.data)) | ||
|
||
def test_rate_article_twice(self): | ||
self.create_login_user() | ||
self.create_login_user2() | ||
self.client.post( | ||
f'/api/articles/{self.slug}/rating/', self.rate_data, format="json") | ||
response = self.client.post( | ||
f'/api/articles/{self.slug}/rating/', self.rate_data, format="json") | ||
self.assertIn("already rated this article", str(response.data)) |
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