def create_state(): """ Create a state """ data = request.form try: if 'name' not in data: raise KeyError('name') state_check = State.select().where(State.name == data['name']) new = State.create(name=data['name']) res = {} res['code'] = 201 res['msg'] = "State was created successfully" return res, 201 except KeyError as e: response = {} response['code'] = 40000 response['msg'] = 'Missing parameters' return response, 400 except Exception as e: response = {} response['code'] = 10001 response['msg'] = "State already exists" return response, 409
def states(): if request.method == 'GET': #get list states states_list = [] states = State.select() for state in states: states_list.append(state.to_hash()) return jsonify(states_list) elif request.method == 'POST': #create new state post_name = request.form['name'] states = State.select() for state in states: if state.name == post_name: message = { 'code': 10001, 'msg': 'State already exists', } res = jsonify(message) res.status_code = 409 return res else: new_state = State.create(name=post_name) return jsonify(new_state.to_hash())
def handle_states(): '''Returns all the states from the database as JSON objects with a GET request, or adds a new state to the database with a POST request. Refer to exception rules of peewee `get()` method for additional explanation of how the POST request is handled: http://docs.peewee-orm.com/en/latest/peewee/api.html#SelectQuery.get ''' if request.method == 'GET': list = ListStyle().list(State.select(), request) return jsonify(list), 200 elif request.method == 'POST': params = request.values '''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 try: State.select().where(State.name == request.form['name']).get() return jsonify(code=10001, msg="State already exists"), 409 except State.DoesNotExist: state = State.create(name=request.form['name']) return jsonify(state.to_dict()), 201
def create_state(): data = request.form name_check = State.select().where(State.name == data['name']) if name_check: return {'code': 10001, 'msg': 'State already exists'}, 409 state = State.create( name = data['name'] ) return {'code': 201,'msg': 'State was created successfully'}, 201
def create_new_state(): post_data = request.values if 'name' not in post_data: return {'code':400, 'msg':'bad request'}, 400 state_query = State.select().where(State.name == post_data['name']) if state_query.exists(): out = {'code': 10001, 'msg': 'State already exists'} return out, 409 try: state_row = State.create(name=post_data['name']) return state_row.to_hash() except: return {'code':500, 'msg':'database connection error'}, 500
def app_states(): if request.method == "GET": try: query = State.select() return ListStyles.list(query, request), 200 except State.DoesNotExist: return jsonify({"code": 404, "msg": "No tables exist yet"}), 404 elif request.method == "POST": try: new = State.create(name=str(request.form['name'])) return jsonify(new.to_dict()), 201 except: return jsonify({"code": 10001, "msg": "State already exists"}), 409
def state_create_modify(): if request.method == 'GET': state_list = State.select() order_values = [i.to_hash() for i in state_list] return jsonify(order_values) elif request.method == 'POST': check_state_name = State.select().where( State.name == request.form['name']) if check_state_name: error_msg = {'code': 10001, 'msg': 'State already exists'} return jsonify(error_msg) else: state = State.create(name=request.form['name']) return jsonify(state.to_hash())
def states(state_id=None): if request.method == "GET": if state_id != None: try: state = State.get(State.id == int(state_id)) return state.to_dict() except: pass return { 'code': 404, 'msg': "not found" }, 404 return { 'data': [state.to_dict() for state in State.select()] }, 200 elif request.method == "POST": name = request.form.get('name') try: new_state = State.create(name=name) except IntegrityError: return { 'code': 10001, 'msg': "State already exists" }, 409 except Exception as e: raise e return new_state.to_dict(), 201 elif request.method == "DELETE": if state_id != None: state = None try: state = State.get(State.id == int(state_id)) except: state = None if state != None: state.delete_instance() return {}, 200 return { 'code': 404, 'msg': "not found" }, 404
def handle_states(): '''Returns all the states from the database as JSON objects with a GET request, or adds a new state to the database with a POST request. Refer to exception rules of peewee `get()` method for additional explanation of how the POST request is handled: http://docs.peewee-orm.com/en/latest/peewee/api.html#SelectQuery.get ''' if request.method == 'GET': list = ListStyle().list(State.select(), request) return jsonify(list), 200 elif request.method == 'POST': params = request.values '''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 try: State.select().where(State.name == request.form['name']).get() return jsonify(code=10001, msg="State already exists"), 409 except State.DoesNotExist: state = State.create(name=request.form['name']) return jsonify(state.to_dict()), 201
def create_state(): """ Create a new state Create a new state in the database --- tags: - State parameters: - name: name in: form type: string required: True description: Name of the state responses: 201: description: State was created schema: $ref: '#/definitions/create_amenity_post_post_success' 400: description: Issue with state request 409: description: State already exists 500: description: The request was not able to be processed """ data = {} for key in request.form.keys(): for value in request.form.getlist(key): data[key] = value try: ''' Check that name key is in data ''' if not 'name' in data: raise KeyError('name') ''' Check that name key is not null ''' if not data['name']: raise TypeError("'name' cannot be NULL") ''' Check that name key value is a string ''' if not type_test(data['name'], 'string'): raise TypeError("'name' must be a string") ''' Check if state already exists ''' query = State.select().where(State.name == data['name']) if query.exists(): raise ValueError('State already exists') new = State.create( name = data['name'] ) res = {} res['code'] = 201 res['id'] = new.id res['msg'] = "State was created successfully" return res, 201 except TypeError as e: response = {} response['code'] = 400 response['msg'] = e.message return response, 400 except ValueError as e: response = {} response['code'] = 10001 response['msg'] = e.message return response, 409 except KeyError as e: response = {} response['code'] = 40000 response['msg'] = 'Missing parameters' return response, 400 except Exception as e: print e.message abort(500)