diff --git a/App/Auth/auth.py b/App/Auth/auth.py index d5e3b20..044accb 100644 --- a/App/Auth/auth.py +++ b/App/Auth/auth.py @@ -1,9 +1,6 @@ -from flask import request, Blueprint, render_template, session, current_app,redirect, url_for -import os -from .auth_route import route -from Database.sql_provider import SQLProvider +from flask import request, Blueprint, render_template, session, current_app, redirect, url_for +from .auth_model import auth_model -sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql')) auth_bp = Blueprint('auth_bp', __name__, template_folder='templates') @auth_bp.route('/', methods=['GET', 'POST']) @@ -13,7 +10,7 @@ def auth(): else: data = request.form.to_dict() data['table'] = 'internal_users' if 'internal' in data else 'external_users' - auth_data = route(current_app.config['db_config'], data, sql_provider, 'auth.sql') + auth_data = auth_model(data) if auth_data.status: session.update({ 'login': auth_data.result[0]['login'], diff --git a/App/Auth/auth_model.py b/App/Auth/auth_model.py new file mode 100644 index 0000000..294bcbe --- /dev/null +++ b/App/Auth/auth_model.py @@ -0,0 +1,25 @@ +from dataclasses import dataclass +from Database.select import select_list +from Database.sql_provider import SQLProvider +from flask import current_app +import os + +sql_provider = SQLProvider(os.path.join(os.path.dirname(__file__), 'sql')) +@dataclass +class InfoRespronse: + result: tuple + error_message: str + status: bool + +def auth_model(input_data) -> InfoRespronse: + _sql = sql_provider.get('auth.sql', input_data) + result = select_list(current_app.config['db_config'], _sql) + if result is None: + return InfoRespronse(result, + error_message = 'Произошла ошибка на этапе авторизации', + status=False) + elif len(result) == 0: + return InfoRespronse(result, + error_message = 'Пользователь не найден', + status=False) + return InfoRespronse(result, error_message='', status=True) \ No newline at end of file diff --git a/App/Auth/auth_route.py b/App/Auth/auth_route.py deleted file mode 100644 index c65e492..0000000 --- a/App/Auth/auth_route.py +++ /dev/null @@ -1,17 +0,0 @@ -from dataclasses import dataclass -from Database.select import select_list -@dataclass -class InfoRespronse: - result: tuple - error_message: str - status: bool - -def route(db_config, input_data, sql_provider, name) -> InfoRespronse: - _sql = sql_provider.get(name, input_data) - # print("sql = ", _sql) - result = select_list(db_config, _sql) - if result is None: - return InfoRespronse(result, error_message = 'Произошла ошибка на этапе авторизации', status=False) - elif len(result) == 0: - return InfoRespronse(result, error_message = 'Пользователь не найден', status=False) - return InfoRespronse(result, error_message='', status=True) \ No newline at end of file diff --git a/App/Requests/requests.py b/App/Requests/requests.py index 03af5ca..0e6e5c7 100644 --- a/App/Requests/requests.py +++ b/App/Requests/requests.py @@ -1,15 +1,13 @@ -from flask import request, Blueprint, render_template, session +from flask import request, Blueprint, render_template from os import path -from Database.sql_provider import SQLProvider from checker import check_auth, group_required -from .requests_route import route +from .requests_model import sklad, get_goods, get_sellers, materials_per_seller from datetime import date import json with open(path.join(path.dirname(__file__), 'zapros_menu.json')) as f: requests_list = json.load(f) -sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql')) requests_bp = Blueprint('requests_bp', __name__, template_folder='templates') @requests_bp.route('/', methods=['GET', 'POST']) @@ -19,59 +17,58 @@ def requests(): if request.method == 'GET': return render_template('zapros_menu.html', options=requests_list) -@requests_bp.route('/req1', methods=['GET', 'POST']) +@requests_bp.route('/sklad', methods=['GET', 'POST']) @check_auth @group_required def sklad_zapros(): if request.method == 'GET': - zagotovki = route(session['db_config'], {}, sql_provider, 'zagotovki.sql') + 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(session['db_config'], material, sql_provider, 'zapros1.sql') + zagotovki = sklad(material) 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('/req2', methods=['GET', 'POST']) +# Под вопросом +""" @requests_bp.route('/req2', methods=['GET', 'POST']) @check_auth @group_required def zagotovki_ship(): if request.method == 'GET': - zagotovki = route(session['db_config'], {}, sql_provider, 'zagotovki.sql') + 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(session['db_config'], material, sql_provider, 'zapros2.sql') + 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) + return render_template('error.html', error_message=zagotovki.error_message) """ -@requests_bp.route('/req3', methods=['GET', 'POST']) +@requests_bp.route('/shipments', methods=['GET', 'POST']) @check_auth @group_required def sellers_ship(): if request.method == 'GET': - zagotovki = route(session['db_config'], {}, sql_provider, 'sellers.sql') + zagotovki = get_sellers() if zagotovki.status: - min_year = '2000' - max_year = str(date.today().year) - return render_template('sellers_ship.html', sellers=zagotovki.result, year_from=min_year, year_to=max_year) + return render_template('sellers_ship.html', sellers=zagotovki.result, year_from='2000', year_to=str(date.today().year)) else: return render_template('error.html', error_message=zagotovki.error_message) else: seller = dict(request.form) - zagotovki = route(session['db_config'], seller, sql_provider, 'zapros3.sql') + zagotovki = materials_per_seller(seller) if zagotovki.status: header = f'Поставки от поставщика \'{seller['seller']}\'' return render_template('output.html', items=zagotovki.result, object=header) diff --git a/App/Requests/requests_model.py b/App/Requests/requests_model.py new file mode 100644 index 0000000..476fa7f --- /dev/null +++ b/App/Requests/requests_model.py @@ -0,0 +1,49 @@ +from dataclasses import dataclass +from Database.select import select_list +from Database.sql_provider import SQLProvider +from flask import current_app +from os import path + +sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql')) +@dataclass +class InfoRespronse: + result: tuple + error_message: str + status: bool + +def get_goods() -> InfoRespronse: + _sql = sql_provider.get('zagotovki.sql', {}) + result = select_list(current_app.config['db_config'], _sql) + if result is None: + return InfoRespronse(result, + error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором', + status=False) + return InfoRespronse(result, error_message='', status=True) + +def get_sellers() -> InfoRespronse: + _sql = sql_provider.get('sellers.sql', {}) + result = select_list(current_app.config['db_config'], _sql) + if result is None: + return InfoRespronse(result, + error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором', + status=False) + return InfoRespronse(result, error_message='', status=True) + +def sklad(input_data) -> InfoRespronse: + _sql = sql_provider.get('zapros1.sql', input_data) + print("sql = ", _sql) + result = select_list(current_app.config['db_config'], _sql) + if result is None: + return InfoRespronse(result, + error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором', + status=False) + return InfoRespronse(result, error_message='', status=True) + +def materials_per_seller(input_data) -> InfoRespronse: + _sql = sql_provider.get('zapros3.sql', input_data) + result = select_list(current_app.config['db_config'], _sql) + if result is None: + return InfoRespronse(result, + error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором', + status=False) + return InfoRespronse(result, error_message='', status=True) \ No newline at end of file diff --git a/App/Requests/requests_route.py b/App/Requests/requests_route.py deleted file mode 100644 index 513474b..0000000 --- a/App/Requests/requests_route.py +++ /dev/null @@ -1,15 +0,0 @@ -from dataclasses import dataclass -from Database.select import select_list -@dataclass -class InfoRespronse: - result: tuple - error_message: str - status: bool - -def route(db_config, input_data, sql_provider, name) -> InfoRespronse: - _sql = sql_provider.get(name, input_data) - print("sql = ", _sql) - result = select_list(db_config, _sql) - if result is None: - return InfoRespronse(result, error_message = 'Ошибка в подключении к базе данных. Свяжитесь с администратором', status=False) - return InfoRespronse(result, error_message='', status=True) \ No newline at end of file diff --git a/App/Requests/zapros_menu.json b/App/Requests/zapros_menu.json index eacda20..77b9a68 100644 --- a/App/Requests/zapros_menu.json +++ b/App/Requests/zapros_menu.json @@ -1,5 +1,5 @@ [ {"name": "Количество заготовок на складе", "url": "requests_bp.sklad_zapros"}, - {"name": "Поставки заготовок", "url": "requests_bp.zagotovki_ship"}, + {"name": "Поставки поставщиком за год", "url": "requests_bp.sellers_ship"} ] \ No newline at end of file diff --git a/App/app.py b/App/app.py index 8952206..d70d835 100644 --- a/App/app.py +++ b/App/app.py @@ -7,13 +7,10 @@ import os, json app = Flask(__name__) app.secret_key = 'suplex' -# app.config.from_file(os.path.join(os.path.dirname(__file__), 'data/db_access.json'), load=json.load) -# app.config.from_file(os.path.join(os.path.dirname(__file__), 'data/config.json'), load=json.load) - -with open(os.path.join(os.path.dirname(__file__), 'data/db_access.json')) as f: - app.config['db_access'] = json.load(f) -with open(os.path.join(os.path.dirname(__file__), 'data/config.json')) as f: - app.config['db_config'] = json.load(f) +app.config.update( + db_config=json.load(open(os.path.join(os.path.dirname(__file__), 'data/config.json'))), + db_access=json.load(open(os.path.join(os.path.dirname(__file__), 'data/db_access.json'))) +) app.register_blueprint(requests_bp, url_prefix='/requests') app.register_blueprint(auth_bp, url_prefix='/auth') diff --git a/Docs/MVC_auth_1.drawio b/Docs/MVC_auth_1.drawio new file mode 100644 index 0000000..fc25eed --- /dev/null +++ b/Docs/MVC_auth_1.drawio @@ -0,0 +1,694 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Docs/MVC_auth_2.drawio b/Docs/MVC_auth_2.drawio new file mode 100644 index 0000000..9f878fd --- /dev/null +++ b/Docs/MVC_auth_2.drawio @@ -0,0 +1,591 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +