def places_in_state(state_id): try: State.get(State.id == state_id) except State.DoesNotExist: return jsonify({'code': 404, 'msg': "State does not exist in the database"}), 404 query = Place.select().join(City).join(State).where(State.id == state_id) return ListStyles.list(query, request), 200
def modify_state(state_id): state = State.get(State.id == state_id) if request.method == "GET": return jsonify(state.to_hash()) elif request.method == 'DELETE': state_info = State.get(State.id == state_id) state_info.delete_instance() state_info.save() return jsonify(msg="State deleted")
def state_id(state_id): if request.method == 'GET': #get state from state_id state = State.get(State.id == state_id) return jsonify(state.to_hash()) elif request.method == 'DELETE': #delete state with id state_id state = State.get(State.id == state_id) state.delete_instance() return 'State %s deleted \n' % state_id
def app_states_id(state_id): if request.method == "GET": try: query = State.get(State.id == state_id) return jsonify(query.to_dict()), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404 elif request.method == "DELETE": try: query = State.get(State.id == state_id) query.delete_instance() return jsonify({"code": 200, "msg": "success"}), 200 except: return jsonify({"code": 404, "msg": "not found"}), 404
def get_state(id): try: state = State.get(State.id == id) except Exception as e: return {'code': 404, 'msg': "State not found"}, 404 return state.to_hash(), 200
def state_id(state_id): """Handle GET and DELETE requests to /states/<state_id> route. Return a hash of the appropriate record in the database in the case of a GET request. Delete the appropriate record in the case of a DELETE request. """ # check whether resource exists: # -------------------------------------------------------------------------- try: record = State.get(State.id == state_id) # return 404 not found if it does not except State.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 DELETE requests elif request.method == "DELETE": record.delete_instance() record.save() return 'deleted state\n'
def state_id(state_id): """Handle GET and DELETE requests to /states/<state_id> route. Return a hash of the appropriate record in the database in the case of a GET request. Delete the appropriate record in the case of a DELETE request. """ # check whether resource exists: # -------------------------------------------------------------------------- try: record = State.get(State.id == state_id) # return 404 not found if it does not except State.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 DELETE requests elif request.method == "DELETE": record.delete_instance() record.save() return 'deleted state\n'
def delete_state(number): try: query = State.get(State.id == number) except State.DoesNotExist: return {'code':404, 'msg':'state not found'}, 404 out_json = query.to_dict() query.delete_instance() return out_json
def modify_state(state_id): try: # displaying state by id if request.method == 'GET': return jsonify(State.get(State.id == state_id).to_dict()) except: return "No State was found with id %d\n" % (int(id)) if request.method == 'DELETE': id = state_id try: # creating and oobject calling the State get_state = State.get(State.id == id) # deleting state based on the id get_state.delete_instance() return "State with id %d was deleted\n" % (int(id)) except: return "No State was found with id %d\n" % (int(id))
def to_hash(self): """Method to_hash returns City object model to hash""" state = State.get(State.id == self.state) city = { 'name' : self.name, 'state_id' : state.id, } return super(City, self).to_hash(self, city)
def get_state(state_id): """ Get the given state Returns the given state in the database. --- tags: - State parameters: - in: path name: state_id type: integer required: True description: ID of the state responses: 200: description: State returned successfully schema: id: State required: - name - id - created_at - updated_at properties: name: type: string description: name of the given state default: None id: type: integer description: id of the state default: 1 created_at: type: datetime string description: date and time the state was created in the database default: '2016-08-11 20:30:38' updated_at: type: datetime string description: date and time the state was updated in the database default: '2016-08-11 20:30:38' 404: description: State was not found 500: description: Request could not be processed """ try: ''' Check that state_id exists ''' query = State.select().where(State.id == state_id) if not query.exists(): raise LookupError('state_id') state = State.get(State.id == state_id) return state.to_dict(), 200 except LookupError as e: abort(404) except Exception as e: abort(500)
def states_id(state_id): if request.method == 'GET': try: state = State.get(State.id == state_id) return jsonify(state.to_dict()), 200 except State.DoesNotExist: return json_response(status_=404, msg="not found") elif request.method == 'DELETE': try: state = State.get(State.id == state_id) state.delete_instance() state.save() return json_response(status_=200, msg="State succesfully deleted") except State.DoesNotExist: return json_response(status_=404, msg="state does not exist")
def get_list_places(state_id): try: # Checking if a state exist State.get(State.id == state_id) except State.DoesNotExist: return make_response(jsonify({'code': '10001', 'msg': 'Place not found'}), 404) if request.method == 'GET': try: # getting a place that is in a specific state list = ListStyle.list(Place.select() .join(City) .where(Place.city == City.id) .join(State) .where(State.id == City.state), request) return jsonify(list) except: return make_response(jsonify({'code': '10001', 'msg': 'Place not found'}), 404)
def get_state(state_id): """ Get a state with id as state_id """ try: state = State.get(State.id == state_id) except Exception as error: response = {} response['code'] = 404 response['msg'] = 'State not found' return response, 404 return state.to_dict(), 200
def state(number): if request.method == 'GET': query = State.get(State.id == number) return query.to_hash() else: try: query = State.select().where(State.id == number).get() except: return {'code':404, 'msg':'state not found'}, 404 out_json = query.to_hash() query.delete_instance() return out_json
def cities(state_id=None, city_id=None): state = None try: if state_id != None: state = State.get(State.id == int(state_id)) except: state = None if state == None: return { 'code': 404, 'msg': "not found" }, 404 if request.method == "GET": if city_id != None: try: city = City.get(City.id == int(city_id), City.state == state) return city.to_dict() except: pass return { 'code': 404, 'msg': "not found" }, 404 return { 'data': [city.to_dict() for city in City.select().where(City.state == state)] }, 200 elif request.method == "POST": name = request.form.get('name') if City.select().where(City.state == state).where(City.name == name).count() > 0: return { 'code': 10002, 'msg': "City already exists in this state" }, 409 try: new_city = City.create(name=name, state=state) except IntegrityError: return { 'code': 10002, 'msg': "City already exists in this state" }, 409 except Exception as e: raise e return new_city.to_dict(), 201 elif request.method == "DELETE": if city_id != None: city = None try: city = City.get(City.id == int(city_id), City.state == state) except: city = None if city != None: city.delete_instance() return {}, 200 return { 'code': 404, 'msg': "not found" }, 404
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 test_create(self): """ Test proper creation (or non-creation) of state records upon POST requests to API. """ # test creation of state with all parameters provided in POST request State.drop_table() database.create_tables([State], safe=True) POST_request1 = self.app.post('/states', data=dict( name='namestring' )) self.assertEqual(POST_request1.status[:3], '200') now = datetime.now().strftime('%d/%m/%Y %H:%M') self.assertEqual(State.get(State.id == 1).name, 'namestring') self.assertEqual(State.get(State.id == 1).created_at.strftime('%d/%m/%Y %H:%M'), now) self.assertEqual(State.get(State.id == 1).updated_at.strftime('%d/%m/%Y %H:%M'), now) # test creation of state in all cases of a parameter missing in POST request POST_request2 = self.app.post('/states', data=dict()) self.assertEqual(POST_request2.status[:3], '400') # test that state ID for sole record in database is correct self.assertEqual(State.select().get().id, 1) # test that a post request with a duplicate name value is rejected POST_request3 = self.app.post('/states', data=dict( name='namestring' )) self.assertEqual(POST_request3.status[:3], '409') self.assertEqual(json.loads(POST_request3.data), {'code': 10001, 'msg': 'State already exists'})
def delete_state(state_id): """ Delete state with id as state_id """ try: state = State.get(State.id == state_id) except Exception as error: response = {} response['code'] = 404 response['msg'] = 'State not found' return response, 404 delete_state = State.delete().where(State.id == state_id) delete_state.execute() response = {} response['code'] = 200 response['msg'] = "State was deleted successfully" return response, 200
def get_single_state(number): try: query = State.get(State.id == number) except State.DoesNotExist: return {'code':404, 'msg':'state not found'}, 404 return query.to_dict()