Beispiel #1
0
    def create(self, request, *args, **kwargs):
        serializer = LikeSerializerForCreate(data=request.data,
                                             context={'request': request})
        if not serializer.is_valid():
            return Response(
                {
                    'message': 'Please check input.',
                    'errors': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)
        #instance, created = serializer.get_or_create()
        #if created:
        #    NotificationService.send_like_notification(instance)
        # use Message Queue to create Like in DB
        if serializer.validated_data['content_type'] == 'comment':
            content_type_int = 0
            model_class = Comment
        else:
            content_type_int = 1
            model_class = Tweet
        object_id = serializer.validated_data['object_id']
        LikeService.async_create_like(content_type_int, object_id,
                                      request.user.id)

        instance = Like(
            content_type=ContentType.objects.get_for_model(model_class),
            object_id=object_id,
            user=request.user,
        )

        return Response(
            LikeSerializer(instance).data,
            status=status.HTTP_201_CREATED,
        )
Beispiel #2
0
 def create(self, request, *args, **kwargs):
     serializer = LikeSerializerForCreate(
         data=request.data,
         context={'request': request},
     )
     if not serializer.is_valid():
         return Response(
             {
                 'message': 'Please check input',
                 'errors': serializer.errors,
             },
             status=status.HTTP_400_BAD_REQUEST)
     instance = serializer.create(serializer.validated_data)
     return Response(LikeSerializer(instance).data,
                     status=status.HTTP_201_CREATED)
Beispiel #3
0
 def create(self, request, *args, **kwargs):
     serializer = LikeSerializerForCreate(data=request.data,
                                          context={'request': request})
     if not serializer.is_valid():
         return Response(
             {
                 'message': 'Please check input',
                 'errors': serializer.errors,
             },
             status=status.HTTP_400_BAD_REQUEST)
     instance, created = serializer.get_or_create()
     if created:
         NotificationService.send_like_notification(instance)
     return Response(LikeSerializer(instance).data,
                     status=status.HTTP_201_CREATED)
Beispiel #4
0
class CommentSerializerWithLikes(serializers.ModelSerializer):
    user = UserSerializerForComment(source='cached_user')
    likes = LikeSerializer(source='like_set', many=True)
    likes_count = serializers.SerializerMethodField()
    has_liked = serializers.SerializerMethodField()

    class Meta:
        model = Comment
        fields = ('id', 'tweet_id', 'user', 'content', 'likes_count',
                  'has_liked', 'likes', 'created_at', 'updated_at')

    def get_likes_count(self, obj):
        return obj.like_set.count()

    def get_has_liked(self, obj):
        return LikeService.has_liked(self.context['request'].user, obj)
Beispiel #5
0
class TweetSerializerForDetail(TweetSerializer):
    comments = CommentSerializer(source='comment_set', many=True)
    likes = LikeSerializer(source='like_set', many=True)

    class Meta:
        model = Tweet
        fields = (
            'id',
            'user',
            'created_at',
            'content',
            'likes',
            'comments',
            'comments_count',
            'likes_count',
            'has_liked',
        )
Beispiel #6
0
    def create(self, request):
        serialzier = LikeSerializerForCreate(
            data=request.data,
            context={'request': request},
        )
        if not serialzier.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": serialzier.errors,
                },
                status=HTTP_400_BAD_REQUEST)

        like = serialzier.save()
        #send like notification
        NotificationService.send_like_notificaion(like)
        return Response(LikeSerializer(like).data, status=HTTP_201_CREATED)
Beispiel #7
0
 def create(self, request):
     serializer = LikeSerializerForCreate(
         data=request.data,
         context={'request': request},
     )
     if not serializer.is_valid():
         return Response({
             'success': False,
             'error': serializer.errors,
         },
                         status=status.HTTP_400_BAD_REQUEST)
     like, created = serializer.get_or_create()
     # raise notifications in creation, do not dispatch notifications if liked
     if created:
         NotificationService.send_like_notification(like)
     return Response({
         'success': True,
         'like': LikeSerializer(like).data
     },
                     status=status.HTTP_200_OK)
class TweetSerializerForDetail(TweetSerializer):
    comments = CommentSerializer(source='comment_set', many=True)
    likes = LikeSerializer(source='like_set', many=True)

    class Meta:
        model = Tweet
        # 下列fields的option必须写完整,不能因为 class TweetSerializerForDetail 继承了 TweetSerializer,
        # 而不罗列 TweetSerializer 的Meta class 里面的fields
        fields = (
            'id',
            'user',
            'comments',
            'created_at',
            'content',
            'likes',
            'comments',
            'likes_count',
            'comments_count',
            'has_liked',
            'photo_urls',
        )
Beispiel #9
0
class TweetSerializerForDetail(TweetSerializer):
    user = UserSerializerForTweet()
    # <HOMEWORK> 使用 serialziers.SerializerMethodField 的方式实现 comments
    comments = CommentSerializer(source='comment_set', many=True)
    likes = LikeSerializer(source='like_set', many=True)

    class Meta:
        model = Tweet
        fields = (
            'id',
            'user',
            'comments',
            'created_at',
            'content',
            'likes',
            'comments',
            'likes_count',
            'comments_count',
            'has_liked',
            'photo_urls',
        )
Beispiel #10
0
class TweetSerializerForDetail(TweetSerializer):
    comments = CommentSerializer(source='comment_set', many=True)
    likes = LikeSerializer(source='like_set', many=True)

    class Meta:
        model = Tweet
        fields = (
            'id',
            'user',
            'comments',
            'created_at',
            'content',
            'likes',
            'comments',
            'likes_count',
            'comments_count',
            'has_liked',
            'photo_urls',
        )

    def get_comments(self, obj):
        return CommentSerializer(obj.comment_set.all(), many=True).data
Beispiel #11
0
class TweetSerializersWithCommentsAndLikes(serializers.ModelSerializer):
    user = UserSerializerForTweet(source='cached_user')
    comments = CommentSerializerWithLikes(source='comment_set', many=True)
    likes = LikeSerializer(source='like_set', many=True)
    has_liked = serializers.SerializerMethodField()
    comments_count = serializers.SerializerMethodField()
    likes_count = serializers.SerializerMethodField()
    photo_urls = serializers.SerializerMethodField()

    class Meta:
        model = Tweet
        fields = (
            'id',
            'user',
            'content',
            'comments',
            'likes',
            'has_liked',
            'likes_count',
            'comments_count',
            'photo_urls',
            'created_at',
        )

    def get_has_liked(self, obj):
        return LikeService.has_liked(self.context['request'].user, obj)

    def get_comments_count(self, obj):
        return obj.comment_set.count()

    def get_likes_count(self, obj):
        return obj.like_set.count()

    def get_photo_urls(self, obj):
        photo_urls = []
        for photo in obj.tweetphoto_set.all().order_by('order'):
            photo_urls.append(photo.file.url)
        return photo_urls