Pack of changes (auth and requests)
This commit is contained in:
30
App/Auth/auth.py
Normal file
30
App/Auth/auth.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from flask import request, Blueprint, render_template, session, redirect, url_for
|
||||||
|
from os import path
|
||||||
|
from .auth_route import route
|
||||||
|
from Database.sql_provider import SQLProvider
|
||||||
|
import json
|
||||||
|
|
||||||
|
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
|
||||||
|
auth_bp = Blueprint('auth_bp', __name__, template_folder='templates')
|
||||||
|
|
||||||
|
with open(path.join(path.dirname(__file__), 'config.json')) as f:
|
||||||
|
config = json.load(f)
|
||||||
|
|
||||||
|
@auth_bp.route('/', methods=['GET', 'POST'])
|
||||||
|
def auth():
|
||||||
|
if request.method == 'GET':
|
||||||
|
return render_template('auth.html')
|
||||||
|
else:
|
||||||
|
data = request.form.to_dict()
|
||||||
|
print(data)
|
||||||
|
auth_data = route(config, data, sql_provider, 'auth.sql')
|
||||||
|
if auth_data.status:
|
||||||
|
session.update({
|
||||||
|
'user_id': auth_data.result[0]['user_ID'],
|
||||||
|
'role': auth_data.result[0]['role'],
|
||||||
|
'db_config': config,
|
||||||
|
'permanent': True
|
||||||
|
})
|
||||||
|
return redirect(url_for('index'))
|
||||||
|
else:
|
||||||
|
return auth_data.error_message
|
||||||
@@ -1,28 +1,15 @@
|
|||||||
from flask import request, Blueprint, render_template, session, redirect, url_for
|
from dataclasses import dataclass
|
||||||
from os import path
|
from Database.select import select_list
|
||||||
from db.sql_provider import SQLProvider
|
@dataclass
|
||||||
from db.select import select_list
|
class InfoRespronse:
|
||||||
import json
|
result: tuple
|
||||||
|
error_message: str
|
||||||
|
status: bool
|
||||||
|
|
||||||
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
|
def route(db_config, input_data, sql_provider, name) -> InfoRespronse:
|
||||||
auth_bp = Blueprint('auth_bp', __name__, template_folder='templates')
|
_sql = sql_provider.get(name, input_data)
|
||||||
|
print("sql = ", _sql)
|
||||||
with open(path.join(path.dirname(__file__), 'config.json')) as f:
|
result = select_list(db_config, _sql)
|
||||||
config = json.load(f)
|
if result is None:
|
||||||
|
return InfoRespronse(result, error_message = 'Ошибка на этапе авторизации', status=False)
|
||||||
@auth_bp.route('/', methods=['GET', 'POST'])
|
return InfoRespronse(result, error_message='', status=True)
|
||||||
def auth():
|
|
||||||
if request.method == 'GET':
|
|
||||||
return render_template('auth.html')
|
|
||||||
else:
|
|
||||||
data = dict(request.form)
|
|
||||||
print(data)
|
|
||||||
result = select_list(config, sql_provider.get('auth.sql', data))
|
|
||||||
if result[1]:
|
|
||||||
session['user_id'] = result[1][0]['user_ID']
|
|
||||||
session['role'] = result[1][0]['role']
|
|
||||||
session['db_config'] = config
|
|
||||||
session.permanent = True
|
|
||||||
return redirect(url_for('index'))
|
|
||||||
else:
|
|
||||||
return 'Неправильный логин или пароль'
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Authorization</title>
|
<title>Авторизация</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
from flask import request, Blueprint, render_template, session, redirect, url_for
|
|
||||||
from os import path
|
|
||||||
from db.sql_provider import SQLProvider
|
|
||||||
from db.select import select_list
|
|
||||||
from checker import check_auth
|
|
||||||
|
|
||||||
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
|
|
||||||
requests_bp = Blueprint('requests_bp', __name__, template_folder='templates')
|
|
||||||
|
|
||||||
@requests_bp.route('/', methods=['GET', 'POST'])
|
|
||||||
@check_auth
|
|
||||||
def sklad_zapros():
|
|
||||||
if request.method == 'GET':
|
|
||||||
result = select_list(session['db_config'], sql_provider.get('zagotovki.sql', {}))
|
|
||||||
return render_template('sklad_zapros.html', materials=result[1], status=True)
|
|
||||||
else:
|
|
||||||
name = dict(request.form)
|
|
||||||
print(name)
|
|
||||||
result = select_list(session['db_config'], sql_provider.get('zapros1.sql', name))
|
|
||||||
return render_template('sklad_zapros.html', materials=result[1], result_table=result)
|
|
||||||
26
App/Queries/requests.py
Normal file
26
App/Queries/requests.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from flask import request, Blueprint, render_template, session
|
||||||
|
from os import path
|
||||||
|
from Database.sql_provider import SQLProvider
|
||||||
|
from checker import check_auth
|
||||||
|
from .requests_route import route
|
||||||
|
|
||||||
|
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
|
||||||
|
requests_bp = Blueprint('requests_bp', __name__, template_folder='templates')
|
||||||
|
|
||||||
|
@requests_bp.route('/', methods=['GET', 'POST'])
|
||||||
|
@check_auth
|
||||||
|
def sklad_zapros():
|
||||||
|
if request.method == 'GET':
|
||||||
|
zagotovki = route(session['db_config'], {}, sql_provider, 'zagotovki.sql')
|
||||||
|
if zagotovki.status:
|
||||||
|
return render_template('sklad_zapros.html', materials=zagotovki.result, status=True)
|
||||||
|
else:
|
||||||
|
return zagotovki.error_message
|
||||||
|
else:
|
||||||
|
material = dict(request.form)
|
||||||
|
zagotovki = route(session['db_config'], material, sql_provider, 'zapros1.sql')
|
||||||
|
if zagotovki.status:
|
||||||
|
print(zagotovki.result)
|
||||||
|
return render_template('sklad_zapros.html', items=zagotovki.result)
|
||||||
|
else:
|
||||||
|
return zagotovki.error_message
|
||||||
15
App/Queries/requests_route.py
Normal file
15
App/Queries/requests_route.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
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)
|
||||||
@@ -2,12 +2,12 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Hello World</title>
|
<title>Заготовки на складе</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<!-- Input -->
|
|
||||||
<h1>Hello World</h1>
|
|
||||||
{% if status %}
|
{% if status %}
|
||||||
|
<!-- Input -->
|
||||||
|
<h1>Выберите материал</h1>
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
<select name="material">
|
<select name="material">
|
||||||
{% for item in materials %}
|
{% for item in materials %}
|
||||||
@@ -18,14 +18,15 @@
|
|||||||
</form>
|
</form>
|
||||||
{% else %}
|
{% else %}
|
||||||
<!-- Output -->
|
<!-- Output -->
|
||||||
{% if result_table %}
|
<h1>Заготовки на складе</h1>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
{% for item in result_table[0] %}
|
<th>Материал</th>
|
||||||
<th>{{ item }}</th>
|
<th>Вес</th>
|
||||||
{% endfor %}
|
<th>Цена</th>
|
||||||
|
<th>Количество</th>
|
||||||
</tr>
|
</tr>
|
||||||
{% for item in result_table[1] %}
|
{% for item in items %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ item['material'] }}</td>
|
<td>{{ item['material'] }}</td>
|
||||||
<td>{{ item['weight'] }}</td>
|
<td>{{ item['weight'] }}</td>
|
||||||
@@ -35,7 +36,6 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
from flask import Flask, redirect, render_template, url_for, session, request
|
from flask import Flask, render_template, session
|
||||||
from os import path
|
from Queries.requests import requests_bp
|
||||||
from Queries.req import requests_bp
|
from Auth.auth import auth_bp
|
||||||
from Auth.auth_route import auth_bp
|
|
||||||
from checker import check_auth
|
from checker import check_auth
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from .DBconnect import DBContextManager
|
from .DBconnect import DBContextManager
|
||||||
|
|
||||||
def select_list(db_config, sql):
|
def select_list(db_config, sql) -> list:
|
||||||
with DBContextManager(db_config) as cursor:
|
with DBContextManager(db_config) as cursor:
|
||||||
if cursor is None:
|
if cursor is None:
|
||||||
raise ValueError("Cursor not created")
|
raise ValueError("Cursor not created")
|
||||||
@@ -9,4 +9,4 @@ def select_list(db_config, sql):
|
|||||||
result = cursor.fetchall()
|
result = cursor.fetchall()
|
||||||
schema = [item[0] for item in cursor.description]
|
schema = [item[0] for item in cursor.description]
|
||||||
lst = [dict(zip(schema, row)) for row in result]
|
lst = [dict(zip(schema, row)) for row in result]
|
||||||
return schema, lst
|
return lst
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Hello World</title>
|
<title>Привет мир!</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Hello World</h1>
|
<h1>Hello World</h1>
|
||||||
|
|||||||
Reference in New Issue
Block a user