コード例 #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)
コード例 #2
0
ファイル: test_api.py プロジェクト: bigbeer/Comment
 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)
コード例 #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)
コード例 #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']
コード例 #5
0
ファイル: views.py プロジェクト: bigbeer/Comment
    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)
コード例 #6
0
ファイル: views.py プロジェクト: ayushbisht2001/Comment
    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)
コード例 #7
0
ファイル: serializers.py プロジェクト: matttsb/django-stuff
 def get_comments(self, obj):
     comments_qs = Comment.objects.filter_by_object(obj)
     return CommentSerializer(comments_qs, many=True).data
コード例 #8
0
 def get_comments(self, obj):
     comments_qs = Comment.objects.filter_by_instance(obj)
     comments = CommentSerializer(comments_qs, many=True).data
     return comments
コード例 #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