def create_amenity(): """ Create a amenity """ data = request.form try: if 'name' not in data: raise KeyError("'name'") check_amenity = Amenity.select(). where(Amenity.name == data['name']) if check_amenity: return {'code': 10003, 'msg': 'Name already exists'}, 409 new = Amenity.create( name=data['name'] ) res = {} res['code'] = 201 res['msg'] = "Amenity was created successfully" return res, 201 except KeyError as e: res = {} res['code'] = 40000 res['msg'] = 'missing parameters' return res, 400 except Exception as error: response = {} response['code'] = 409 response['msg'] = str(error) return response, 409
def create_amenities(): data = request.form check_amenity = Amenity.select(). where(Amenity.name == data['name']) if check_amenity: return {'code': 10003, 'msg': 'Name already exists'}, 409 amenity = Amenity.create( name = data['name'] ) return {'code': 201, 'msg': 'Amenity created successfully'}, 201
def app_amenities(): if request.method == "GET": try: query = Amenity.select() if not query.exists(): return jsonify({"code": 404, "msg": "not found"}), 404 return ListStyles.list(query, request), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404 elif request.method == "POST": try: new = Amenity.create(name=request.form['name']) return jsonify(new.to_dict()), 201 except: return jsonify({"code": 10003, "msg": "Name already exists"}), 409
def handle_amenity(): '''Returns all amenities as JSON objects in an array with a GET request. Adds an amenity with a POST request. ''' if request.method == 'GET': list = ListStyle().list(Amenity.select(), request) return jsonify(list), 200 elif request.method == 'POST': try: Amenity.select().where(Amenity.name == request.form['name']).get() return jsonify(code=10003, msg="Name already exists"), 409 except Amenity.DoesNotExist: '''Check that all the required parameters are made in request.''' required = set(["name"]) <= set(request.values.keys()) if required is False: return jsonify(msg="Missing parameter."), 400 amenity = Amenity.create(name=request.form['name']) return jsonify(amenity.to_dict()), 200
def amenities(): if request.method == 'GET': amenities_list = [] amenities = Amenity.select() for amenity in amenities: amenities_list.append(amenity.to_hash()) return jsonify(amenities_list) elif request.method == 'POST': amenities = Amenity.select() for amenity in amenities: if amenity.name == request.form['name']: message = { 'code': 10003, 'msg': 'Amenity already exists', } res = jsonify(message) res.status_code = 409 return res else: new_amenity = Amenity.create(name=request.form['name']) return jsonify(new_amenity.to_hash())
def create_amenity(): """ Create a new amenity Create a new amenity in the database. --- tags: - Amenity parameters: - name: name in: form type: string required: True description: Name of the amenity responses: 201: description: Amenity was created schema: id: post_success required: - code - id - msg properties: code: type: integer description: Response code from the API default: 201 id: type: integer description: ID of the newly created record default: 1 msg: type: string description: Message about record creation default: "created successfully" 400: description: Issue with amenity request 409: description: Amenity already exists 500: description: The request was not able to be processed """ data = {} if request.json: data = request.json else: for key in request.form.keys(): for value in request.form.getlist(key): data[key] = value try: ''' Check if name for amenity was given ''' if not 'name' in data: raise KeyError("'name'") ''' Check if name is a string ''' if not type_test(data['name'], 'string'): raise TypeError("amenity 'name' must be a string value") ''' Check if amenity already exists ''' query = Amenity.select().where(Amenity.name == data['name']) if query.exists(): raise ValueError('amenity already exists') ''' Create new amenity ''' new = Amenity.create( name = data['name'] ) res = {} res['code'] = 201 res['id'] = new.id res['msg'] = "amenity was created successfully" return res, 201 except KeyError as e: res = {} res['code'] = 40000 res['msg'] = 'missing parameters' return res, 400 except TypeError as e: res = {} res['code'] = 400 res['msg'] = e.message return res, 400 except ValueError as e: res = {} res['code'] = 10003 res['msg'] = e.message return res, 409 except Exception as e: abort(500)