Ejemplo n.º 1
0
    def test_passing_context_to_serializer(self):
        serializer = CommentSerializer(self.comment_1)
        self.assertFalse(serializer.fields['content'].read_only)

        serializer = CommentSerializer(self.comment_1, context={'reaction_update': True})
        self.assertTrue(serializer.fields['content'].read_only)

        serializer = CommentSerializer(self.comment_1, context={'flag_update': True})
        self.assertTrue(serializer.fields['content'].read_only)
Ejemplo n.º 2
0
 def test_success(self):
     response = self.client.get(self.get_url())
     self.assertEqual(response.status_code, status.HTTP_201_CREATED)
     comment = Comment.objects.get(email=self.comment_obj.email,
                                   posted=self.time_posted)
     self.assertEqual(response.data, CommentSerializer(comment).data)
     self.assertEqual(Comment.objects.all().count(), self.init_count + 1)
Ejemplo n.º 3
0
class PostSerializer(serializers.ModelSerializer):
    author = serializers.SerializerMethodField('get_username_from_author')
    video = serializers.SerializerMethodField('validate_video_url')
    like_count = serializers.SerializerMethodField('get_like_count')
    dislike_count = serializers.SerializerMethodField('get_dislike_count')
    comments = CommentSerializer(many=True)
    class Meta:
        model = Post
        fields = ['slug','description','video','created_date','author','like_count','likes','dislike_count','dislikes','favorites','comments']

    def get_username_from_author(self,post):
        username = post.author.username
        return username

    def validate_video_url(self,post):
        video = post.video
        new_url = video.url
        if '?' in new_url:
            new_url = image.url[:image.url.rfind('?')]
        return new_url

    def get_like_count(self,obj):
        return obj.likes.count()

    def get_dislike_count(self,obj):
        return obj.dislikes.count()
    
    def create(self,validated_data):
        comments_data = validated_data.pop('comments')
        post = Post.objects.create(**validated_data)
        for comment_data in comments_data:
            Comment.objects.create(post=post,**comment_data)
Ejemplo n.º 4
0
class PostSerializer(serializers.ModelSerializer):
    category = CategorySerializer()
    author = AuthorSerializer()
    comments = CommentSerializer(many=True)

    class Meta:
        model = Post
        fields = ['id', 'title', 'excerpt', 'category', 'author', 'comments']
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        key = kwargs.get('key', None)
        comment = get_comment_from_key(key)

        if comment.why_invalid == CommentFailReason.BAD:
            return Response({'error': _('Bad Signature, Comment discarded')}, status=status.HTTP_400_BAD_REQUEST)

        if comment.why_invalid == CommentFailReason.EXISTS:
            return Response({'error': _('Comment already verified')}, status=status.HTTP_200_OK)

        return Response(CommentSerializer(comment.obj).data, status=status.HTTP_201_CREATED)
Ejemplo n.º 6
0
    def get(request, *args, **kwargs):
        key = kwargs.get('key', None)
        comment = get_comment_from_key(key)

        if comment.why_invalid == CommentFailReason.BAD:
            return Response({'detail': EmailError.BROKEN_VERIFICATION_LINK}, status=status.HTTP_400_BAD_REQUEST)

        if comment.why_invalid == CommentFailReason.EXISTS:
            return Response({'detail': EmailError.USED_VERIFICATION_LINK}, status=status.HTTP_200_OK)

        return Response(CommentSerializer(comment.obj).data, status=status.HTTP_201_CREATED)
Ejemplo n.º 7
0
 def get_comments(self, obj):
     comments_qs = Comment.objects.filter_by_object(obj)
     return CommentSerializer(comments_qs, many=True).data
Ejemplo n.º 8
0
 def get_comments(self, obj):
     comments_qs = Comment.objects.filter_by_instance(obj)
     comments = CommentSerializer(comments_qs, many=True).data
     return comments
Ejemplo n.º 9
0
 def get_comments(self, obj):
     comments_qs = Comment.objects.filter_parents_by_object(obj).order_by(
         'posted')
     return CommentSerializer(comments_qs, many=True).data