def test_link_amenity_already_in_place(self): """test linking an amenity already in place""" rv = self.app.post('{}/places/{}/amenities/{}/'.format( self.path, self.place.id, self.amenity.id), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), self.amenity_args["name"]) self.assertEqual(json_format.get("id"), self.amenity.id) self.assertIn(self.amenity.id, [a.id for a in storage.get("Place", self.place.id).amenities])
def test_delete_city(self): """test delete a city""" city_args = {"name": "Gaborone", "id": "GA", "state_id": "BO"} city = City(**city_args) city.save() rv = self.app.delete('{}/cities/{}/'.format(self.path, city_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("City", city_args["id"]))
def test_delete_state(self): """test delete a state""" state_args = {"name": "Zanzibar", "id": "ZA"} state = State(**state_args) state.save() rv = self.app.delete('{}/states/{}/'.format(self.path, state_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("State", state_args["id"]))
def test_delete_amenity(self): """test delete a amenity""" amenity_args = {"name": "quokka", "id": "QO"} amenity = Amenity(**amenity_args) amenity.save() rv = self.app.delete('{}/amenities/{}/'.format( self.path, amenity_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("Amenity", amenity_args["id"]))
def test_delete_user(self): """test delete a user""" user_args = {"first_name": "quokka", "id": "QO", "email": "*****@*****.**", "password": "******"} user = User(**user_args) user.save() rv = self.app.delete('{}/users/{}/'.format( self.path, user_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("User", user_args["id"]))
def test_delete_review(self): """test delete a review""" review_args = {"text": "poor cage", "place_id": self.place.id, "user_id": self.user.id, "id": "RCA"} review = Review(**review_args) review.save() rv = self.app.delete('{}/reviews/{}/'.format(self.path, review_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("Review", review_args["id"]))
def test_delete_place(self): """test delete a place""" place_args = {"name": "cage", "city_id": self.city.id, "user_id": self.user.id, "id": "CA"} place = Place(**place_args) place.save() rv = self.app.delete('{}/places/{}/'.format(self.path, place_args["id"]), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNone(storage.get("Place", place_args["id"]))
def test_create_state(self): """test creating a state""" state_args = {"name": "Zanzibar", "id": "ZA2"} rv = self.app.post('{}/states/'.format(self.path), content_type="application/json", data=json.dumps(state_args), follow_redirects=True) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), state_args["name"]) self.assertEqual(json_format.get("id"), state_args["id"]) s = storage.get("State", state_args["id"]) self.assertIsNotNone(s) storage.delete(s)
def test_delete_amenity_in_place(self): """test remove an amenity from a place""" amenity_args = {"name": "bear", "id": "BA"} amenity = Amenity(**amenity_args) self.place.amenities.append(amenity) amenity.save() rv = self.app.delete('{}/places/{}/amenities/{}/'.format( self.path, self.place.id, "BA"), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format, {}) self.assertIsNotNone(storage.get("Amenity", amenity.id)) storage.delete(amenity)
def test_create_amenity(self): """test creating a amenity""" amenity_args = {"name": "quokka", "id": "QO"} rv = self.app.post('{}/amenities/'.format(self.path), content_type="application/json", data=json.dumps(amenity_args), follow_redirects=True) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), amenity_args["name"]) self.assertEqual(json_format.get("id"), amenity_args["id"]) s = storage.get("Amenity", amenity_args["id"]) self.assertIsNotNone(s) storage.delete(s)
def test_create_city(self): """test creating a city""" city_args = {"name": "Gaborone", "id": "GA"} rv = self.app.post('{}/states/{}/cities/'.format( self.path, self.state.id), content_type="application/json", data=json.dumps(city_args), follow_redirects=True) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), city_args["name"]) self.assertEqual(json_format.get("id"), city_args["id"]) s = storage.get("City", city_args["id"]) self.assertIsNotNone(s) storage.delete(s)
def test_link_amenity_place(self): """test linking an amenity to a place""" amenity_args = {"name": "bear", "id": "BA"} amenity = Amenity(**amenity_args) amenity.save() rv = self.app.post('{}/places/{}/amenities/{}/'.format( self.path, self.place.id, amenity.id), follow_redirects=True) self.assertEqual(rv.status_code, 201) json_format = getJson(rv) self.assertEqual(json_format.get("name"), amenity_args["name"]) self.assertEqual(json_format.get("id"), amenity_args["id"]) self.assertIn(self.amenity.id, [a.id for a in storage.get("Place", self.place.id).amenities]) storage.delete(amenity)
def test_create_place(self): """test creating a place""" place_args = {"name": "cage", "user_id": self.user.id, "id": "CA"} rv = self.app.post('{}/cities/{}/places/'.format(self.path, self.city.id), content_type="application/json", data=json.dumps(place_args), follow_redirects=True) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("name"), place_args["name"]) self.assertEqual(json_format.get("id"), place_args["id"]) s = storage.get("Place", place_args["id"]) self.assertIsNotNone(s) self.assertEqual(s.user_id, place_args["user_id"]) storage.delete(s)
def test_create_user(self): """test creating a user""" user_args = {"first_name": "quokka", "id": "QO", "email": "*****@*****.**", "password": "******"} rv = self.app.post('{}/users/'.format(self.path), content_type="application/json", data=json.dumps(user_args), follow_redirects=True) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("first_name"), user_args["first_name"]) self.assertEqual(json_format.get("id"), user_args["id"]) self.assertEqual(json_format.get("email"), user_args["email"]) s = storage.get("User", user_args["id"]) self.assertIsNotNone(s) storage.delete(s)
def test_create_review(self): """test creating a review""" rv = self.app.get('{}/places/{}/reviews/'.format(self.path, self.place.id), follow_redirects=True) review_args = {"text": "cage", "user_id": self.user.id, "id": "RCA"} rv = self.app.post('{}/places/{}/reviews/'.format(self.path, self.place.id), content_type="application/json", data=json.dumps(review_args), follow_redirects=True) self.assertEqual(rv.status_code, 201) self.assertEqual(rv.headers.get("Content-Type"), "application/json") json_format = getJson(rv) self.assertEqual(json_format.get("text"), review_args["text"]) self.assertEqual(json_format.get("id"), review_args["id"]) s = storage.get("Review", review_args["id"]) self.assertIsNotNone(s) self.assertEqual(s.user_id, review_args["user_id"]) storage.delete(s)
def GetReview(review_id): """Retrieves review based on its id for GET HTTP method""" review = storage.get("Review", review_id) if review is None: abort(404) return jsonify(review.to_dict())
def place_by_city(city_id): city_object = storage.get("City", city_id) if not city_object: abort(404) my_places_list = [my_places.to_dict() for my_places in city_object.places] return (jsonify(my_places_list), 200)
def get_place(place_id): """get place information for specified place""" place = storage.get("Place", place_id) if place is None: abort(404) return jsonify(place.to_dict())
def get_place(place_id): """Retrieves a place given its ID""" p = storage.get('Place', place_id) if p is None: abort(404) return jsonify(p.to_dict())
def all_places(place_id): '''retrieves a Place object''' place = storage.get('Place', place_id) if not place: abort(404) return jsonify(place.to_dict()), 200
def r_state_id(state_id): """State object """ state = storage.get("State", state_id) if not state: abort(404) return jsonify(state.to_dict())
def get_city_by_id(city_id): """Retrieve a city object by id""" obj = storage.get("City", city_id) if obj is None: abort(404) return jsonify(obj.to_dict())
def place_by_id(place_id): """ function that gets a plece by the id """ place = storage.get("Place", place_id) if place: return jsonify(place.to_dict()) abort(404)
def place_search(): """get places search with his id""" list_places = [] list_place = [] list_final = [] list_send = [] lista_ids_pl = set() if request.is_json: data = request.get_json() else: msg = "Not a JSON" return jsonify({"error": msg}), 400 if len(data) == 0: for val in storage.all("Place").values(): list_places.append(val) else: if "states" in data and len(data["states"]) > 0: for state_id in data["states"]: place = storage.get("State", state_id) for city in place.cities: for pla in city.places: list_places.append(pla) if "cities" in data and len(data["cities"]) > 0: for city_id in data["cities"]: city = storage.get("City", city_id) for pla in city.places: list_places.append(pla) if "cities" not in data and "states" not in data: for val in storage.all("Place").values(): list_place.append(val) else: for val in list_places: list_place.append(val) if "amenities" in data and len(data["amenities"]) > 0: amenities = set( list(amenid for amenid in data["amenities"] if storage.get('Amenity', amenid))) for place in list_place: plaamen = None if (os.environ.get('HBNB_TYPE_STORAGE') == 'db' and place.amenities): plaamen = list(pla.id for pla in place.amenities) else: if len(place.amenities) > 0: plaamen = place.amenities if (plaamen and all(list(pla in plaamen for pla in amenities))): list_places.append(place) for pla in list_places: lista_ids_pl.add(pla.id) lista_ids_iter = list(lista_ids_pl) for pla in list_places: if pla.id in lista_ids_iter: list_final.append(pla) lista_ids_iter.remove(pla.id) for pl in list_final: llave = pl.to_dict() del llave["amenities"] list_send.append(llave) return jsonify(list_send)
def place_by_id(place_id): """ Retrieves a Place object """ a_place = storage.get('Place', place_id) if a_place is None: abort(404) return jsonify(a_place.to_dict())
def cities_id(city_id): """retrieves a city object""" res = storage.get(City, city_id) if res is None: abort(404) return jsonify(res.to_dict())
def get_amenity(amenity_id): """Retrieves an amenity given its ID""" a = storage.get('Amenity', amenity_id) if a is None: abort(404) return jsonify(a.to_dict())
def route_review_id(review_id): ''' search a place with specific id ''' obj = storage.get(Review, review_id) if obj is None: abort(404) return jsonify(obj.to_dict())
def get_places(place_id): """GET request""" place = storage.get(Place, place_id) if not place: abort(404) return jsonify(place.to_dict())
def retrieve_search(): """ Return a list of places linked to the request data all cities in state and containing all amenities """ js = request.get_json() if js is None or type(js) != dict: return jsonify(error="Not a JSON"), 400 states = js["states"] if "states" in js else [] cities = js["cities"] if "cities" in js else [] amenities = js["amenities"] if "amenities" in js else [] conds = [ True if len(states) == 0 else False, True if len(cities) == 0 else False, True if len(amenities) == 0 else False ] print(conds) if all(conds): print("return all places") places = storage.all(Place) all_places = [] for place in places.values(): all_places.append(place) return Response(json.dumps(all_places, indent=2), 200, mimetype="application/json") s_places = [] if len(states) > 0: for st_id in states: st = storage.get(State, st_id) if st is not None: for ci in st.cities: for pla in ci.places: s_places.append(pla) c_places = [] if len(cities) > 0: for ci_id in cities: ci = storage.get(City, ci_id) if ci is not None: for pl in ci.places: c_places.append(pl) f_places = [] f_places.extend(s_places) f_places.extend(c_places) print("\033[31mStates Places\033[0m") for pl in s_places: print_place(pl, "Obj") print("\033[31mCities Places\033[0m") for pl in c_places: print_place(pl, "Obj") if len(amenities) > 0: f_places = filter_by_amenities(f_places, amenities) print("\033[31mFiltered by Amenities\033[0m") for pl in f_places: print_place(pl, "Obj") all_places = storage.all(Place) f_places.extend(filter_by_amenities(all_places.values(), amenities)) resp = [] for pl in f_places: dic = pl.to_dict() if "amenities" in dic: del dic["amenities"] resp.append(dic) return Response(json.dumps(resp, indent=2), 200, mimetype="application/json")
def get_city(city_id): """ retrieves the city object """ obj = storage.get("City", city_id) if not obj: abort(404) return jsonify(obj.to_dict())
def get_amenity(amenity_id): """GET amenity object based off id""" amenity = storage.get("Amenity", amenity_id) if not amenity: abort(404) return jsonify(amenity.to_dict()), 200
def get_stateId(state_id): """retrieve state objects with id""" state = storage.get('State', state_id) if state is None: abort(404) return jsonify(state.to_dict())
def get_place(place_id): """Retrieve Place object linked to a given id. Raise 404 otherwise.""" place_obj = storage.get(Place, place_id) if place_obj is None: abort(404) return jsonify(place_obj.to_dict())
def getAmenity(amenity_id): ''' get amenity information for specified amenity ''' amenitySelect = storage.get("Amenity", amenity_id) if amenitySelect is None: abort(404) return jsonify(amenitySelect.to_dict())
def get_amenity(amenity_id): """ get amenity object """ obj = storage.get("Amenity", amenity_id) if obj: return jsonify(obj.to_dict()) abort(404)
def get_amenity_id(amenity_id): """Returns a Amenity object""" if storage.get("Amenity", amenity_id): return jsonify(d.to_dict()) else: abort(404)
def view_one_review(review_id): """returns one review""" a_review = storage.get("Review", review_id) if a_review is None: abort(404) return jsonify(a_review.to_dict())
def get_userId(user_id): """retrieve user objects with id""" user = storage.get('User', user_id) if user is None: abort(404) return jsonify(user.to_dict())
def city_by_id(city_id): """Retrieves a City object""" city = storage.get(City, city_id) if city is None: abort(404) return jsonify(city.to_dict())
def get_user_id(user_id): """Display the user matched by id""" user_by_id = storage.get(User, user_id) if user_by_id is not None: return jsonify(user_by_id.to_dict()) abort(404)
def get_state(state_id): """Get a state with a specific id""" state = storage.get(State, state_id) if state is None: abort(404) return jsonify(state.to_dict())
def get_by_id(state_id): """ get state by id """ state = storage.get(State, state_id).to_dict() return jsonify(state)
def test_get(self): """ test get method file storage """ state = State(name="Neiva") state.save() neiva = storage.get(State, state.id) self.assertTrue(state.id == neiva.id)
def get_quote_or_404(quote_id): text = storage.get(QUOTE_TPL.format(quote_id)) return Quote(quote_id, text) if text else abort(404)
def get_city_places(city_id): """Retrieves the list of all Place objects attached to a City""" c = storage.get('City', city_id) if c is None: abort(404) return jsonify([p.to_dict() for p in c.places])