async def create_perimeter(request): """ create new row in db, if db has already row with that id or name send response with error :param request: body json with keys: id(int) - optional; name(string); devices(string) :return: request + 200 or error message + status 400 """ async with request.app['db'].acquire() as conn: try: body = await request.read() body.decode('utf-8') data = json.loads(body) if 'id' in data: cursor = await conn.execute( perimeter.select().where(perimeter.c.id == data['id'])) match_the_id = await cursor.fetchall() if match_the_id: response_obj = { 'status': 'failed', 'reason': 'perimeter with that id has already' } return web.Response(text=json.dumps(response_obj, indent=4, sort_keys=False, default=str), status=400) await conn.execute(perimeter.insert().values( id=data['id'], name=data['name'], devices=data['devices'])) else: cursor = await conn.execute( perimeter.select().where(perimeter.c.name == data['name'])) match_the_name = await cursor.fetchall() if match_the_name: response_obj = { 'status': 'failed', 'reason': 'perimeter with that name has already' } return web.Response(text=json.dumps(response_obj, indent=4, sort_keys=False, default=str), status=400) await conn.execute(perimeter.insert().values( name=data['name'], devices=data['devices'])) return web.Response(text=json.dumps(data, indent=4, sort_keys=False, default=str), status=200) except Exception as e: response_obj = {'status': 'failed', 'reason': str(e)} return web.Response(text=json.dumps(response_obj), status=500)
async def read_all_perimeters(request): """ get all table 'perimeter' :param request: None :return: json response with all table 'perimeter' """ async with request.app['db'].acquire() as conn: try: cursor = await conn.execute(perimeter.select()) records = await cursor.fetchall() response_obj = [dict(q) for q in records] return web.Response(text=json.dumps(response_obj), status=200) except Exception as e: response_obj = {'status': 'failed', 'reason': str(e)} return web.Response(text=json.dumps(response_obj, indent=4, sort_keys=False, default=str), status=500)
async def update_perimeter(request): async with request.app['db'].acquire() as conn: try: body = await request.read() body.decode('utf-8') data = json.loads(body) cursor = await conn.execute( perimeter.select().where(perimeter.c.id == data['id'])) match_the_id = await cursor.fetchall() print(match_the_id) if match_the_id: await conn.execute(perimeter.update().values( name=data['name'], devices=data['devices'])) return web.Response(text=json.dumps(data), status=200) else: await conn.execute(perimeter.insert().values( id=data['id'], name=data['name'], devices=data['devices'])) return web.Response(text=json.dumps(data), status=200) except Exception as e: response_obj = {'status': 'failed', 'reason': str(e)} return web.Response(text=json.dumps(response_obj), status=500)
def show_states(): for row in con.execute(perimeter.select()): print(row)
print(row) con, meta = connect('postgres', 'nbhyfyjun', 'aiohttpdemo_polls') print(con) print(meta) print('=============ALL ROWS DEVICES===========') show_dev() print('=============ALL ROWS PERIMETERS===========') show_perim() print('=============ALL ROWS STATES===========') show_states() print('=============MATCH===========') cursor = con.execute(perimeter.select().where(perimeter.c.name == 'Главный')) records = cursor.fetchall() if records: print(records) con.execute(perimeter.update().values(devices='11,12').where( perimeter.c.name == 'Главный')) cursor = con.execute( perimeter.select().where(perimeter.c.name == 'Главный')) records = cursor.fetchall() print(records) else: print('Dict is empty') d = datetime.datetime(2019, 3, 13, 13, 30, 00) d1 = datetime.datetime.now() print('time1:', d.timestamp())