コード例 #1
0
ファイル: comments.py プロジェクト: Igor1py/social-network
def update_comment(comment_id):
    payload = converts_keys(loads(request.data), case='snake')
    check_only_required_payload_props(payload, 'post_id', 'content')
    cookies = request.cookies
    if 'token' not in cookies:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Users.get_user_id(), cookies)
            user_id = cursor.fetchone()['user_id']
            cursor.execute(Comments.get_author_id(), {'id': comment_id})
            author_id = cursor.fetchone()['author_id']
    if user_id != author_id:
        return jsonify(), 401
    with connect(DSN) as connection:
        with connection.cursor(cursor_factory=RealDictCursor) as cursor:
            cursor.execute(Comments.update(), {'id': comment_id, **payload})
            comment = cursor.fetchone()
    return jsonify(converts_keys(comment, case='camel'))