def amenities(): """Handle GET and POST requests to /amenities route. Return a list of all amenities in database in the case of a GET request. Create a new amenity record in the database in the case of a POST request. """ # handle GET requests: # -------------------------------------------------------------------------- if request.method == 'GET': list = ListStyle.list(Amenity.select(), request) return jsonify(list) # handle POST requests: # -------------------------------------------------------------------------- elif request.method == 'POST': try: record = Amenity(name=request.form['name']) record.save() return jsonify(record.to_hash()) # return 409 if amenity with given name already exists except IntegrityError: return json_response( add_status_=False, status_=409, code=10003, msg="Name already exists" )
def create_amenity(): try: new_amenity = Amenity(name=request.form['name']) new_amenity.save() return jsonify(new_amenity.to_dict()) except: return jsonify({'code': 10003, 'msg': 'Name already exists'}), 409
def amenities(): """Handle GET and POST requests to /amenities route. Return a list of all amenities in database in the case of a GET request. Create a new amenity record in the database in the case of a POST request. """ # handle GET requests: # -------------------------------------------------------------------------- if request.method == 'GET': list = [] for record in Amenity.select(): hash = record.to_hash() list.append(hash) return jsonify(list) # handle POST requests: # -------------------------------------------------------------------------- elif request.method == 'POST': try: record = Amenity(name=request.form['name']) record.save() return jsonify(record.to_hash()) # return 409 if amenity with given name already exists except IntegrityError: return json_response(add_status_=False, status_=409, code=10003, msg="Name already exists")
def createAmenityViaPeewee(self): """ Create an amenity record using the API's database/Peewee models. createAmenityViaPeewee returns the Peewee object for the record. This method will not work if the database models are not written correctly. """ record = Amenity(name= 'amenity_name') record.save() return record
def createAmenityViaPeewee(self): """ Create an amenity record using the API's database/Peewee models. createAmenityViaPeewee returns the Peewee object for the record. This method will not work if the database models are not written correctly. """ record = Amenity(name='amenity_name') record.save() return record
def create_asdfstate(): content = request.get_json() if not all(param in content.keys() for param in ["name"]): #ERROR return "Failed: bad input" try: amenity = Amenity() amenity.name = content["name"] amenity.save() except Exception as e: return "Failed" return "Success"
def create_amenity(): """ Create an amenity Creates an amenity based on post parameters. --- tags: - amenity parameters: - name: name in: query type: string description: name of the amenity to create responses: 200: description: Success message schema: id: success_message properties: status: type: number description: status code default: 200 msg: type: string description: Status message default: 'Success' 400: description: Error message schema: id: error_message properties: status: type: number description: status code default: 40000 msg: type: string description: Status message default: 'Missing parameters' """ content = request.get_json(force=True) if not content: return error_msg(400, 400, "Error") if not all(param in content.keys() for param in ["name"]): #ERROR return error_msg(400, 40000, "Missing parameters") try: amenity = Amenity() amenity.name = content["name"] amenity.save() except Exception as e: return error_msg(400, 400, "Error") return error_msg(200, 200, "Success")
def amenities(): if request.method == 'GET': amenities = Amenity.select() return ListStyle.list(amenities, request), 200 elif request.method == 'POST': try: if "name" not in request.form: return json_response(status_=400, msg="missing parameters", code=40000) test = Amenity.select().where(Amenity.name == request.form["name"]) if test.wrapped_count() > 0: return json_response(status_=409, code=10002, msg="place already exists with this name") amenity = Amenity(name=request.form["name"]) amenity.save() return jsonify(amenity.to_dict()), 201 except IntegrityError: return json_response(status_=409, msg="Name already exists", code=10003)
def test_Place_amenities(self): new_state = State(name='California') new_state.save() new_city = City(name='San Francisco', state=1) new_city.save() # Creating a new users user1 = User(first_name='Jon', last_name='Snow', email='jon@snow', password='******') user1.save() # Creating Place new_place = Place(owner=1, city=1, name="Steven", description="house", number_rooms=3, number_bathrooms=2, max_guest=3, price_by_night=100, latitude=37.774929, longitude=-122.419416) new_place.save() # Creating an amenity new_amenity = Amenity(name="amenity1") new_amenity.save() # Creating a place amenity new_place_amenity = PlaceAmenities(place=1, amenity=1) new_place_amenity.save() # Creating amenity test for different user new_state = State(name='Oregon') new_state.save() new_city = City(name='San Francisco', state=1) new_city.save() # Creating a new users 2 user1 = User(first_name='Jon', last_name='Snow', email='jon+1c@snow', password='******') user1.save() # Creating Place 2 new_place = Place(owner=2, city=2, name="Steven", description="house", number_rooms=3, number_bathrooms=2, max_guest=3, price_by_night=100, latitude=37.774929, longitude=-122.419416) new_place.save() # Creating an amenity 2 new_amenity = Amenity(name="amenity2") new_amenity.save() # Creating a place amenity 1 new_place_amenity = PlaceAmenities(place=1, amenity=2) new_place_amenity.save() # Creating a place amenity 2 new_place_amenity = PlaceAmenities(place=2, amenity=2) new_place_amenity.save() # testing amenities for a different place get_place_amenity = self.app.get('/places/1/amenities') # assert get_place_amenity.status_code == 200 print get_place_amenity.data # testing amenities for a different place get_place_amenity = self.app.get('/places/2/amenities') assert get_place_amenity.status_code == 200 # testing amenities for a different place that does not exist get_place_amenity = self.app.get('/places/3/amenities') assert get_place_amenity.status_code == 404 # creating a new amenity to a place add_amenity = self.app.post('/places/1/amenities/1') assert add_amenity.status_code == 200 # creating a new amenity to a place that does not exist add_amenity = self.app.post('/places/3/amenities/1') assert add_amenity.status_code == 404 # creating a new amenity that does not exist for a place add_amenity = self.app.post('/places/1/amenities/3') assert add_amenity.status_code == 404 # deleting amenities from a place delete_amenity = self.app.delete('/places/1/amenities/1') assert delete_amenity.status_code == 200