def test_to_dict_result(self): """ Tests the result of the dict """ state = State() new_dict = state.to_dict() self.assertEqual(new_dict["__class__"], "State") self.assertEqual(type(new_dict["created_at"]), str) self.assertEqual(type(new_dict["updated_at"]), str) self.assertEqual(type(new_dict["id"]), str)
def states_post(): """Add a state to storage with given dictionary""" obj = request.get_json(silent=True) if obj is None: return "Not a JSON", 400 if "name" not in obj: return "Missing name", 400 obj = State(**obj) obj.save() return jsonify(obj.to_dict()), 201
def post_state(): """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) new_state = State(**req) return(jsonify(new_state.to_dict()), 201)
def post_states(): content = request.get_json() if content is None: return (jsonify({"error": "Not a JSON"}), 400) name = content.get("name") if name is None: return (jsonify({"error": "Missing name"}), 400) new_state = State(**content) new_state.save() return (jsonify(new_state.to_dict()), 201)
def create_state(): ''' Creates a State. ''' if not request.is_json: abort(400, "Not a JSON") update = request.get_json() if 'name' not in update.keys(): abort(400, "Missing name") new_state = State() storage.new(new_state) for key, value in update.items(): new_state.__dict__[key] = value storage.save() return jsonify(new_state.to_dict()), 201
def post_state(): """ 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') newo = State(name=news) newo.save() return jsonify(newo.to_dict()), 201 else: return jsonify({'error': 'Not a JSON'}), 400 except: abort(404)
def all_states(): ''' GET: list all states POST: create a new state ''' if request.method == 'POST': state_dict = request.get_json() if state_dict is None: return 'Not a JSON', 400 if 'name' not in state_dict.keys(): return 'Missing name', 400 my_state = State(**state_dict) my_state.save() return jsonify(my_state.to_dict()), 201 my_states = [state.to_dict() for state in storage.all('State').values()] return jsonify(my_states)
def post_states(): """ post method """ content = request.get_json() if content is None: return jsonify({"error": "Not a JSON"}), 400 if "name" not in content: return jsonify({"error": "Missing name"}), 400 exclude = ['id', 'created_at', 'updated_at'] for e in exclude: content.pop(e, None) new_state = State(**content) storage.new(new_state) storage.save() return jsonify(new_state.to_dict()), 201
def post_states(): """ function to add states """ content = request.get_json() if content is None: return (jsonify({"error": "Not a JSON"}), 400) name = content.get("name") if name is None: return (jsonify({"error": "Missing name"}), 400) add_state = State() add_state.name = name add_state.save() return (jsonify(add_state.to_dict()), 201)
def get_states(): """ Retrieves list of all State objects """ if request.method == 'GET': return jsonify( [obj.to_dict() for obj in storage.all("State").values()]) 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_state = State(name=data) new_state.save() return make_response(jsonify(new_state.to_dict()), 201)
def posting_states(): """ Creating a new state object Return: jsonified dicitonary of the new object or error 400 and 404 """ try: if not request.json: return jsonify({"error": "Not a JSON"}), 400 data = request.get_json() if "name" not in data: return jsonify({"error": "Missing name"}), 400 new_obj = State(name=data["name"]) new_obj.save() return jsonify(new_obj.to_dict()), 201 except Exception: abort(404)
def create_state(): """ Creates new state. 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 state object with status 201 """ try: state = request.get_json() if state.get("name") is None: return jsonify({"error": "Missing name"}), 400 except: return jsonify({"error": "Not a JSON"}), 400 state = State(**state) storage.save() return jsonify(state.to_dict()), 201
def create_state(): ''' Creates a new State object and saves it to storage ''' if not request.json: abort(400) return jsonify({"error": "Not a JSON"}) else: state_dict = request.get_json() if "name" in state_dict: state_name = state_dict["name"] state = State(name=state_name) for k, v in state_dict.items(): setattr(state, k, v) state.save() else: abort(400) return jsonify({"error": "Missing name"}) return jsonify(state.to_dict()), 201
def test_to_dict_f(self): """ Tests the dictionary that comes from the father class """ state = State() new_dict = state.to_dict() self.assertEqual(type(new_dict), dict)