from flask import request, Blueprint, render_template, session, url_for from checker import check_auth from os import path from .report_model import view_report, make_report import json with open(path.join(path.dirname(__file__), 'reports.json')) as f: report_list = json.load(f) report_bp = Blueprint('report_bp', __name__, template_folder='templates') @report_bp.route('/menu') @check_auth def menu(): if request.method == 'GET': return render_template('report_menu.html') @report_bp.route('/create', methods=['GET', 'POST']) @check_auth def create(): if request.method == 'GET': return render_template('report_basic.html', write=True, title='Создание отчета', items = report_list) else: data = dict(id=request.form.get('category'), month=request.form.get('month'), year=request.form.get('year')) with open(path.join(path.dirname(__file__), f'access/{data['id']}.json')) as f: report_access = json.load(f) if session['role'] in report_access['write']: proc_name = report_access['procedure'] ready_report = make_report(data, proc_name) if ready_report.status: return render_template("OK.html") else: return render_template("error.html", error_message=ready_report.error_message) else: return render_template("error.html", error_message='Недостаточно прав для создания данного отчета!') @report_bp.route('/view', methods=['GET', 'POST']) @check_auth def view(): if request.method == 'GET': return render_template('report_basic.html', write=False, title='Просмотр отчета', items = report_list) else: data = dict(id=request.form.get('category'), month=request.form.get('month'), year=request.form.get('year')) with open(path.join(path.dirname(__file__), f'access/{data['id']}.json')) as f: report_access = json.load(f) if session['role'] in report_access['read']: ready_report = view_report(data, report_access['view']) if ready_report.status: title= f'{report_access["title"]} за {data["month"]}-{data["year"]}' return render_template("output.html", items=ready_report.result, header=title, link = url_for('report_bp.menu')) else: return render_template("error.html", error_message=ready_report.error_message) else: return render_template("error.html", error_message='Недосточно прав для чтения данного отчета!')