Example #1
0
def update_post_route(request, topic_id, post_id):
    """
    Update an existing post. Must be one's own post.

    For post:
    - Only the body field may be changed.

    For proposals:
    - Only the name, body, and status fields can be changed.
    - The status can only be changed to declined, and only when
      the current status is pending or blocked.

    For votes:
    - The only fields that can be updated are body and response.
    """

    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    # ## STEP 1) Find existing post instance ## #
    post_ = get_post_facade(db_conn, post_id)
    if not post_:
        return abort(404)
    if post_['user_id'] != current_user['id']:
        return abort(403)
    post_kind = post_['kind']

    # ## STEP 2) Limit the scope of changes ## #
    post_data = request['params']['post']
    if post_kind is 'post':
        post_data = pick(post_data, ('body',))
    elif post_kind is 'proposal':
        post_data = pick(post_data, ('name', 'body', 'status',))
        if (post_data.get('status') != 'declined' or
                post_data.get('status') not in ('pending', 'blocked',)):
            del post_data['status']
    elif post_kind is 'vote':
        post_data = pick(post_data, ('body', 'response',))

    # ## STEP 3) Validate and save post instance ## #
    post_, errors = post_.update(db_conn, post_data)
    if errors:
        errors = prefix_error_names('post.', errors)
        return 400, {
            'errors': errors,
            'ref': 'E4LFwRv2WEJZks7use7TCpww'
        }

    # ## STEP 4) Make updates based on proposal / vote status ## #
    if post_kind == 'proposal':
        update_entity_status(db_conn, post_)
    if post_kind == 'vote':
        proposal = Proposal.get(db_conn, id=post_['replies_to_id'])
        update_entity_status(db_conn, proposal)

    # ## STEP 5) Return response ## #
    return 200, {'post': post_.deliver()}
Example #2
0
def test_get_post_facade(db_conn, posts_table):
    """
    Expect to get a post, and the instance to match the kind.
    """
    posts_table.insert(
        {"id": "fghj4567", "user_id": "abcd1234", "topic_id": "wxyz7890", "body": "abcd", "kind": "post"}
    ).run(db_conn)
    assert isinstance(discuss.get_post_facade("fghj4567"), Post)
Example #3
0
def test_get_post_facade(db_conn, posts_table):
    """
    Expect to get a post, and the instance to match the kind.
    """
    posts_table.insert({
        'id': 'fghj4567',
        'user_id': 'abcd1234',
        'topic_id': 'wxyz7890',
        'body': 'abcd',
        'kind': 'post',
    }).run(db_conn)
    assert isinstance(discuss.get_post_facade(db_conn, 'fghj4567'), Post)
Example #4
0
def update_post_route(request, topic_id, post_id):
    """
    Update an existing post. Must be one's own post.

    For post:
    - Only the body field may be changed.

    For proposals:
    - Only the name, body, and status fields can be changed.
    - The status can only be changed to declined, and only when
      the current status is pending or blocked.

    For votes:
    - The only fields that can be updated are body and response.
    """

    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    # ## STEP 1) Find existing post instance ## #
    post_ = get_post_facade(db_conn, post_id)
    if not post_:
        return abort(404)
    if post_['user_id'] != current_user['id']:
        return abort(403)
    post_kind = post_['kind']

    # ## STEP 2) Limit the scope of changes ## #
    post_data = request['params']['post']
    if post_kind is 'post':
        post_data = pick(post_data, ('body', ))
    elif post_kind is 'proposal':
        post_data = pick(post_data, (
            'name',
            'body',
            'status',
        ))
        if (post_data.get('status') != 'declined'
                or post_data.get('status') not in (
                    'pending',
                    'blocked',
                )):
            del post_data['status']
    elif post_kind is 'vote':
        post_data = pick(post_data, (
            'body',
            'response',
        ))

    # ## STEP 3) Validate and save post instance ## #
    post_, errors = post_.update(db_conn, post_data)
    if errors:
        errors = prefix_error_names('post.', errors)
        return 400, {'errors': errors, 'ref': 'E4LFwRv2WEJZks7use7TCpww'}

    # ## STEP 4) Make updates based on proposal / vote status ## #
    if post_kind == 'proposal':
        update_entity_status(db_conn, post_)
    if post_kind == 'vote':
        proposal = Proposal.get(db_conn, id=post_['replies_to_id'])
        update_entity_status(db_conn, proposal)

    # ## STEP 5) Return response ## #
    return 200, {'post': post_.deliver()}