From ef0b6503cb9908c7a4268982f6e8f25d323a3f32 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 29 Sep 2022 12:53:22 -0400 Subject: [PATCH] Try to init CLForm choices sooner. --- undercover/routes.py | 66 +++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/undercover/routes.py b/undercover/routes.py index 9a39a4b..3ee1afd 100644 --- a/undercover/routes.py +++ b/undercover/routes.py @@ -18,6 +18,11 @@ writing_blueprint = Blueprint('writing', __name__,) class CLForm(Form): + def __init__(self, form: dict[str, str] = None, email_address: str = None, **kwargs): + super().__init__(form, **kwargs) + if email_address: + self.letterName.choices = letter_choices(email_address) + letterName = SelectField( 'Letter Name:', [validators.optional()], @@ -139,6 +144,14 @@ def add_letter() -> Response: return redirect(f'/?letter_name={new_letter_name}') +def letter_choices(email_address: str) -> list[(int, str)]: + user = db.get_user(email_address) + letters = db.get_user_letters(user.id) + if len(letters) == 0: + return [] + + return [(i + 1, letter.title) for i, letter in enumerate(letters)] + @writing_blueprint.route('/', methods=['GET']) def index_get() -> Response: @@ -147,12 +160,9 @@ def index_get() -> Response: return render_index() form = CLForm() - user = db.get_user(email_address) - letters = db.get_user_letters(user.id) - if len(letters) == 0: + letter_names = letter_choices(email_address) + if len(letter_names) == 0: return render_index() - - letter_names = [(i + 1, letter.title) for i, letter in enumerate(letters)] form.letterName.choices = letter_names form.letterName.data = 1 @@ -265,31 +275,31 @@ def git_update() -> None: @writing_blueprint.route('/', methods=['POST']) def generate_pdf() -> Response: - form = CLForm(request.form) - if form.validate(): - data = form.to_cl_data() + email_address = session.get('username') + form = CLForm(request.form, email_address=email_address) + if not form.validate(): + return render_index(form=form) - email_address = session.get('username') - if email_address: - user = db.get_user(email_address) - letters = db.get_user_letters(user.id) - letter_json = jsonify(data).get_data(True) - if len(letters) == 0: - db.add_letter(user.id, 'My Cover Letter', letter_json) - else: - letter = letters[data.selectedLetter] - # TODO: Support title editing - db.edit_letter(letter.id, letter.title, letter_json) + data = form.to_cl_data() - try: - resp = data.generate_pdf() - except ValueError as e: - resp = render_index(form=form, letter_errors=e.args[0]) + if email_address: + user = db.get_user(email_address) + letters = db.get_user_letters(user.id) + letter_json = jsonify(data).get_data(True) + if len(letters) == 0: + db.add_letter(user.id, 'My Cover Letter', letter_json) + else: + letter = letters[data.selectedLetter] + # TODO: Support title editing + db.edit_letter(letter.id, letter.title, letter_json) - # Save entered data as cookies on user's machine - for pair in data.get_pairs(): - resp.set_cookie(pair[0], urllib.parse.quote(pair[1])) + try: + resp = data.generate_pdf() + except ValueError as e: + resp = render_index(form=form, letter_errors=e.args[0]) - return resp + # Save entered data as cookies on user's machine + for pair in data.get_pairs(): + resp.set_cookie(pair[0], urllib.parse.quote(pair[1])) - return render_index(form=form) + return resp