Skip to content

Commit

Permalink
Merge 7341089 into 3900a32
Browse files Browse the repository at this point in the history
  • Loading branch information
huxaiphaer committed Oct 30, 2018
2 parents 3900a32 + 7341089 commit 90ad5da
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination
from authors.apps.articles.exceptions import NotFoundException

from authors.apps.articles.models import (Article,
Tag, Rating, ArticleReport, Comments, Replies)
Tag, Rating, ArticleReport, Comments, Replies)
from authors.apps.articles.utils import get_date
from authors.apps.authentication.models import User
from authors.apps.profiles.models import UserProfile
from authors.apps.profiles.serializers import UserProfileSerializer
from rest_framework.exceptions import NotFound


class TagRelatedField(serializers.RelatedField):
"""
Implements a custom relational field by overriding RelatedFied.
Expand All @@ -24,29 +22,52 @@ def to_representation(self, value):

return value.tag_name


class TagSerializer(serializers.ModelSerializer):
"""Handles serialization and deserialization of Tag objects."""

class Meta:
model = Tag
fields = "__all__"


class RepliesSerializer(serializers.ModelSerializer):

def to_representation(self, instance):
"""
formats serializer display response
:param instance:
:return:
"""
response = super().to_representation(instance)
profile = UserProfile.objects.get(user=instance.author)

response['author'] = profile.user.username
return response

class Meta:
model = Replies
fields = '__all__'
fields = ('id', 'content', 'created_at', 'comment', 'author')


class CommentSerializer(serializers.ModelSerializer):

replies = RepliesSerializer(many=True, read_only=True)

def to_representation(self, instance):
"""
formats serializer display response
:param instance:
:return:
"""
response = super().to_representation(instance)
profile = UserProfile.objects.get(user=instance.author)

response['article'] = instance.article.slug
response['author'] = profile.user.username
return response

class Meta:
model = Comments
fields = ('id', 'body', 'article', 'author', 'replies')
fields = ('id', 'body', 'article', 'author', 'created_at', 'replies')


class ArticleSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -170,7 +191,6 @@ def get_likes(self, instance):
def get_dislikes(self, instance):
return instance.dislikes.all().count()


class PaginatedArticleSerializer(PageNumberPagination):
"""
Pagination class
Expand All @@ -194,7 +214,6 @@ def get_paginated_response(self, data):
'results': data
}


class RatingSerializer(serializers.ModelSerializer):
"""
Define action logic for an article rating
Expand Down Expand Up @@ -289,4 +308,4 @@ def to_representation(self, instance):

class Meta:
model = ArticleReport
fields = ['user', 'article', 'report_message']
fields = ['user', 'article', 'report_message']

0 comments on commit 90ad5da

Please sign in to comment.