Пример #1
0
def review_comment(aid, cid):
    '''``PATCH`` |API_URL_BASE|/article/:aid/comment/:cid

    Review a comment. The author is the only one having permission.

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            comment: {
                id: integer,
                nickname: string,
                content: string,
                time: datetime,
                reply_to: integer // maybe null if no references.
            }
        }

        // failed
        {$errors: {reply_to: 'the comment you reply to doesn't not exist.'}}

    Permission required: ``REVIEW_COMMENT``
    '''
    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
        comment = Comment.get((Comment.id == cid) & (Comment.article == aid))
        if not author_id == current_user.get_id():
            raise Comment.DoesNotExist()
    except (Article.DoesNotExist, Comment.DoesNotExist):
        return {'comment_id': '欲回复的评论不存在'}

    comment.reviewed = True
    comment.save()

    event_emitted.send(
        current_app._get_current_object(),
        type='Comment: Review',
        description='comment(%d) has been reviewed by %s.' %
                    (comment.id, current_user.get_id())
    )

    return None, {'comment': comment.to_dict()}
Пример #2
0
def get_a_comment(aid, cid):
    '''``GET`` |API_URL_BASE|/article/:aid/comment/:cid

    Get a comment to an article

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            comment: {
                id: integer,
                nickname: string,
                content: string,
                time: datetime,
                reply_to: integer // maybe null if no references.
            }
        }

        // failed
        {$errors: {comment_id: 'this comment does not exist.'}}

    Permission required: ``READ_COMMENT``
    '''
    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
        comment = Comment.get((Comment.id == cid) & (Comment.article == aid))

        if comment.reviewed is False and current_user.get_id() != author_id:
            raise Comment.DoesNotExist()
    except (Article.DoesNotExist, Comment.DoesNotExist):
        return {'comment_id': '该评论不存在'}

    return None, {'comment': comment.to_dict()}
Пример #3
0
def write_comment(aid, reply_to_cid=None):
    '''``POST`` |API_URL_BASE|/article/:aid/comment/
    ``POST`` |API_URL_BASE|/article/:aid/comment/:cid

    Write a comment to an article.

    :param nickname: **JSON Param** this argument will be ignored
        when current user is authenticated
    :param content: **JSON Param** required

    Response JSON:

    .. code-block:: javascript

        // success
        {
            $errors: null,
            comment: {
                id: integer,
                nickname: string,
                content: string,
                time: datetime,
                reply_to: integer // maybe null if no references.
            }
        }

        // failed
        {
            $errors: {
                comment_id: 'the comment you reply to doesn't not exist.'
                nickname: 'this nickname is illegal',
                content: 'this content is illegal.'
            }
        }

    Permission required: ``WRITE_COMMENT``
    '''
    json = request.get_json()

    try:
        author_id = (Article.select(Article.author)
                            .where(Article.id == aid).get()).author_id
        reply_to = reply_to_cid
        if reply_to is not None:
            if not Comment.exist((Comment.article == aid)
                                 & (Comment.id == reply_to)):
                raise Comment.DoesNotExist()
    except (Article.DoesNotExist, Comment.DoesNotExist):
        return {'comment_id': '欲回复的评论不存在'}

    is_author = author_id == current_user.get_id()

    if is_author:
        nickname = current_user.name
    else:
        nickname = json.get('nickname') or ''
        nickname = nickname.strip()
        if not nickname:
            return {'nickname': '请输入有效的昵称'}

    content = json.get('content') or ''
    content = content.strip()
    if not content:
        return {'content': '请输入有效的内容'}

    reviewed = is_author or not app_config['COMMENT_NEED_REVIEW']

    comment = Comment.create(article=aid, content=content,
                             nickname=nickname, reviewed=reviewed,
                             is_author=is_author, reply_to=reply_to)

    event_emitted.send(
        current_app._get_current_object(),
        type='Comment: Create',
        description='new comment(%d) in article(%s) has been added.' %
                    (comment.id, aid)
    )

    return None, {'comment': comment.to_dict()}