From e746d1e94eeeafc3229b6c1d790394aa73f32102 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 6 Jul 2019 16:54:30 +0400 Subject: =?UTF-8?q?WIP:=20=D0=A0=D0=B5=D0=BD=D0=B4=D0=B5=D1=80=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=81=D1=82=D0=BE=D1=80=D0=BE=D0=BD=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day9/task5/main.py | 56 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'day9/task5/main.py') diff --git a/day9/task5/main.py b/day9/task5/main.py index 8ed3b50..db54d13 100644 --- a/day9/task5/main.py +++ b/day9/task5/main.py @@ -1,5 +1,5 @@ from router import route -from utils import render_template, parse_query +from utils import render_template, parse_query, NOT_FOUND_CODE from database import db from config import SERVER_HOST, SERVER_PORT @@ -28,34 +28,50 @@ def add_post(query, *args): return f'

ADD: {query}

' -@route('/') -def index_get(query, *args): +@route('/get', ['POST']) +def db_get(query, *args): cursor = db.cursor() - cursor.execute('DESCRIBE table_task1;') - table_structure = cursor.fetchall() - cursor.execute('SELECT * FROM table_task1;') - content = cursor.fetchall() + if query['type'] == 'full': + cursor.execute('DESCRIBE table_task1;') + table_structure = cursor.fetchall() - cursor.close() + cursor.execute('SELECT * FROM table_task1;') + content = cursor.fetchall() + + cursor.close() + + table_headers = [field[0] for field in table_structure] - heading = [] - for column in [field[0] for field in table_structure]: - heading.append(f'{column}') - heading = '\n%s\n' % '\n'.join(heading) + json_content = [] + for row in content: + new_row = [] + for col in row: + if not isinstance(col, (float, bool, int, dict, list, tuple)): + col = str(col) + new_row.append(col) + json_content.append(new_row) - rows = [] - for row_index, row in enumerate(content): - formatted = [] - for field_index, field in enumerate(row): - color = 'odd' if (field_index % 2 + row_index % 2) % 2 == 0 else 'even' + return {'headers': table_headers, 'content': json_content} + elif query['type'] == 'single_id': + cursor.execute(f'SELECT * FROM table_task1 WHERE service_id="{query["service_id"]}";') + content = cursor.fetchone() - formatted.append(f'{field}') + cursor.close() - rows.append('{}'.format(''.join(formatted))) + json_content = [] + for col in content: + if not isinstance(col, (float, bool, int, dict, list, tuple)): + col = str(col) + json_content.append(col) - body = '{}'.format("\n".join(rows)) + return json_content + return NOT_FOUND_CODE + + +@route('/') +def index_get(query, *args): data = render_template('index.html', heading=heading, body=body) return data -- cgit v1.2.3