From 1e73c9ea5abb6771e1de90499e8e3f9870baa5a9 Mon Sep 17 00:00:00 2001 From: Brian Date: Sun, 20 Aug 2023 22:31:16 -0400 Subject: [PATCH] testing --- eAbsentee/form/form.py | 7 ++++++- eAbsentee/form/utils.py | 22 ++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/eAbsentee/form/form.py b/eAbsentee/form/form.py index d9153d7..e8a9790 100644 --- a/eAbsentee/form/form.py +++ b/eAbsentee/form/form.py @@ -4,7 +4,7 @@ from flask_login import login_required, current_user from dotenv import load_dotenv from eAbsentee.app import babel -from eAbsentee.form.utils import application_process +from eAbsentee.form.utils import application_process, SystemOverloadError from flask_babel import get_locale # from eAbsentee.app import db # from eAbsentee.admin.models import GroupReference @@ -48,6 +48,11 @@ def form_test(): else: return render_template('form.html') +@form_bp.errorhandler(SystemOverloadError) +def handle_system_overload(e): + # Here, we can return an error page with JavaScript to automatically retry the form submission after 5 seconds + # This is a placeholder and can be replaced with the actual error page name. + return render_template('system_overload_error.html'), 500 @form_bp.errorhandler(500) def handle_exception(e): return render_template('error.html'), 500 diff --git a/eAbsentee/form/utils.py b/eAbsentee/form/utils.py index 26ee36f..993ef0f 100644 --- a/eAbsentee/form/utils.py +++ b/eAbsentee/form/utils.py @@ -22,15 +22,21 @@ application_id_chars = ascii_lowercase + digits - +class SystemOverloadError(Exception): + pass def application_process(request, group_code=None, lang=None, email_registrar=True): - application_id = ''.join(random_choices(application_id_chars, k=24)) - print(f"processing application {application_id}") - write_pdf(application_id, request, lang) - email_pdf(application_id, request, email_registrar) - add_to_database(application_id, request, group_code=group_code) - if not current_app.debug: - os.remove(f'{application_id}.pdf') + try: + application_id = ''.join(random_choices(application_id_chars, k=24)) + print(f"processing application {application_id}") + write_pdf(application_id, request, lang) + email_pdf(application_id, request, email_registrar) + add_to_database(application_id, request, group_code=group_code) + if not current_app.debug: + os.remove(f'{application_id}.pdf') + except Exception as e: + # Here, we assume any exception is due to system overload, this can be refined further + raise SystemOverloadError("System Overload. Please try again later.") from e + def write_pdf(application_id, request, lang): today_date = date.today()