Exemple #1
0
def create_city(state_id):
    """Returns the new State with the status code 201"""
    my_state = storage.get('State', state_id)
    if my_state is None:
        abort(404)
    if not request.json:
        abort(400, 'Not a JSON')
    if 'name' not in request.json:
        abort(400, 'Missing name')
    my_city = city.City(name=request.json.get('name', ""), state_id=state_id)
    storage.new(my_city)
    my_city.save()
    return make_response(jsonify(my_city.to_dict()), 201)
Exemple #2
0
def post_city(state_id):
    """ Add a new city based in state_id """
    content = request.get_json()
    if content is None:
        abort(400, 'Not a JSON')
    if 'name' not in content:
        abort(400, 'Missing name')
    one_state = storage.get(state.State, state_id)
    if one_state is None:
        abort(404)
    new_city = city.City()
    new_city.name = content['name']
    new_city.state_id = state_id
    storage.new(new_city)
    storage.save()
    return (jsonify(new_city.to_dict()), 201)
Exemple #3
0
def add_city(state_id):
    """add a city to a state"""
    data = {}
    state = storage.get('State', state_id)
    if state is None:
        abort(404)
    data = request.get_json(silent=True)
    if data is None:
        abort(400, "Not a JSON")
    if 'name' not in data.keys():
        abort(400, "Missing name")
    new_city = city.City(state_id=state.id)
    for k, v in data.items():
        setattr(new_city, k, v)
    storage.new(new_city)
    storage.save()
    return jsonify(new_city.to_dict()), 201
Exemple #4
0
def do_create_city(request, state_id):
    """
        Creates a city object
        Return: new city object
    """
    do_check_id(state.State, state_id)
    body_request = request.get_json()
    if (body_request is None):
        abort(400, 'Not a JSON')
    try:
        city_name = body_request['name']
    except KeyError:
        abort(400, 'Missing name')
    new_city = city.City(name=city_name, state_id=state_id)
    storage.new(new_city)
    storage.save()
    return jsonify(new_city.to_dict())
Exemple #5
0
all_objs = storage.all()
print("-- Reloaded objects --")
for obj_id in all_objs.keys():
    obj = all_objs[obj_id]
    print(obj)

print("-- Create a new User --")
my_user = user.User()
my_user.first_name = "Betty"
my_user.last_name = "Holberton"
my_user.email = "*****@*****.**"
my_user.password = "******"
my_user.save()
print(my_user)

my_city = city.City()
my_city.name = "San Francisco"
my_city.state_id = "101"
my_city.save()
print(my_city)

my_state = state.State()
my_state.name = "california"
my_state.save()
print(my_state)

my_place = place.Place()
my_place.number_rooms = 3
my_place.max_guest = 2
my_place.price_by_night = 100
my_place.save()