def app_create_delete_amenity(place_id, amenity_id): try: Place.get(Place.id == place_id) except Place.DoesNotExist: return jsonify({"code": 409, "msg": "place id does not exist"}), 409 try: Amenity.get(Amenity.id == amenity_id) except Amenity.DoesNotExist: return jsonify({"code": 409, "msg": "amenity id does not exist"}), 409 if request.method == "POST": if PlaceAmenities.select().where( PlaceAmenities.place == place_id, PlaceAmenities.amenity == amenity_id).exists(): return jsonify({ "code": 409, "msg": "Place already has this amenity" }), 409 PlaceAmenities.create(place=place_id, amenity=amenity_id) return jsonify({"code": 200, "msg": "success"}), 200 elif request.method == "DELETE": try: query = PlaceAmenities.select().where( PlaceAmenities.place == place_id, PlaceAmenities.amenity == amenity_id).get() query.delete_instance() return jsonify({"code": 200, "msg": "success"}), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404
def amenity_id(amenity_id): if request.method == 'GET': amenity = Amenity.get(Amenity.id == amenity_id) return jsonify(amenity.to_hash()) elif request.method == 'DELETE': amenity = Amenity.get(Amenity.id == amenity_id) amenity.delete_instance() return 'Amenity %s deleted \n' % place_id
def app_amenities_id(amenity_id): if request.method == "GET": try: query = Amenity.get(Amenity.id == amenity_id) return ListStyles.list(query, request), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404 elif request.method == "DELETE": try: query = Amenity.get(Amenity.id == amenity_id) query.delete_instance() return jsonify({"code": 200, "msg": "success"}), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404
def amenity_id(amenity_id): """Handle GET, PUT & DELETE requests to /amenities/<amenity_id> route. Return a hash of the appropriate record in the case of a GET request. Delete appropriate record in case of DELETE request. """ # check whether resource exists: # -------------------------------------------------------------------------- try: record = Amenity.get(Amenity.id == amenity_id) # return 404 not found if it does not except Amenity.DoesNotExist: return json_response( add_status_=False, status_=404, code=404, msg="not found" ) # if exception does not arise: # -------------------------------------------------------------------------- # handle GET requests if request.method == 'GET': return jsonify(record.to_hash()) # handle PUT requests elif request.method == "DELETE": record.delete_instance() record.save() return 'deleted booking\n'
def amenity_id(amenity_id): """Handle GET, PUT & DELETE requests to /amenities/<amenity_id> route. Return a hash of the appropriate record in the case of a GET request. Delete appropriate record in case of DELETE request. """ # check whether resource exists: # -------------------------------------------------------------------------- try: record = Amenity.get(Amenity.id == amenity_id) # return 404 not found if it does not except Amenity.DoesNotExist: return json_response(add_status_=False, status_=404, code=404, msg="not found") # if exception does not arise: # -------------------------------------------------------------------------- # handle GET requests if request.method == 'GET': return jsonify(record.to_hash()) # handle PUT requests elif request.method == "DELETE": record.delete_instance() record.save() return 'deleted booking\n'
def delete_amenity(amenity_id): try: amenity = Amenity.get(Amenity.id == amenity_id) amenity.delete_instance() return jsonify({'msg': 'Deleted amenity!'}) except: return jsonify({'code': 404, 'msg': 'not found'}), 404
def subtest_createWithAllParams(self): """ Test proper creation of an amenity record upon POST request to the API with all parameters provided. """ POST_request1 = self.createAmenityViaAPI() self.assertEqual(POST_request1.status[:3], '200') now = datetime.now().strftime('%d/%m/%Y %H:%M') self.assertEqual(Amenity.get(Amenity.id == 1).name, 'amenity_name') self.assertEqual(Amenity.get(Amenity.id == 1).created_at.strftime('%d/%m/%Y %H:%M'), now) self.assertEqual(Amenity.get(Amenity.id == 1).updated_at.strftime('%d/%m/%Y %H:%M'), now) # test that placebook ID for sole record in database is correct self.assertEqual(Amenity.select().get().id, 1)
def get_amenity(id): try: amenity = Amenity.get(Amenity.id == id) except Exception: return {'code': 404, 'msg': 'Amenity not found'}, 404 return amenity.to_hash(), 200
def get_amenity(amenity_id): """ Get the given amenity Return the given amenity in the database. --- tags: - Amenity parameters: - in: path name: amenity_id type: string required: True description: ID of the amenity responses: 200: description: Amenity returned successfully schema: id: Amenity required: - name - id - created_at - updated_at properties: name: type: string description: Name of the amenity default: "Swimming Pool" id: type: number description: id of the amenity default: 1 created_at: type: datetime string description: date and time the amenity was created in the database default: '2016-08-11 20:30:38' updated_at: type: datetime string description: date and time the amenity was updated in the database default: '2016-08-11 20:30:38' 404: description: Amenity was not found 500: description: Request could not be processed """ try: ''' Check if amenity exists ''' query = Amenity.select().where(Amenity.id == amenity_id) if not query.exists(): raise LookupError('amenity_id') ''' Return amenity data ''' amenity = Amenity.get(Amenity.id == amenity_id) return amenity.to_dict(), 200 except LookupError as e: abort(404) except Exception as e: abort(500)
def delete_amenity(id): try: amenity = Amenity.get(Amenity.id == id) except Exception: return {'code': 404, 'msg': 'Amenity not found'}, 404 amenity.delete_instance() return {'code': 200, 'msg': 'Deleted successfully'}, 200
def del_amenity(amenity_id): try: query = Amenity.get(Amenity.id == amenity_id) except Amenity.DoesNotExist: return {"code":404, "msg": "not found"}, 404 out_dict = query.to_dict() query.delete_instance() return out_dict
def get_amenity(amenity_id): """ Get a amenity with id as amenity_id """ try: amenity = Amenity.get(Amenity.id == amenity_id) except Exception: return {'code': 404, 'msg': 'Amenity not found'}, 404 return amenity.to_dict(), 200
def subtest_createWithAllParams(self): """ Test proper creation of an amenity record upon POST request to the API with all parameters provided. """ POST_request1 = self.createAmenityViaAPI() self.assertEqual(POST_request1.status[:3], '200') now = datetime.now().strftime('%d/%m/%Y %H:%M') self.assertEqual(Amenity.get(Amenity.id == 1).name, 'amenity_name') self.assertEqual( Amenity.get(Amenity.id == 1).created_at.strftime('%d/%m/%Y %H:%M'), now) self.assertEqual( Amenity.get(Amenity.id == 1).updated_at.strftime('%d/%m/%Y %H:%M'), now) # test that placebook ID for sole record in database is correct self.assertEqual(Amenity.select().get().id, 1)
def delete_amenity(amenity_id): """ Delete amenity with id as amenity_id """ try: amenity = Amenity.get(Amenity.id == amenity_id) except Exception: return {'code': 404, 'msg': 'Amenity not found'}, 404 amenity = Amenity.delete().where(Amenity.id == amenity_id) amenity.execute() res = {} res['code'] = 201 res['msg'] = "Amenity was deleted successfully" return res, 201
def update(place_id, amenity_id): # Checking if place exist try: Place.get(Place.id == place_id) except Place.DoesNotExist: return jsonify({'code': 404, 'msg': 'Place not found'}), 404 # Checking is Amenity exist try: Amenity.get(Amenity.id == amenity_id) except Amenity.DoesNotExist: return jsonify({'code': 404, 'msg': 'Amenity not found'}), 404 if request.method == "POST": new_place_amenity = PlaceAmenities(place=place_id, amenity=amenity_id) new_place_amenity.save() return jsonify(new_place_amenity.amenity.to_dict()) elif request.method == "DELETE": get_place_a = PlaceAmenities.get(PlaceAmenities.place == place_id and PlaceAmenities.amenity == amenity_id) get_place_a.delete_instance return "amenity deleted"
def update(place_id, amenity_id): # Checking if place exist try: Place.get(Place.id == place_id) except Place.DoesNotExist: return jsonify({'code': 404, 'msg': 'Place not found'}), 404 # Checking is Amenity exist try: Amenity.get(Amenity.id == amenity_id) except Amenity.DoesNotExist: return jsonify({'code': 404, 'msg': 'Amenity not found'}), 404 if request.method == "POST": new_place_amenity = PlaceAmenities(place=place_id, amenity=amenity_id) new_place_amenity.save() return jsonify(new_place_amenity.amenity.to_dict()) elif request.method == "DELETE": get_place_a = PlaceAmenities.get( PlaceAmenities.place == place_id and PlaceAmenities.amenity == amenity_id) get_place_a.delete_instance return "amenity deleted"
def amenities_id(amenity_id): if request.method == 'GET': try: amenity = Amenity.select().where(Amenity.id == amenity_id) except Amenity.DoesNotExist: return json_response(status_=404, msg="Not found") return jsonify(amenity.to_dict()), 200 elif request.method == 'DELETE': try: amenity = Amenity.get(Amenity.id == amenity_id) except Amenity.DoesNotExist: return json_response(status_=404, msg="Not found") amenity.delete_instance() amenity.save() return json_response(status_=200, msg="Amenity deleted")
def find_amenity(amenity_id): try: list = Amenity.get(Amenity.id == amenity_id) return jsonify(list.to_dict()) except: return jsonify({'code': 404, 'msg': 'not found'}), 404
def get_amenity_by_id(amenity_id): try: get_amenity = Amenity.get(Amenity.id == amenity_id) except Amenity.DoesNotExist: return {"code":404, "msg": "not found"}, 404 return get_amenity.to_dict()