Beispiel #1
0
class TweetSerializer(serializers.ModelSerializer):
    user = UserSerializerForTweet(source='cached_user')
    comments_count = serializers.SerializerMethodField()
    likes_count = serializers.SerializerMethodField()
    has_liked = serializers.SerializerMethodField()
    photo_urls = serializers.SerializerMethodField()

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

    def get_likes_count(self, obj):
        return RedisHelper.get_count(obj, 'likes_count')

    def get_comments_count(self, obj):
        return RedisHelper.get_count(obj, 'comments_count')

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

    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
Beispiel #2
0
class TweetSerializer(serializers.ModelSerializer):

    user = UserSerializerForTweet(source='cached_user')

    class Meta:
        model = Tweet
        fields = ('id', 'user', 'created_at', 'content')
Beispiel #3
0
class TweetSerializer(serializers.ModelSerializer):
    user = UserSerializerForTweet(source='cached_user')
    has_liked = serializers.SerializerMethodField()
    comments_count = serializers.SerializerMethodField()
    likes_count = serializers.SerializerMethodField()

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

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

    def get_likes_count(self, obj):
        return RedisHelper.get_count(obj, 'likes_count')

    def get_comments_count(self, obj):
        return RedisHelper.get_count(obj, 'comments_count')
Beispiel #4
0
class CommentSerializer(serializers.ModelSerializer):
    user = UserSerializerForTweet()

    class Meta:
        model = Comment
        fields = (
            'id',
            'tweet_id',
            'user',
            'content',
            'created_at',
            'updated_at',
        )
Beispiel #5
0
class TweetSerializerForDetail(TweetSerializer):
    user = UserSerializerForTweet(source='cached_user')
    comments = CommentSerializerForTweet(source='comment_set', many=True)

    class Meta:
        model = Tweet
        fields = (
            'id',
            'user',
            'created_at',
            'content',
            'comments',
            'comment_count',
            'like_count',
            'has_liked',
            'photo_urls',
        )
Beispiel #6
0
class TweetSerializer(serializers.ModelSerializer):
    user = UserSerializerForTweet(
        source='cached_user')  # who creates this tweet
    comments_count = serializers.SerializerMethodField()
    likes_count = serializers.SerializerMethodField()
    has_liked = serializers.SerializerMethodField()
    photo_urls = serializers.SerializerMethodField()

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

    def get_likes_count(self, obj):
        # select count(*) -> redis get
        # N + 1 queries
        # N 如果是 db queries -> 不可以接受
        # N 如果是 redis/memcached queries -> 可以接受
        return RedisHelper.get_count(obj, 'likes_count')
        # return obj.like_set.count()

    def get_comments_count(self, obj):
        # Django定义的反查机制
        return RedisHelper.get_count(obj, 'comments_count')
        # return obj.comment_set.count()

    def get_has_liked(self, obj):
        # current login user can be obtained from self.context['request'].user
        return LikeService.has_liked(self.context['request'].user, obj)

    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
Beispiel #7
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 #8
0
class TweetSerializer(serializers.ModelSerializer):
    user = UserSerializerForTweet()
    # If user is not defined, user generated by ModelSerializer will be returned
    # as integer. It shows up that serializers(e.g. fields here) can be embedded
    # in serializers(ModelSerializer)
    comments_count = serializers.SerializerMethodField()
    likes_count = serializers.SerializerMethodField()
    has_liked = serializers.SerializerMethodField()
    photo_urls = serializers.SerializerMethodField()

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

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

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

    def get_has_liked(self, obj):
        # current login user has this like
        return LikeService.has_liked(self.context['request'].user, obj)

    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