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['name'] }}
+

Цена: {{ item['price'] }} ₽

+ {% if show_amount %} + Количество: {{item['count']}}
+
+ + + + +
+ {% endif %} + {% if show_form %} +
+ + +
+ {% endif %} +
+
+ +{% endmacro %} \ No newline at end of file diff --git a/App/Waybill/templates/waybill.html b/App/Waybill/templates/waybill.html index 529270f..a52256f 100644 --- a/App/Waybill/templates/waybill.html +++ b/App/Waybill/templates/waybill.html @@ -1,48 +1,41 @@ +{% import 'card.html' as card %} - + - Авторизация + Оформление накладной + + + -

Заглушка для примера составления накладной

-
-
- -
- Фото товара -

Название товара 1

-

Цена: 500 руб.

-

Количество: 10 шт.

-
- +
+
+ +
+

Список товаров

+
+ {% for item in items %} +
+ {{ card.render_item(item, show_form = True, show_amount = False) }} +
+ {% endfor %}
-
- Фото товара -

Название товара 2

-

Цена: 1200 руб.

-

Количество: 5 шт.

-
- -
-
-
- Фото товара -

Название товара 3

-

Цена: 800 руб.

-

Количество: 7 шт.

-
- + + +
+

Корзина

+
    +
  • Корзина пуста
  • +
+
+
Итог: 0
-
- - -
- +
- \ No newline at end of file + diff --git a/App/static/css/waybill.css b/App/static/css/waybill.css index 048abb5..b7d39da 100644 --- a/App/static/css/waybill.css +++ b/App/static/css/waybill.css @@ -1,48 +1,10 @@ -.container { - display: flex; - flex-wrap: wrap; - gap: 20px; - padding: 20px; - justify-content: center; -} .card { background: #fff; border: 1px solid #ddd; + color: black; border-radius: 10px; width: 250px; padding: 15px; + margin-bottom: 15px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); -} -.card img { - width: 100%; - height: 150px; - object-fit: cover; - border-radius: 5px; -} -.card h3 { - margin: 10px 0; - font-size: 18px; -} -.price { - font-weight: bold; - color: #28a745; -} -.quantity { - margin: 10px 0; - font-weight: bold; - color: #007bff; -} -.input-container { - margin: 10px 0; -} -.input-container input { - width: 80%; - padding: 8px; - font-size: 14px; - border: 1px solid #ddd; - border-radius: 5px; -} -.button-container { - text-align: center; - margin: 20px 0; } \ No newline at end of file