75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
from dataclasses import dataclass
|
|
from os import path
|
|
|
|
from Database.sql_provider import SQLProvider
|
|
from Database.work import procedure, select_list
|
|
from flask import current_app, session
|
|
|
|
sql_provider = SQLProvider(path.join(path.dirname(__file__), "sql"))
|
|
|
|
|
|
@dataclass
|
|
class InfoRespronse:
|
|
result: tuple
|
|
error_message: str
|
|
status: bool
|
|
|
|
|
|
def check_report(input_data: dict) -> bool:
|
|
db_config = current_app.config["db_config"]
|
|
|
|
result = procedure(db_config, "check_report", tuple(input_data.values()))
|
|
if result is None or result == 0:
|
|
return False
|
|
return True
|
|
|
|
|
|
def view_report_model(request, report_list: dict) -> InfoRespronse:
|
|
id = request.form.get("category")
|
|
month = request.form.get("month")
|
|
year = request.form.get("year")
|
|
|
|
data = dict(id=id, month=month, year=year)
|
|
|
|
if session["role"] in report_list[id]["data"]["read"]:
|
|
status = check_report(data)
|
|
if not status:
|
|
return InfoRespronse((), error_message="Отчет не найден", status=False)
|
|
|
|
db_config = current_app.config["db_config"]
|
|
view_script = report_list[id]["data"]["view"]
|
|
_sql = sql_provider.get(f"{view_script}.sql", data)
|
|
result = select_list(db_config, _sql)
|
|
return InfoRespronse(result, error_message="", status=True)
|
|
else:
|
|
return InfoRespronse(
|
|
(),
|
|
error_message="Недостаточно прав для чтения данного отчета!",
|
|
status=False,
|
|
)
|
|
|
|
|
|
def create_report_model(request, report_list: dict) -> InfoRespronse:
|
|
id = request.form.get("category")
|
|
month = request.form.get("month")
|
|
year = request.form.get("year")
|
|
|
|
data = dict(id=id, month=month, year=year)
|
|
|
|
if session["role"] in report_list[id]["data"]["write"]:
|
|
status = check_report(data)
|
|
if status:
|
|
return InfoRespronse((), error_message="Отчет уже существует", status=False)
|
|
|
|
db_config = current_app.config["db_config"]
|
|
proc_name = report_list[id]["data"]["procedure"]
|
|
args = tuple(data.values())
|
|
procedure(db_config, proc_name, args)
|
|
return InfoRespronse((), error_message="", status=True)
|
|
else:
|
|
return InfoRespronse(
|
|
(),
|
|
error_message="Недостаточно прав для создания данного отчета!",
|
|
status=False,
|
|
)
|