def post_place(city_id): """ Add a new place based in city_id """ s_city = storage.get(city.City, city_id) content = request.get_json() if not s_city: abort(404) if not content: abort(400, "Not a JSON") if 'user_id' not in content: abort(400, "Missing user_id") s_user = storage.get(user.User, content['user_id']) if not s_user: abort(404) if 'name' not in content: abort(400, "Missing name") content['city_id'] = city_id new_place = place.Place(**content) new_place.save() return (jsonify(new_place.to_dict()), 201)
def test_typos(self): ''' check for attributes ''' obj = place.Place() self.assertEqual(type(obj.city_id), str) self.assertEqual(type(obj.user_id), str) self.assertEqual(type(obj.name), str) self.assertEqual(type(obj.description), str) self.assertEqual(type(obj.number_rooms), int) self.assertEqual(type(obj.number_bathrooms), int) self.assertEqual(type(obj.max_guest), int) self.assertEqual(type(obj.price_by_night), int) self.assertEqual(type(obj.latitude), float) self.assertEqual(type(obj.longitude), float) self.assertEqual(type(obj.amenity_ids), list) self.assertEqual(type(obj.id), str) self.assertEqual(type(obj.created_at), datetime) self.assertEqual(type(obj.updated_at), datetime) self.assertEqual(obj.__class__.__name__, 'Place')
def test_verify_attr(self): ''' check for attributes ''' obj = place.Place() self.assertTrue(hasattr(obj, 'city_id')) self.assertTrue(hasattr(obj, 'user_id')) self.assertTrue(hasattr(obj, 'name')) self.assertTrue(hasattr(obj, 'description')) self.assertTrue(hasattr(obj, 'number_rooms')) self.assertTrue(hasattr(obj, 'number_bathrooms')) self.assertTrue(hasattr(obj, 'max_guest')) self.assertTrue(hasattr(obj, 'price_by_night')) self.assertTrue(hasattr(obj, 'latitude')) self.assertTrue(hasattr(obj, 'longitude')) self.assertTrue(hasattr(obj, 'amenity_ids')) self.assertTrue(hasattr(obj, 'id')) self.assertTrue(hasattr(obj, 'created_at')) self.assertTrue(hasattr(obj, 'updated_at')) self.assertEqual(obj.__class__.__name__, 'Place')
def create_place(city_id): """ create a Place """ the_city = storage.get('City', city_id) if the_city is None: abort(404) if not request.json: abort(400, 'Not a JSON') if 'user_id' not in request.json: abort(400, 'Missing user_id') if 'name' not in request.json: abort(400, 'Missing name') my_user = storage.get('User', request.json.get('user_id', "")) if my_user is None: abort(404) req = request.get_json(silent=True) req['city_id'] = city_id the_place = place.Place(**req) storage.new(the_place) the_place.save() return make_response(jsonify(the_place.to_dict()), 201)
def post_place_obj(city_id): """add new place object""" dic = {} if storage.get("City", city_id) is None: abort(404) dic = request.get_json(silent=True) if dic is None: abort(400, "Not a JSON") if "user_id" not in dic.keys(): abort(400, "Missing user_id") if storage.get("User", dic["user_id"]) is None: abort(404) if "name" not in dic.keys(): abort(400, "Missing name") new_place = place.Place() setattr(new_place, "city_id", city_id) for k, v in dic.items(): setattr(new_place, k, v) storage.new(new_place) storage.save() return jsonify(new_place.to_dict()), 201
def do_create_place(request, city_id): """ Creates a place object Return: new place object """ do_check_id(city.City, city_id) body_request = request.get_json() if (body_request is None): abort(400, 'Not a JSON') try: user_id = body_request['user_id'] except KeyError: abort(400, 'Missing user_id') do_check_id(user.User, user_id) try: place_name = body_request['name'] except KeyError: abort(400, 'Missing name') new_place = place.Place(name=place_name, city_id=city_id, user_id=user_id) storage.new(new_place) storage.save() return jsonify(new_place.to_dict())
def test_kwargs(self): """Test the class - BaseModel passing kwargs """ dictonary = {'id': '662a23b3-abc7-4f43-81dc-64c000000c00'} user1 = place.Place(**dictonary) self.assertTrue(issubclass(user1.__class__, BaseModel))
def test_subClass(self): '''Check if object have inheritance of the superclass''' obj = place.Place() self.assertTrue(issubclass(type(obj), BaseModel))
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() print(my_place) my_amenity = amenity.Amenity() my_amenity.name = "House Keeping" my_amenity.save() print(my_amenity) my_review = review.Review() my_review.text = "Its a good place." my_review.save() print(my_review)