Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlzhou committed Aug 21, 2023
1 parent b083c2a commit 1e73c9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
7 changes: 6 additions & 1 deletion eAbsentee/form/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
22 changes: 14 additions & 8 deletions eAbsentee/form/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1e73c9e

Please sign in to comment.