Skip to content

Commit

Permalink
fix(core-api-functionality): ensure all views have serialisers
Browse files Browse the repository at this point in the history
-Add serialisers to all social auth views for core api to work with
-Add serialisers to FavoriteHandlerView

[Fixes #164726566]
  • Loading branch information
joelethan authored and marthamareal committed Mar 21, 2019
1 parent a2c1de7 commit b870cc3
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 90 deletions.
17 changes: 8 additions & 9 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from rest_framework.response import Response
from rest_framework import status
from authors.apps.profiles.models import Profile
from django.contrib.postgres.fields import ArrayField

from authors.settings import WORD_LENGTH, WORD_PER_MINUTE

Expand All @@ -14,17 +13,18 @@ class ArticleManager(models.Manager):
In our article manager we are going to specify
methods relating to how to handle favoriting an article
"""

def handle_favorite_an_article(self, user_obj, slug):

user_profile = Profile.objects.filter(user=user_obj).first()
article_to_favorite = self.model.objects.filter(slug=slug).first()
article_to_favorite.favoritesCount = article_to_favorite.favorites.count() + 1
article_to_favorite.favorited = True
article_to_favorite.favorites.add(user_profile)
article_to_favorite.save()
return Response({
"message": "Article has been added to favorites"
},status=status.HTTP_201_CREATED)
"message": "Article has been added to favorites"},
status=status.HTTP_201_CREATED)

def unfavorite_an_article(self, request_user, slug):
article_slug = slug
Expand All @@ -36,20 +36,19 @@ def unfavorite_an_article(self, request_user, slug):
unfavorite_article.favorites.remove(user_)
unfavorite_article.favorited = unfavorite_article.favorites.count() > 0
unfavorite_article.save()
return Response({
"message": "Article has been removed from favorites"
},status=status.HTTP_200_OK)
return Response({"message": "Article has been removed from favorites"},
status=status.HTTP_200_OK)

class Article(models.Model):
title = models.CharField(max_length=100)
title = models.CharField(max_length=255)
author = models.ForeignKey(
Profile,
on_delete=models.CASCADE,
related_name="author_articles"
)
body = models.TextField()
slug = models.SlugField(unique=True, blank=True)
description = models.CharField(max_length=100)
description = models.CharField(max_length=255)
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
Expand Down
10 changes: 9 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ArticleSerializer (serializers.ModelSerializer):

author = ProfileSerializer(read_only=True)
user_rating = serializers.CharField(
source="average_rating", required=False)
source="average_rating", required=False,read_only=True)
read_time = serializers.CharField(max_length=100, read_only=True)
favorites = serializers.SerializerMethodField()
comments = serializers.SerializerMethodField()
Expand Down Expand Up @@ -41,7 +41,15 @@ class Meta:
'slug',
'created_at',
'updated_at',
"favorited",
"favorites",
'user_rating',
"favoritesCount",
"likes",
"dislikes",
"read_time",
"comments",

)

def get_favorites(self, obj):
Expand Down
8 changes: 4 additions & 4 deletions authors/apps/articles/tests/test_data.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
""" article test data """
valid_article = {
"article": {

"title": "When ooh in oo updated ocean",
"description": "this is the description,.",
"body": "this is the body"
}
}


valid_article_with_tags = {
"article": {

"title": "When ooh in oo updated ocean",
"description": "this is the description,.",
"body": "this is the body",
"tagList": ["test", "apps"]
}

}
12 changes: 6 additions & 6 deletions authors/apps/articles/tests/test_rating_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_rate_your_own_article(self):
article = Article.objects.all().first()
response = self.client.post(
reverse("articles:rating", kwargs={'slug': article.slug}),
data={"article": {"score": 3}},
data={"score": 3},
format="json"
)
data = response.data
Expand All @@ -39,7 +39,7 @@ def test_rate_someones_article(self):
self.register_and_login_new_user()
response = self.client.post(
reverse("articles:rating", kwargs={'slug': article.slug}),
data={"article": {"score": 3}},
data={"score": 3},
format="json"
)
data = response.data
Expand All @@ -55,7 +55,7 @@ def test_rate_out_of_range(self):
self.register_and_login_new_user()
response = self.client.post(
reverse("articles:rating", kwargs={'slug': article.slug}),
data={"article": {"score": 6}},
data={"score": 6},
format="json"
)
data = response.data
Expand All @@ -70,7 +70,7 @@ def test_rate_article_not_found(self):
self.register_and_login_new_user()
response = self.client.post(
reverse("articles:rating", kwargs={'slug': "tommy"}),
data={"article": {"score": 3}},
data={"score": 3},
format="json"
)
self.assertEqual(response.status_code, 404)
Expand All @@ -86,13 +86,13 @@ def test_rate_article_already_rated(self):
self.register_and_login_new_user()
self.client.post(
reverse("articles:rating", kwargs={'slug': article.slug}),
data={"article": {"score": 3}},
data={"score": 3},
format="json"
)
"Second rating"
response = self.client.post(
reverse("articles:rating", kwargs={'slug': article.slug}),
data={"article": {"score": 3}},
data={"score": 3},
format="json"
)
data = response.data
Expand Down
7 changes: 4 additions & 3 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_queryset(self):
return queryset

def post(self, request):
data = request.data.get('article')
data = request.data
serializer = self.serializer_class(
data=data,
context={"request": request}
Expand Down Expand Up @@ -101,7 +101,7 @@ def get(self, request, slug):

def patch(self, request, slug):
data = request.data
article_data = data.get('article') if "article" in data else data
article_data = data
article = self.get_object(slug)
context = {"request": request}
if article:
Expand Down Expand Up @@ -196,7 +196,7 @@ class RateArticleView(generics.GenericAPIView):
def post(self, request, slug):

user = request.user
score_data = request.data.get("article", {})
score_data = request.data

score = score_data.get("score", 0)
article = get_object_or_404(Article, slug=slug)
Expand Down Expand Up @@ -240,6 +240,7 @@ def get(self, request):
class FavoriteHandlerView(generics.GenericAPIView):
permission_classes = [permissions.IsAuthenticated, ]
renderer_classes = [ArticleJSONRenderer, ]
serializer_class = serializers.ArticleSerializer

def post(self, request, slug):
user = request.user
Expand Down
14 changes: 14 additions & 0 deletions authors/apps/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,17 @@ def update(self, instance, validated_data):
instance.save()

return instance


class ResetPasswordSerializer(serializers.Serializer):
email = serializers.CharField(max_length=255)

class Meta:
fields = ('email', )


class ChangePasswordSerializer(serializers.Serializer):
password = serializers.CharField(max_length=128, write_only=True)

class Meta:
fields = ('password', )
72 changes: 36 additions & 36 deletions authors/apps/authentication/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,138 +2,138 @@
authentication test data
"""
valid_user = {
"user": {

"username": "Bagzie12",
"email": "bagendadeogracious@gmail.com",
"password": "Password123"
}

}

valid_login = {
"user": {

"email": "bagendadeogracious@gmail.com",
"password": "Password123"
}

}

valid_user_two = {
"user": {

"username": "crycetruly",
"email": "crycetruly@gmail.com",
"password": "xvq6thcuzy"
}

}


valid_login_two = {
"user": {

"email": "crycetruly@gmail.com",
"password": "xvq6thcuzy"
}

}

wrong_password = {
"user": {

"email": "bagendadeogracious@gmail.com",
"password": "Password12"
}

}

wrong_email = {
"user": {

"email": "bagenda@gmail.com",
"password": "Password123"
}

}

missing_password_data = {
"user": {

"email": "Password123"
}

}
missing_email_data = {
"user": {

"password": "Password123"
}

}
empty_username = {
"user": {

"username": "",
"email": "bagendadeogracious@gmail.com",
"password": "Password123"
}

}
empty_email = {
"user": {

"username": "Bagzie",
"email": "",
"password": "Password123"
}

}
empty_password = {
"user": {

"username": "Bagzie",
"email": "bagendadeogracious@gmail.com",
"password": ""
}

}
invalid_user_email = {
"user": {

"username": "Bagzie",
"email": "bagendadegmail.com",
"password": "Password123"
}

}

short_password = {
"user": {

"username": "Bagzie",
"email": "bagendadeogracious@gmail.com",
"password": "Pass"
}

}

missing_username_key = {
"user": {

"email": "bagendadeogracious@gmail.com",
"password": "Password123"
}

}


invalid_token = "eyJ0eXiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IkFoZWJ3YTEiLCJlbWFpbCI6ImNyeWNldHJ1bHlAZ21haWwuY29tIiwiZXhwIjoxNTUxNzc2Mzk0fQ.PFimaBvSaxR_cKwLmeRMod7LHkhNTcem22IXTrrg7Ko"
expired_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IkFoZWJ3YTEiLCJlbWFpbCI6ImNyeWNldHJ1bHlAZ21haWwuY29tIiwiZXhwIjoxNTUxNzc2Mzk0fQ.PFimaBvSaxR_cKwLmeRMod7LHkhNTcem22IXTrrg7Ko"

invalid_username = {
"user": {

"username": "testus i",
"email": "testuser@gmail.com",
"password": "testing123"
}

}

invalid_password = {
"user": {

"username": "testuser12",
"email": "testuser@gmail.com",
"password": "testingui"
}

}

same_email = {
"user": {

"username": "roy12",
"email": "bagendadeogracious@gmail.com",
"password": "Password123"
}

}

same_username = {
"user": {

"username": "Bagzie12",
"email": "roywaisibani@gmail.com",
"password": "Password123"
}

}

responses = {
Expand Down
Loading

0 comments on commit b870cc3

Please sign in to comment.