Пример #1
0
def get_thread(thread_id):
    thread_query = SINGLE_THREAD_QUERY_TEMPLATE.format(thread_id)
    json_thread = get_single_json_entity(thread_query)
    if json_thread:
        response = jsonify(json_thread)
    else:
        response = error_response(404)
    return response
Пример #2
0
def get_post(post_id):
    post_query = SINGLE_POST_QUERY_TEMPLATE.format(post_id)
    json_post = get_single_json_entity(post_query)
    if json_post:
        response = jsonify(json_post)
    else:
        response = error_response(404)
    return response
Пример #3
0
def get_forum(forum_id):
    json_forum = get_single_json_entity(
        FULL_FORUM_QUERY_TEMPLATE.format(forum_id))
    if json_forum:
        response = jsonify(json_forum)
    else:
        response = error_response(404)
    return response
Пример #4
0
def get_user(user_id):
    json_user = get_single_json_entity(
        FULL_USER_QUERY_TEMPLATE.format(user_id)
    )
    if json_user:
        response = jsonify(json_user)
    else:
        response = error_response(404)
    return response
Пример #5
0
def update_user(user_id):
    if token_auth.current_user().id != user_id:
        abort(403)
    query = A_USER_QUERY_TEMPLATE.format('id', user_id)
    json_user = get_single_json_entity(query)
    if not json_user:
        return error_response(404)
    request_data = request.get_json() or {}

    mutable_field_names = ['username', 'email', 'common_name']
    fields_to_update = {
        k: v for k, v in request_data.items() if k in mutable_field_names
    }
    if not fields_to_update:
        return bad_request('must include username, email or common_name')

    if 'username' in fields_to_update:
        name_query = A_USER_QUERY_TEMPLATE.format(
            'username',
            request_data['username']
        )
        query_result_proxy = database.session.execute(name_query)
        new_username_is_not_unique = bool([r for r in query_result_proxy])
        if new_username_is_not_unique:
            return bad_request(f'please use a different username')

    update_query_template = "UPDATE users SET {} WHERE users.id = {}"
    updating_set = ','.join(
        [f"{f} = '{v}'" for f, v in fields_to_update.items()]
    )
    update_query = update_query_template.format(updating_set, user_id)
    database.session.execute(update_query)
    query = FULL_USER_QUERY_TEMPLATE.format(user_id)
    updated_user = get_single_json_entity(query)
    database.session.commit()
    return jsonify(updated_user)
Пример #6
0
def remove_post(post_id):
    post_query = SINGLE_POST_QUERY_TEMPLATE.format(post_id)
    json_post = get_single_json_entity(post_query)
    if not json_post:
        return error_response(404)
    if token_auth.current_user().id != json_post['user_id']:
        abort(403)

    delete_post_query = f"""
    UPDATE post SET deleted = TRUE WHERE post.id = '{post_id}'
    """
    database.session.execute(delete_post_query)
    database.session.commit()
    response = jsonify({'status': 'OK'})
    return response
Пример #7
0
def get_user_posts(user_id):
    app.logger.debug(f'Receive request: {request.data}')
    query = A_USER_QUERY_TEMPLATE.format('id', user_id)
    json_user = get_single_json_entity(query)
    if not json_user:
        return error_response(404)

    posts_query = f"""
    SELECT post.id, post.text, post.creation_timestamp, post.user_id FROM post 
    WHERE post.user_id = {user_id} AND post.deleted = FALSE
    """
    query_result_proxy = database.session.execute(posts_query)
    database.session.commit()
    posts = [{k: v for k, v in row.items()} for row in query_result_proxy]
    response = jsonify({'user_posts': posts})
    return response
Пример #8
0
def get_forum_threads(forum_id):
    app.logger.debug(f'Receive request: {request.data}')
    query = A_FORUM_QUERY_TEMPLATE.format('id', forum_id)
    json_forum = get_single_json_entity(query)
    if not json_forum:
        return error_response(404)

    threads_query = f"""
    SELECT * FROM thread 
    WHERE thread.forum_id = {forum_id} AND thread.deleted = FALSE
    """
    query_result_proxy = database.session.execute(threads_query)
    database.session.commit()
    threads = [{k: v for k, v in row.items()} for row in query_result_proxy]
    response = jsonify({'forum_threads': threads})
    return response
Пример #9
0
def remove_thread(thread_id):
    thread_query = SINGLE_THREAD_QUERY_TEMPLATE.format(thread_id)
    json_thread = get_single_json_entity(thread_query)
    if not json_thread:
        return error_response(404)
    if token_auth.current_user().id != json_thread['creator_id']:
        abort(403)

    delete_thread_query = f"""
    UPDATE thread SET deleted = TRUE WHERE thread.id = '{thread_id}';
    UPDATE post SET deleted = TRUE, deleted_by_thread = TRUE 
    WHERE post.thread_id = '{thread_id}' AND post.deleted = FALSE
    """
    database.session.execute(delete_thread_query)
    database.session.commit()
    response = jsonify({'status': 'OK'})
    return response