Пример #1
0
def create_city(state_id):
    """
    Creates new city. If request body not valid JSON, raises 400
    If state_id not linked to State, raise 404
    If dict does not contain 'name' key, raise 400
    Returns city object with status 201
    """

    if not request.get_json():
        abort(400, "Not a JSON")
    if 'name' not in request.get_json():
        abort(400, "Missing name")

    a = storage.get("State", state_id)
    if a is None:
        abort(404)

    kwargs = request.get_json()
    kwargs['state_id'] = state_id
    my_city = City(**kwargs)

    storage.new(my_city)
    storage.save()

    return jsonify(my_city.to_dict()), 201
Пример #2
0
 def test_to_dict_result(self):
     """ Tests the result of the dict """
     city = City()
     new_dict = city.to_dict()
     self.assertEqual(new_dict["__class__"], "City")
     self.assertEqual(type(new_dict["created_at"]), str)
     self.assertEqual(type(new_dict["updated_at"]), str)
     self.assertEqual(type(new_dict["id"]), str)
Пример #3
0
def post_city(state_id):
    '''
        Create a City object
    '''
    if not request.json:
        return jsonify({"error": "Not a JSON"}), 400
    if 'name' not in request.json:
        return jsonify({"error": "Missing name"}), 400
    new_city = City(**request.get_json())
    new_city.save()
    return jsonify(new_city.to_dict()), 201
Пример #4
0
def cities_post(state_id):
    """Add a city to storage with given dictionary"""
    obj = request.get_json(silent=True)
    if obj is None:
        return "Not a JSON", 400

    if storage.get("State", state_id) is None:
        abort(404)
    if "name" not in obj:
        return "Missing name", 400

    obj["state_id"] = state_id
    obj = City(**obj)
    obj.save()
    return jsonify(obj.to_dict()), 201
Пример #5
0
def post_city(state_id):
    """ Adds a new state to State
    """
    try:
        if request.json:
            if 'name' not in request.json:
                return jsonify({'error': 'Missing name'}), 400
            news = request.get_json().get('name')
            state = storage.get('State', state_id)
            if state is None:
                abort(404)
            newo = City(name=news, state_id=state_id)
            newo.save()
            return jsonify(newo.to_dict()), 201
        return jsonify({'error': 'Not a JSON'}), 400
    except BaseException:
        abort(404)
Пример #6
0
def post_cities(state_id):
    """
    function to create a city
    """
    state = storage.get("State", state_id)
    if state is None:
        abort(404)
    content = request.get_json()
    if not content:
        return (jsonify({"error": "Not a JSON"}), 400)
    if "name" not in content:
        return (jsonify({"error": "Missing name"}), 400)
    content['state_id'] = state.id
    post_city = City(**content)
    post_city.save()

    return (jsonify(post_city.to_dict()), 201)
Пример #7
0
def post_cities(id):
    ''' Post a new city '''
    try:
        content = request.get_json()
    except:
        return (jsonify({"error": "Not a JSON"}), 400)
    name = content.get("name")
    if name is None:
        return (jsonify({"error": "Missing name"}), 400)
    my_state = storage.get('State', id)
    if my_state is None:
        abort(404)
    new_city = City()
    new_city.state_id = id
    new_city.name = name
    new_city.save()

    return (jsonify(new_city.to_dict()), 201)
Пример #8
0
def create_city():
    '''
    Creates a City object.
    '''
    if not request.is_json:
        abort(400, "Not a JSON")
    update = request.get_json()
    if 'name' not in update.keys():
        abort(400, "Missing name")
    check_state = storage.get('State', state_id)
    if check_state is None:
        abort(404)
    new_city = City()
    storage.new(new_city)
    for key, value in update.items():
        new_state.__dict__[key] = value
    storage.save()
    return jsonify(new_city.to_dict())
