Example #1
0
def reply_comment(create_comment: CreateComment,
                  article_id: str = Path(2, description="博客文章id"),
                  comment_id: str = Path(2, description="评论id"),
                  current_user: User = Depends(get_current_user)):
    content = create_comment.content
    article = Article.filter(Article.id == article_id).first()
    if not article:
        return fail_response('此文章不存在')
    if article.user_id == current_user.uuid:
        # 博主的情况
        comment = Comment.filter(Comment.id == comment_id, Comment.article_id == article_id,
                                 Comment.user_id != current_user.uuid).first()
    else:
        # 用户
        comment = Comment.filter(Comment.id == comment_id, Comment.article_id == article_id,
                                 Comment.user_id != current_user.uuid,
                                 Comment.parent_id != '').first()
    if not comment:
        return fail_response('此评论不存在或原评论你不能进行回复!')
    try:
        Comment.create(article_id=article_id, user_id=current_user.uuid, content=content, parent_id=comment.id)
    except Exception as e:
        db.rollback()
        logger.error(f'评论回复失败,失败原因:{e}')
        return fail_response('评论回复失败')
    return success_response('评论回复成功!')
Example #2
0
def add_comment(create_comment: CreateComment,
                article_id: str = Path(2, description="博客文章id"),
                current_user: User = Depends(get_current_user)):
    content = create_comment.content
    article = Article.filter(Article.id == article_id).first()
    if not article:
        return fail_response('此文章不存在')
    try:
        Comment.create(article_id=article_id, user_id=current_user.uuid, content=content, parent_id=None)
    except Exception as e:
        db.rollback()
        logger.error(f'博客文章评论失败,失败原因:{e}')
        return fail_response('删除博客文章失败')
    return success_response('新增评论成功!')
Example #3
0
def get_comment_list(article_id: str):
    article = Article.filter(Article.id == article_id).first()
    if not article:
        return fail_response('此文章不存在')
    comment_query = Comment.select().order_by(Comment.create_time.desc())
    count = comment_query.count()
    comment_list = [comment.to_dict() for comment in comment_query]
    return success_response({"count": count,
                             "comment_list": comment_list})
Example #4
0
def delete_comment(article_id: str,
                   comment_id: str,
                   current_user: User = Depends(get_current_user)):
    article = Article.filter(Article.id == article_id).first()
    if not article:
        return fail_response('此文章不存在')
    comment = Comment.filter(Comment.article_id == article_id, Comment.id == comment_id,
                             Comment.user_id == current_user.uuid).first()
    if not comment:
        return fail_response('此评论不存在或你没有删除权限')
    try:
        result = comment.delete_instance()
        if not result:
            return fail_response('删除评论失败')
    except Exception as e:
        db.rollback()
        logger.error(f'删除评论失败失败,失败原因:{e}')
        return fail_response('删除评论失败!')
    return success_response('删除评论成功!')
Example #5
0
def update_comment(article_id: str,
                   comment_id: str,
                   create_comment: CreateComment,
                   current_user: User = Depends(get_current_user)):
    content = create_comment.content
    article = Article.filter(Article.id == article_id).first()
    if not article:
        return fail_response('此文章不存在')
    comment = Comment.filter(Comment.article_id == article_id, Comment.id == comment_id,
                             Comment.user_id == current_user.uuid).first()
    if not comment:
        return fail_response('此评论不存在')
    try:
        comment.content = content
        comment.save()
    except Exception as e:
        db.rollback()
        logger.error(f'博客文章评论修改失败,失败原因:{e}')
        return fail_response('博客文章评论修改失败!')
    return success_response('博客文章评论修改成功!')
Example #6
0
    def post(self):
        '''提交评论
        Args:
            content: 
            post_id: 5db17a4f14fc6a9a236c8d63
            reply_id: 5db17a4f14fc6a9a236c8d63 or None
        '''
        args = parser.parse(comment_args, request)
        author = User.objects(id=g.uid).first()
        post = Post.objects(id=args['post_id']).first()

        if not post: return api_abort(404)
        try:
            c = Comment(author=author,
                        content=args['content'],
                        reply=args.get('reply_id', '') or args['post_id'])
            post.comments.append(c)
            post.save()
            return '', 201
        except Exception as e:
            current_app.logger.error(e)
            return api_abort(500)
Example #7
0
    def initdb():
        click.echo('Initializing the database...')
        User.drop_collection()
        Post.drop_collection()
        Question.drop_collection()


        click.echo('Generating the User..')
        # FIXME:
        for i in range(1, 10):
            u = User(
                email = fake.email(),
                username = fake.name(),
                avatar = f'http://192.168.1.106:8000/avatar/r{i}.png'
            )
            u.set_password('helloworld')
            u.save()
        click.echo('Done')

        click.echo('Generating the Post..')
        for i in range(10):
            p = Post(
                title = fake.sentence(),
                content = fake.text(2000),
                category = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django'])[0],
                tags = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django']),
            )
            p.save()
        click.echo('Done')
        
        click.echo('Generating the Comment..')
        for i in Post.objects.all():
            for j in range(5):
                c = Comment(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    )
                i.comments.append(c)
                i.save()
        click.echo('Done')


        click.echo('Generating the Reply..')
        for i in Post.objects.all():
            for j in range(5):
                c = Comment(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    reply = random.choice(i.comments).cid
                )
                i.comments.append(c)
                i.save()

        click.echo('Done')

        click.echo('Generating the Question..')
       
        for j in range(5):
            q = Question(
                title = fake.sentence(),
                author = random.choice(User.objects.all()),
                description = fake.text(200),
                tags = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django']),
            )
            q.save()

        click.echo('Done')

        click.echo('Generating the Answer Comment..')
        for i in Question.objects.all():
            for j in range(5):
                c = Answer(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    )
                i.answer.append(c)
                i.save()
        click.echo('Done')


        click.echo('Generating the Answer Reply..')
        for i in Question.objects.all():
            for j in range(5):
                c = Answer(
                    author = random.choice(User.objects.all()),
                    content = fake.text(200),
                    reply = random.choice(i.answer)._id
                )
                i.answer.append(c)
                i.save()

        click.echo('Done')