diff --git a/App/Requests/__init__.py b/App/Requests/__init__.py index 879897f..c162543 100644 --- a/App/Requests/__init__.py +++ b/App/Requests/__init__.py @@ -1,8 +1,7 @@ -from flask import request, Blueprint, render_template +from flask import request, Blueprint, render_template, url_for from os import path from checker import check_auth from .requests_model import sklad, materials_per_seller -from datetime import date import json with open(path.join(path.dirname(__file__), 'zapros_menu.json')) as f: @@ -14,14 +13,14 @@ requests_bp = Blueprint('requests_bp', __name__, template_folder='templates') @check_auth def requests(): if request.method == 'GET': - return render_template('zapros_menu.html', options=requests_list) + return render_template('pages_menu.html', options=requests_list, header='Запросы') @requests_bp.route('/sklad', methods=['GET', 'POST']) @check_auth def sklad_zapros(): if request.method == 'GET': - materials = ['Сталь', 'Алюминий', 'Медь', 'Пластик', 'Дерево'] - return render_template('zagotovki.html', materials=materials, header='Количество заготовок на складе') + # materials = ['Сталь', 'Алюминий', 'Медь', 'Пластик', 'Дерево'] + return render_template('zagotovki.html', header='Количество заготовок на складе') else: material = dict(request.form) zagotovki = sklad(material) @@ -30,37 +29,18 @@ def sklad_zapros(): return render_template('output.html', items=zagotovki.result, header=header) else: return render_template('error.html', error_message=zagotovki.error_message) - -# Под вопросом -""" @requests_bp.route('/req2', methods=['GET', 'POST']) -@check_auth -def zagotovki_ship(): - if request.method == 'GET': - zagotovki = get_goods() - if zagotovki.status: - return render_template('zagotovki.html', materials=zagotovki.result, header='Поставки заготовок') - else: - return render_template('error.html', error_message=zagotovki.error_message) - else: - material = dict(request.form) - zagotovki = route(material, 'zapros2.sql') - if zagotovki.status: - header = f'Поставки заготовок из материала \'{material['material']}\'' - return render_template('output.html', items=zagotovki.result, object=header) - else: - return render_template('error.html', error_message=zagotovki.error_message) """ @requests_bp.route('/shipments', methods=['GET', 'POST']) @check_auth def sellers_ship(): if request.method == 'GET': - sellers = ['Alpha Supplies', 'Beta Materials', 'Gamma Parts', 'Delta Components', 'Epsilon Goods'] - return render_template('sellers_ship.html', sellers=sellers) + # sellers = ['Alpha Supplies', 'Beta Materials', 'Gamma Parts', 'Delta Components', 'Epsilon Goods'] + return render_template('sellers_ship.html') else: seller = dict(request.form) zagotovki = materials_per_seller(seller) if zagotovki.status: header = f'Поставки от поставщика \"{seller["seller"]}\"' - return render_template('output.html', items=zagotovki.result, header=header) + return render_template('output.html', items=zagotovki.result, header=header, link=url_for('requests_bp.requests')) else: return render_template('error.html', error_message=zagotovki.error_message) \ No newline at end of file diff --git a/App/Requests/db/DBconnect.py b/App/Requests/db/DBconnect.py new file mode 100644 index 0000000..b335654 --- /dev/null +++ b/App/Requests/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/Requests/db/__init__.py b/App/Requests/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/App/Requests/db/select.py b/App/Requests/db/select.py new file mode 100644 index 0000000..22cc3e7 --- /dev/null +++ b/App/Requests/db/select.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/Requests/db/sql_provider.py b/App/Requests/db/sql_provider.py new file mode 100644 index 0000000..a3a1855 --- /dev/null +++ b/App/Requests/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/Requests/requests_model.py b/App/Requests/requests_model.py index 69fc110..813be3d 100644 --- a/App/Requests/requests_model.py +++ b/App/Requests/requests_model.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from Database.select import select_list -from Database.sql_provider import SQLProvider +from db.select import select_list +from db.sql_provider import SQLProvider from flask import current_app from os import path diff --git a/App/Requests/templates/error.html b/App/Requests/templates/error.html deleted file mode 100644 index c1a8828..0000000 --- a/App/Requests/templates/error.html +++ /dev/null @@ -1,13 +0,0 @@ - - -
- -{{ error_message }}.
- - - diff --git a/App/Requests/templates/sellers_ship.html b/App/Requests/templates/sellers_ship.html index afc6eea..c14c1a8 100644 --- a/App/Requests/templates/sellers_ship.html +++ b/App/Requests/templates/sellers_ship.html @@ -1,8 +1,8 @@ - + -Выберите поставщика
diff --git a/App/Requests/templates/zagotovki.html b/App/Requests/templates/zagotovki.html index eef759a..9d2de58 100644 --- a/App/Requests/templates/zagotovki.html +++ b/App/Requests/templates/zagotovki.html @@ -1,5 +1,5 @@ - +Информации не найдено
{% endif %} \ No newline at end of file diff --git a/App/templates/pages_menu.html b/App/templates/pages_menu.html new file mode 100644 index 0000000..bf6010f --- /dev/null +++ b/App/templates/pages_menu.html @@ -0,0 +1,22 @@ + + + + +