diff --git a/App/Waybill/__init__.py b/App/Waybill/__init__.py index 66fdc1b..78dd921 100644 --- a/App/Waybill/__init__.py +++ b/App/Waybill/__init__.py @@ -1,11 +1,16 @@ from flask import request, Blueprint, render_template, session, current_app, redirect, url_for from checker import check_auth +from .model import waybill_model waybill_bp = Blueprint('waybill_bp', __name__, template_folder='templates') @waybill_bp.route('/', methods=['GET', 'POST']) @check_auth def waybill(): if request.method == 'GET': - return render_template('waybill.html') + pack = waybill_model() + if pack.status: + return render_template('waybill.html', items=pack.result) + else: + return render_template('error.html', error_message=pack.error_message) print(request.form) return 'OK' \ No newline at end of file diff --git a/App/Waybill/db/DBconnect.py b/App/Waybill/db/DBconnect.py new file mode 100644 index 0000000..b335654 --- /dev/null +++ b/App/Waybill/db/DBconnect.py @@ -0,0 +1,34 @@ +import pymysql +from pymysql.err import * +class DBContextManager: + def __init__(self, db_config : dict): + self.db_config = db_config + self.connection = None + self.cursor = None + + def __enter__(self): + try: + self.connection = pymysql.connect( + host=self.db_config['host'], + port=self.db_config['port'], + user=self.db_config['user'], + password=self.db_config['password'], + db=self.db_config['db'] + ) + self.cursor = self.connection.cursor() + return self.cursor + except (OperationalError, KeyError) as err: + print(err.args) + return None + + def __exit__(self, exc_type, exc_val, exc_tb): + if self.connection and self.cursor: + if exc_type: + print(exc_type, '\n', exc_val) + self.connection.rollback() + else: + self.connection.commit() + self.cursor.close() + self.connection.close() + return True + \ No newline at end of file diff --git a/App/Waybill/db/__init__.py b/App/Waybill/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/App/Waybill/db/sql_provider.py b/App/Waybill/db/sql_provider.py new file mode 100644 index 0000000..a3a1855 --- /dev/null +++ b/App/Waybill/db/sql_provider.py @@ -0,0 +1,14 @@ +import os +from string import Template + +class SQLProvider: + def __init__(self, file_path): + self.scripts = {} + for file in os.listdir(file_path): + _sql = open(f'{file_path}/{file}').read() + self.scripts[file] = Template(_sql) + + def get(self, name, params) -> dict: + if name not in self.scripts: + raise ValueError(f'SQL template {name} not found') + return self.scripts[name].substitute(**params) \ No newline at end of file diff --git a/App/Waybill/db/work.py b/App/Waybill/db/work.py new file mode 100644 index 0000000..22cc3e7 --- /dev/null +++ b/App/Waybill/db/work.py @@ -0,0 +1,12 @@ +from .DBconnect import DBContextManager + +def select_list(db_config, sql) -> list: + with DBContextManager(db_config) as cursor: + if cursor is None: + raise ValueError("Cursor not created") + else: + cursor.execute(sql) + result = cursor.fetchall() + schema = [item[0] for item in cursor.description] + lst = [dict(zip(schema, row)) for row in result] + return lst \ No newline at end of file diff --git a/App/Waybill/waybill_model.py b/App/Waybill/model.py similarity index 77% rename from App/Waybill/waybill_model.py rename to App/Waybill/model.py index 2928f24..d63f599 100644 --- a/App/Waybill/waybill_model.py +++ b/App/Waybill/model.py @@ -1,5 +1,5 @@ -from Database.sql_provider import SQLProvider -from Database.select import select_list +from .db.sql_provider import SQLProvider +from .db.work import select_list from flask import current_app from dataclasses import dataclass import os @@ -13,8 +13,8 @@ class InfoRespronse: sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql')) -def waybill_model(input_data) -> InfoRespronse: - _sql = sql_provider.get('waybill.sql', input_data) +def waybill_model() -> InfoRespronse: + _sql = sql_provider.get('goods.sql', {}) result = select_list(current_app.config['db_config'], _sql) if result is None: return InfoRespronse((), diff --git a/App/Waybill/sql/goods.sql b/App/Waybill/sql/goods.sql new file mode 100644 index 0000000..feec29a --- /dev/null +++ b/App/Waybill/sql/goods.sql @@ -0,0 +1 @@ +SELECT work_id, name, price, material, count FROM workpiece diff --git a/App/Waybill/templates/card.html b/App/Waybill/templates/card.html new file mode 100644 index 0000000..a687103 --- /dev/null +++ b/App/Waybill/templates/card.html @@ -0,0 +1,27 @@ +{% macro render_item(item, show_amount = False, show_form = False) %} + +
Цена: {{ item['price'] }} ₽
+ {% if show_amount %} + Количество: {{item['count']}}