comment.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:pillar 作者: armadillica 项目源码 文件源码
def edit_comment(user_id, node_id, patch):
    """Edits a single comment.

    Doesn't do permission checking; users are allowed to edit their own
    comment, and this is not something you want to revoke anyway. Admins
    can edit all comments.
    """

    # Find the node. We need to fetch some more info than we use here, so that
    # we can pass this stuff to Eve's patch_internal; that way the validation &
    # authorisation system has enough info to work.
    nodes_coll = current_app.data.driver.db['nodes']
    projection = {'user': 1,
                  'project': 1,
                  'node_type': 1}
    node = nodes_coll.find_one(node_id, projection=projection)
    if node is None:
        log.warning('User %s wanted to patch non-existing node %s' % (user_id, node_id))
        raise wz_exceptions.NotFound('Node %s not found' % node_id)

    if node['user'] != user_id and not authorization.user_has_role('admin'):
        raise wz_exceptions.Forbidden('You can only edit your own comments.')

    # Use Eve to PATCH this node, as that also updates the etag.
    r, _, _, status = current_app.patch_internal('nodes',
                                                 {'properties.content': patch['content'],
                                                  'project': node['project'],
                                                  'user': node['user'],
                                                  'node_type': node['node_type']},
                                                 concurrency_check=False,
                                                 _id=node_id)
    if status != 200:
        log.error('Error %i editing comment %s for user %s: %s',
                  status, node_id, user_id, r)
        raise wz_exceptions.InternalServerError('Internal error %i from Eve' % status)
    else:
        log.info('User %s edited comment %s', user_id, node_id)

    # Fetch the new content, so the client can show these without querying again.
    node = nodes_coll.find_one(node_id, projection={'properties.content_html': 1})
    return status, node
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号