Beispiel #1
0
class CommentSerializer(serializers.ModelSerializer):
    """评论列表
    """

    user = UserBaseSerializer()
    to_user = UserBaseSerializer()

    class Meta:
        model = Comment
        fields = ('id', 'user', 'to_user', 'reply_to', 'text', 'create_at',
                  'parent', 'total_like', 'blog', 'status')
Beispiel #2
0
class LikeListSerializer(serializers.ModelSerializer):

    blog = BlogListSerialzier()
    user = UserBaseSerializer()

    class Meta:
        model = Like
        fields = ('id', 'user', 'create_at', 'blog')
Beispiel #3
0
class AtMessageSerializer(serializers.ModelSerializer):

    blog = BlogListSerialzier()
    user = UserBaseSerializer(read_only=True)

    class Meta:
        model = AtMessage
        fields = ('id', 'blog', 'status', 'create_at', 'user')
Beispiel #4
0
class BaseBlogSerializer(serializers.ModelSerializer):

    topic = TopicSerializer(read_only=True)
    img_list = serializers.ListField()
    at_list = serializers.ListField()
    user = UserBaseSerializer(read_only=True)

    class Meta:
        model = Blog
        fields = blog_base_fields + ['user']
Beispiel #5
0
    def search(self, request):
        """返回增加搜素用户
        """
        q = request.query_params.get('q', '')
        page_num = int(request.query_params.get('page', 1))
        if q and page_num == 1:
            user_qs = mm_User.filter(name__icontains=q)
            total_user = user_qs.count()
            top_ten = user_qs[:10]
            users_serializers = UserBaseSerializer(
                top_ten, many=True, context={'request': request})
            user_data = {'total': total_user, 'data': users_serializers.data}
        else:
            user_data = {'total': 0, 'data': []}

        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        ad_blog_dict = mm_Ad.get_blog_hot_ad()
        if page_num == 1 and self.action == 'index':
            ad_blog_dict = mm_Ad.get_blog_hot_ad()
            if ad_blog_dict:
                ad_blog_ids = {ad_blog.id for ad_blog in ad_blog_dict.values()}
                _page = [obj for obj in page if obj.id not in ad_blog_ids]
                ads = sorted(ad_blog_dict.items())
                for index, blog in ads:
                    if index < 1:
                        continue
                    idx = index - 1
                    if len(_page) >= index:
                        _page.insert(idx, blog)
                page = _page

        serializer = self.get_serializer(page, many=True)

        # 自定义返回
        data = serializer.data
        return Response(
            OrderedDict([
                ('count', self.paginator.page.paginator.count),
                ('next', self.paginator.get_next_link()),
                ('previous', self.paginator.get_previous_link()),
                ('page_count', self.paginator.page.paginator.num_pages),
                ('results', data),
                ('users', user_data),
            ]))
Beispiel #6
0
class TopicSerializer(serializers.ModelSerializer):

    user = UserBaseSerializer(read_only=True)

    class Meta:
        model = Topic
        fields = ('id', 'name', 'status', 'create_at', 'cover', 'total_view',
                  'total_comment', 'user', 'topic_type', 'detail', 'sub_name',
                  'total_blog')
        read_only_fields = [
            'status', 'total_view', 'total_comment', 'user', 'total_blog'
        ]

    def create(self, validated_data):
        request = self.context['request']
        if request.user.is_staff:
            instance = self.Meta.model(user=request.user, **validated_data)
            instance.save()
            return instance
Beispiel #7
0
class BlogListSerialzier(BaseBlogSerializer):
    """博文列表
    """

    user = UserBaseSerializer()
    is_like = serializers.SerializerMethodField()
    origin_blog = BlogSimpleSerializer()

    def get_is_like(self, obj):
        """是否点赞
        """
        user = self.context['request'].user
        if not user.is_authenticated:
            return 0
        if not hasattr(user, '_likes'):
            user._likes = mm_Like.blogs().filter(user=user).values_list(
                'blog_id', flat=True)
        return 1 if obj.id in user._likes else 0

    class Meta:
        model = Blog
        fields = blog_base_fields + ['user', 'is_like', 'origin_blog']
        read_only_fields = blog_readonly_fields
Beispiel #8
0
class ActivityCreateSerializer(serializers.ModelSerializer):

    user = UserBaseSerializer(read_only=True)
    cover_url = serializers.CharField(write_only=True,
                                      allow_blank=True,
                                      required=False)

    rewardplan = RewardPlanCreateSerializer(allow_null=True, required=False)

    class Meta:
        model = Activity
        # rewardplan_fields = ['coin_name', 'coin_logo', 'total_coin', 'total_luckyuser', 'start_time']
        fields = [
            'id', 'user', 'title', 'cover', 'activity_type', 'start_at',
            'end_at', 'ticket_price', 'address', 'coordinates',
            'live_plateform', 'live_address', 'total_user', 'contact_name',
            'contact_info', 'total_view', 'total_registration', 'create_at',
            'content', 'total_collect', 'province_code', 'province_name',
            'city_code', 'city_name', 'district_code', 'district_name',
            'blog_id', 'cover_url', 'summary_id'
        ] + ['rewardplan']
        read_only_fields = ('user', 'total_view', 'total_registration',
                            'total_collect', 'blog_id', 'cover', 'summary_id')