Ejemplo n.º 1
0
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({'code': 4, 'msg': '用户尚未登录'})

        notifications = Notification.objects.filter(
            to_user_id=request.user.username).order_by('-created_at')

        notification_list = []

        if notifications:
            redis = get_redis()
            ct = ConvertTime()
            notification_list = [{
                'id': notification.id,
                'from_user_id': notification.from_user.username,
                'from_user_avatar': redis.hget('user:{}:detail'.format(notification.from_user.username), 'avatar').decode(),
                'body': notification.body,
                'app': notification.app,
                'viewed': notification.viewed,
                'created_at': ct.convertDatetime(notification.created_at)
            } for notification in notifications]

            notifications.update(viewed=True)

        return JsonResponse({'code': 1, 'msg': notification_list})
Ejemplo n.º 2
0
    def post(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({'code': 4, 'msg': '请先登录再进行操作'})

        data = json.loads(request.body)
        blog_id = data['id']
        reply_id = data['reply_id']
        to_reply = data['to_reply']
        body = data['body'].strip()
        to_user_id = data['to_user_id']

        if len(body) < 1 or len(body) > 1000:
            return JsonResponse({'code': 3, 'msg': '回复内容有效长度区间为[1-300]'})

        if Blog.objects.filter(id=blog_id).count() is not 1:
            return JsonResponse({'code': 5, 'msg': '找不到该博客'})

        if User.objects.filter(username=to_user_id).count() is not 1:
            return JsonResponse({'code': 6, 'msg': '所回复用户不存在'})

        sub_reply = BlogSubReply.objects.create(
            blog_id=blog_id,
            reply_id=reply_id,
            to_reply=to_reply,
            body=body,
            from_user_id=request.user.username,
            to_user_id=to_user_id)

        redis = get_redis()
        ct = ConvertTime()

        sub_reply = {
            'id':
            sub_reply.id,
            'reply_id':
            sub_reply.reply_id,
            'body':
            sub_reply.body,
            'from_user_id':
            sub_reply.from_user_id,
            'from_user_nickname':
            redis.hget("user:{}:detail".format(sub_reply.from_user_id),
                       "nickname").decode(),
            'from_user_avatar':
            redis.hget("user:{}:detail".format(sub_reply.from_user_id),
                       "avatar").decode(),
            'to_user_id':
            sub_reply.to_user_id,
            'created_at':
            ct.convertDatetime(sub_reply.created_at),
            'likes':
            0,
            'liked':
            False
        }
        return JsonResponse({'code': 1, 'msg': sub_reply})
Ejemplo n.º 3
0
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({'code': 2, 'msg': '用户尚未登录'})

        redis = get_redis()
        ct = ConvertTime()
        follows = redis.smembers('user:{}:follows'.format(
            request.user.username))
        follows = [follow.decode() for follow in follows]
        follows.append(request.user.username)
        photos = Photo.objects.annotate(replies_count=Count('replies')).filter(
            author_id__in=follows).filter(
                created_at__gte=datetime.now() -
                timedelta(days=15)).prefetch_related('tags')
        blogs = Blog.objects.annotate(replies_count=Count('replies')).filter(
            author_id__in=follows).filter(
                created_at__gte=datetime.now() -
                timedelta(days=15)).prefetch_related('tags')
        moments = sorted(chain(photos, blogs),
                         key=lambda instance: instance.created_at,
                         reverse=True)
        items = []
        for r in moments:
            item = {}
            item['id'] = r.id
            item['title'] = r.title
            item['app'] = r.app
            item['author_id'] = r.author_id
            item['created_at'] = ct.convertDatetime(r.created_at)
            item['timestamp'] = ct.datetimeToTimeStamp(r.created_at)
            item['tags'] = [tag.name for tag in r.tags.all()]
            item['author'] = redis.hget("user:{}:detail".format(r.author_id),
                                        'nickname').decode()
            item['author_avatar'] = redis.hget(
                "user:{}:detail".format(r.author_id), 'avatar').decode()
            item['replies_count'] = r.replies_count
            item['replies'] = []
            if r.app == 'photo':
                item['url'] = r.url
                item['caption'] = r.caption
                item['likes'] = redis.scard('photo:{}:likes'.format(r.id))
                item['liked'] = redis.sismember('photo:{}:likes'.format(r.id),
                                                request.user.username)
            if r.app == 'blog':
                item['body'] = r.body
                item['likes'] = redis.scard('blog:{}:likes'.format(r.id))
                item['liked'] = redis.sismember('blog:{}:likes'.format(r.id),
                                                request.user.username)
            items.append(item)
        return JsonResponse({'code': 1, 'msg': items})
Ejemplo n.º 4
0
    def get(self, request):
        tag = request.GET['tag']
        page = int(request.GET['page'])
        p_from_index = 12 * (page - 1)
        b_from_index = 6 * (page - 1)
        # 探索
        if not tag:
            photos = Photo.objects.annotate(
                replies_count=Count('replies', distinct=True) +
                Count('sub_replies', distinct=True)
            ).filter(~Q(
                author_id=request.user.username))[p_from_index:p_from_index +
                                                  12]
            blogs = Blog.objects.annotate(
                replies_count=Count('replies', distinct=True) +
                Count('sub_replies', distinct=True)
            ).filter(~Q(
                author_id=request.user.username))[b_from_index:b_from_index +
                                                  6]
        else:
            photos = Photo.objects.annotate(
                replies_count=Count('replies', distinct=True) +
                Count('sub_replies', distinct=True)).filter(
                    tags__name=tag)[p_from_index:p_from_index + 12]
            blogs = Blog.objects.annotate(
                replies_count=Count('replies', distinct=True) +
                Count('sub_replies', distinct=True)).filter(
                    tags__name=tag)[b_from_index:b_from_index + 6]

        explores = chain(photos, blogs)
        if explores:
            redis = get_redis()
            items = []
            ct = ConvertTime()
            for r in explores:
                item = {}
                item['id'] = r.id
                item['app'] = r.app
                item['replies_count'] = r.replies_count
                item['timestamp'] = ct.datetimeToTimeStamp(r.created_at)
                if r.app == 'photo':
                    item['url'] = r.url
                    item['likes'] = redis.scard('photo:{}:likes'.format(r.id))
                if r.app == 'blog':
                    item['title'] = r.title
                    item['likes'] = redis.scard('blog:{}:likes'.format(r.id))
                items.append(item)
            return JsonResponse({'code': 1, 'msg': items})
        return JsonResponse({'code': 2, 'msg': '暂无数据'})
Ejemplo n.º 5
0
    def get(self, request, id):
        redis = get_redis()
        ct = ConvertTime()
        try:
            blog = Blog.objects.get(pk=id)

            redis_blog_user_key = "user:{}:detail".format(blog.author_id)

            blog = {
                'id':
                blog.id,
                'author_id':
                blog.author_id,
                'title':
                blog.title,
                'body':
                blog.body,
                'tags': [tag.name for tag in blog.tags.all()],
                'created_at':
                ct.convertDatetime(blog.created_at),
                'author':
                redis.hget(redis_blog_user_key, 'nickname').decode(),
                'author_avatar':
                redis.hget(redis_blog_user_key, 'avatar').decode(),
                'author_follows':
                redis.scard("user:{}:follows".format(blog.author_id)),
                'author_fans':
                redis.scard("user:{}:fans".format(blog.author_id)),
                'replies_count':
                blog.replies.count() + blog.sub_replies.count(),
                'likes':
                redis.scard('blog:{}:likes'.format(blog.id)),
                'liked':
                redis.sismember('blog:{}:likes'.format(blog.id),
                                request.user.username)
                if request.user.is_authenticated else False
            }

            return JsonResponse({'code': 1, 'msg': blog, 'addition': ['']})
        except exceptions.ObjectDoesNotExist:
            return JsonResponse({'code': 2, 'msg': '找不到该博客'})
Ejemplo n.º 6
0
    def get(self, request):
        reply_id = request.GET['id']
        page = int(request.GET['page'])
        from_index = 10 * (page - 1)
        replies = BlogSubReply.objects.filter(
            reply_id=reply_id)[from_index:from_index + 10]
        if replies:
            redis = get_redis()
            ct = ConvertTime()
            replies = [{
                'id':
                reply.id,
                'reply_id':
                reply.reply_id,
                'body':
                reply.body,
                'from_user_id':
                reply.from_user_id,
                'from_user_nickname':
                redis.hget("user:{}:detail".format(reply.from_user_id),
                           "nickname").decode(),
                'from_user_avatar':
                redis.hget("user:{}:detail".format(reply.from_user_id),
                           "avatar").decode(),
                'to_user_id':
                reply.to_user_id,
                'created_at':
                ct.convertDatetime(reply.created_at),
                'likes':
                redis.scard('bsreply:{}:likes'.format(reply.id)),
                'liked':
                redis.sismember('bsreply:{}:likes'.format(reply.id),
                                request.user.username)
                if request.user.is_authenticated else False
            } for reply in replies]
        else:
            replies = []

        return JsonResponse({'code': 1, 'msg': replies})
Ejemplo n.º 7
0
    def get(self, request, id):
        try:
            photo = Photo.objects.get(pk=id)

            redis = get_redis()
            ct = ConvertTime()

            photo = {
                'id':
                photo.id,
                'author_id':
                photo.author_id,
                'url':
                photo.url,
                'title':
                photo.title,
                'caption':
                photo.caption,
                'tags': [tag.name for tag in photo.tags.all()],
                'author':
                redis.hget("user:{}:detail".format(photo.author_id),
                           "nickname").decode(),
                'author_avatar':
                redis.hget("user:{}:detail".format(photo.author_id),
                           "avatar").decode(),
                'created_at':
                ct.convertDatetime(photo.created_at),
                'replies_count':
                photo.replies.count() + photo.sub_replies.count(),
                'likes':
                redis.scard("photo:{}:likes".format(photo.id)),
                'liked':
                redis.sismember("photo:{}:likes".format(photo.id),
                                request.user.username)
                if request.user.is_authenticated else False
            }
            return JsonResponse({'code': 1, 'msg': photo})
        except exceptions.ObjectDoesNotExist:
            return JsonResponse({'code': 2, 'msg': '找不到该图片'})