示例#1
0
def create_user():
    # TODO: add a test to ensure that the request body contains JSON and
    # the JSON has a member named 'email'. Abort with status 400 if the
    # test fails.
    if ....

    # TODO: get the email from the request JSON and assign it to a variable
    # named 'email'
    email = ....
    app.logger.info('Creating 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 creation of the user to a DAO and
    # assign the new user to the variable 'user'
    # (no code change required)
    user = rest_server_dao.create_user(email, first_name, middles, last_name,
                                       street, post_code, city, state, country)

    # TODO: return two values:
    # 1. a jsonified dictionary with key of 'user' and value of the new user
    # 2. HTTP status 201
    return ....
示例#2
0
def create_user():
    # TODO: add a test to ensure that the request body contains JSON and
    # the JSON has a member named 'email'. Abort with status 400 if the
    # test fails.
    if not request.json or 'email' not in request.json:
        app.logger.error('No email in POST request to create user')
        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('Creating 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 creation of the user to a DAO and
    # assign the new user to the variable 'user'
    # (no code change required)
    user = rest_server_dao.create_user(email, first_name, middles, last_name,
                                       street, post_code, city, state, country)

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