def post(self): #csv in post body of post request data = request.data.decode('utf-8') #save reading to CSV file with open( "metadata.csv", "a", ) as f: writer = csv.writer(f, delimiter=",") writer.writerow([time.ctime(), data]) #prep data for db lines = data.strip().split('\n') for line in lines: #print(line) data = line.strip().split(',') gw_id = data[0] gw_site = data[1] gw_locate = data[2] gw_url = data[3] gw_dir = data[4] gw_port = data[5] gateway = GatewayModel(gw_id, gw_site, gw_locate, gw_url, gw_dir, gw_port) try: gateway.save_to_db() except: return { 'message': 'An error occurred while creating the gateway' }, 500 return gateway.json(), 201
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 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()