summaryrefslogtreecommitdiff
path: root/day9/task5/main.py
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2019-07-06 16:54:30 +0400
committerAndrew <saintruler@gmail.com>2019-07-06 16:54:30 +0400
commite746d1e94eeeafc3229b6c1d790394aa73f32102 (patch)
tree8c27551159d3d94a7fd4b699eff339e924cc2a25 /day9/task5/main.py
parent1e6967f8c4f1ef64947d7f2b95268339d78db454 (diff)
WIP: Рендеринг таблицы на стороне клиента.
Diffstat (limited to 'day9/task5/main.py')
-rw-r--r--day9/task5/main.py56
1 files changed, 36 insertions, 20 deletions
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'<h1>ADD: {query}</h1>'
-@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'<th>{column}</th>')
- heading = '<thead><tr>\n%s\n</tr></thead>' % '\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'<td class={color}>{field}</td>')
+ cursor.close()
- rows.append('<tr>{}</tr>'.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 = '<tbody>{}</tbody>'.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