Пример #9
0
def city_post(state_id):
    """use the post method to create"""
    try:
        if not request.is_json:
            return make_response(jsonify({'error': 'Not a JSON'}), 400)
        req = request.get_json()
    except:
        return make_response(jsonify({'error': 'Not a JSON'}), 400)
    dict_name = req.get("name")
    if dict_name is None:
        return make_response(jsonify({'error': 'Missing name'}), 400)
    state = storage.get("State", state_id)
    if state is None:
        abort(404)
    req["state_id"] = state_id
    new_city = City(**req)
    new_city.save()
    return (jsonify(new_city.to_dict()), 201)
Пример #10
0
def post_city(state_id):
    """Creates a City
    Args:
        state_id: id of the state in uuid format
    Returns:
        jsonified version of created city
    """
    if storage.get("State", state_id) is None:
        abort(404)
    if not request.json:
        return jsonify({"error": "Not a JSON"}), 400
    city_dict = request.get_json()
    if "name" not in city_dict:
        return jsonify({"error": "Missing name"}), 400
    else:
        c_name = city_dict["name"]
        city = City(name=c_name, state_id=state_id)
        for key, value in city_dict.items():
            setattr(city, key, value)
        city.save()
        return jsonify(city.to_dict()), 201
Пример #11
0
def get_cities(state_id):
    """
    Retrieves list of all City objects
    """
    state = storage.get("State", state_id)
    if state is None:
        abort(404)
    if request.method == 'GET':
        return jsonify([
            obj.to_dict() for obj in storage.all("City").values()
            if obj.state_id == state_id
        ])
    if request.method == 'POST':
        if not request.json:
            abort(400, 'Not a JSON')
        if 'name' not in request.json:
            abort(400, 'Missing name')
        data = request.get_json().get('name')
        new_city = City(name=data, state_id=state_id)
        new_city.save()
        return make_response(jsonify(new_city.to_dict()), 201)
Пример #12
0
def create_city(state_id):
    '''
       Creates a new City object and saves it to storage
    '''
    state = storage.get("State", state_id)
    if not state:
        abort(404)

    if not request.json:
        abort(400)
        return jsonify({"error": "Not a JSON"})
    else:
        city_dict = request.get_json()
        if "name" in city_dict:
            city_name = city_dict["name"]
            city = City(name=city_name, state_id=state_id)
            for k, v in city_dict.items():
                setattr(city, k, v)
            city.save()
        else:
            abort(400)
            return jsonify({"error": "Missing name"})
        return jsonify(city.to_dict()), 201
Пример #13
0
def post_cities(state_id):
    """
    City Post method
    """
    content = request.get_json()
    get_state = storage.all("State")
    obj_list = []

    if content is None:
        return jsonify({"error": "Not a JSON"}), 400
    if "name" not in content:
        return jsonify({"error": "Missing name"}), 400

    for state_obj in get_state.values():
        obj_list.append(state_obj)
    for state in obj_list:
        if state.id == state_id:
            new_city = City(**content)
            setattr(new_city, "state_id", state_id)
            storage.new(new_city)
            storage.save()
            return jsonify(new_city.to_dict()), 201
    return jsonify({"error": "Not found"}), 404
Пример #14
0
def cities_of_a_state(state_id):
    '''
        GET: list all cities in a specific state
        POST: add a city to a specific state
    '''
    my_state = storage.get('State', state_id)
    if my_state is None:
        abort(404)
    if request.method == 'POST':
        city_dict = request.get_json()
        if city_dict is None:
            return 'Not a JSON', 400
        if 'name' not in city_dict.keys():
            return 'Missing name', 400
        city_dict['state_id'] = state_id
        my_city = City(**city_dict)
        my_city.save()
        return jsonify(my_city.to_dict()), 201
    my_cities = [
        city.to_dict() for city in storage.all('City').values()
        if city.state_id == state_id
    ]
    return jsonify(my_cities)
Пример #15
0
 def test_to_dict_f(self):
     """ Tests the dictionary that comes from the father class """
     city = City()
     new_dict = city.to_dict()
     self.assertEqual(type(new_dict), dict)