Example #1
0
def update_user(email):
    # TODO: add a test to ensure that the request body contains JSON.
    # Abort with status 400 if the test fails.
    ....

    # TODO: get the email from the request JSON and assign it to a variable
    # named 'email'
    email = ....
    app.logger.info('Updating user %s', email)

    # TODO: note how we get the rest of the input data from the request
    # (no code change required)
    first_name = request.json.get('first_name', '')
    middles = request.json.get('middles', '')
    last_name = request.json.get('last_name', '')
    street = request.json['address'].get('street', '')
    post_code = request.json['address'].get('post_code', '')
    city = request.json['address'].get('city', '')
    state = request.json['address'].get('state', '')
    country = request.json['address'].get('country', '')

    # TODO: note how we delegate the update of the user to a DAO and
    # assign the modified user to the variable 'user'
    # (no code change required)
    user = rest_server_dao.update_user(email, first_name, middles, last_name,
                                       street, post_code, city, state, country)

    # TODO: if user is None, abort with HTTP status 404
    ....

    # TODO: return two values:
    # 1. a jsonified dictionary with key of 'user' and value of the new user
    # 2. HTTP status 202
    ....
Example #2
0
def update_user(email):
    # TODO: add a test to ensure that the request body contains JSON.
    # Abort with status 400 if the test fails.
    if not request.json:
        app.logger.error('No JSON in PUT request to update user %s', email)
        abort(400)

    # TODO: get the email from the request JSON and assign it to a variable
    # named 'email'
    email = request.json['email']
    app.logger.info('Updating user %s', email)

    # TODO: note how we get the rest of the input data from the request
    # (no code change required)
    first_name = request.json.get('first_name', '')
    middles = request.json.get('middles', '')
    last_name = request.json.get('last_name', '')
    street = request.json['address'].get('street', '')
    post_code = request.json['address'].get('post_code', '')
    city = request.json['address'].get('city', '')
    state = request.json['address'].get('state', '')
    country = request.json['address'].get('country', '')

    # TODO: note how we delegate the update of the user to a DAO and
    # assign the modified user to the variable 'user'
    # (no code change required)
    user = rest_server_dao.update_user(email, first_name, middles, last_name,
                                       street, post_code, city, state, country)

    # TODO: if user is None, abort with HTTP status 404
    if user is None:
        app.logger.error("User %s not found, can't update", email)
        abort(404)

    # TODO: return two values:
    # 1. a jsonified dictionary with key of 'user' and value of the new user
    # 2. HTTP status 202
    return jsonify({'user': user}), 202  # 202 == Accepted