def post(self): #data sending as form data = request.form gw_id = data['gw_id'] if GatewayModel.find_by_id(gw_id): return { 'message': "Gateway with id '{}' already exists.".format(gw_id) }, 400 data = Gateway.parser.parse_args() with open( "metadata.csv", "a", ) as f: writer = csv.writer(f, delimiter=",") writer.writerow([time.ctime(), gw_id, *data.values()]) gateway = GatewayModel(gw_id, **data) try: gateway.save_to_db() except: return { 'message': 'An error occurred while creating the gateway' }, 500 return gateway.json(), 201
def patch(self): data = request.form gw_id = data['gw_id'] gw_update = [] gateway = GatewayModel.find_by_id(gw_id) if gateway is None: return {'message': "Gateway id {} doesn't exist. Please add gateway first".format(gw_id)}, 405 if 'gw_model' in data: gateway.gw_model = data.get('gw_model') gw_update.append('gw_model') if 'gw_sw' in data: gateway.gw_sw = data.get('gw_sw') gw_update.append('gw_sw') if 'gw_lora_addr' in data: gateway.gw_lora_addr = data.get('gw_lora_addr') gw_update.append('gw_lora_addr') if 'gw_site' in data: gateway.gw_site = data.get('gw_site') gw_update.append('gw_site') if 'gw_locate' in data: gateway.gw_locate = data.get('gw_locate') gw_update.append('gw_locate') gateway.update_db() gw_update = " ".join(str(elem) for elem in gw_update) return {'message' : "Updated {} in Gateway id {}.".format(gw_update,gw_id)}
def delete(self): #gw_id sent via form data = request.form gw_id = data['gw_id'] gateway = GatewayModel.find_by_id(gw_id) if gateway: gateway.delete_from_db() return {'message': 'Gateway deleted'}
def put(self): data = request.form gw_id = data['gw_id'] gateway = GatewayModel.find_by_id(gw_id) if gateway is None: gateway = GatewayModel(gw_id, data['gw_site'], data['gw_locate']) else: gateway.gw_site = data['gw_site'] gateway.gw_locate = data['gw_locate'] gateway.save_to_db() return gateway.json()
def put(self): data = request.form gw_id = data['gw_id'] gateway = GatewayModel.find_by_id(gw_id) if gateway is None: gateway = GatewayModel(gw_id, **data) else: gateway.gw_id = data['gw_id'] gateway.gw_model = data['gw_model'] gateway.gw_sw = data['gw_sw'] gateway.gw_lora_addr = data['gw_lora_addr'] gateway.gw_site = data['gw_site'] gateway.gw_locate = data['gw_locate'] gateway.save_to_db() return gateway.json()
def get(self, gw_id): gateway = GatewayModel.find_by_id(gw_id) if gateway: return gateway.json() return {'message': 'Gateway not found'}, 404