From 58bebc2109f8abc43311ce53b10c8f47177a08f4 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 27 Sep 2022 22:50:12 -0400 Subject: [PATCH] Refactor index_get() with early returns. --- undercover/routes.py | 55 ++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/undercover/routes.py b/undercover/routes.py index 624df0d..15fd95e 100644 --- a/undercover/routes.py +++ b/undercover/routes.py @@ -109,36 +109,41 @@ def logout(): @writing_blueprint.route('/', methods=['GET']) def index_get(): email_address = session.get('username') + if not email_address: + return render_index() + form = CLForm() - if email_address: - user = db.get_user(email_address) - letters = db.get_user_letters(user.id) - if len(letters) > 0: - letter_names = [(i + 1, letter.title) for i, letter in enumerate(letters)] - form.letterName.choices = letter_names + user = db.get_user(email_address) + letters = db.get_user_letters(user.id) + if len(letters) == 0: + return render_index() - form.letterName.data = 1 - selected_letter = request.args.get('letter_name') - if selected_letter: - for i, letter in enumerate(letters): - if letter.title == selected_letter: - form.letterName.data = i + 1 - break + letter_names = [(i + 1, letter.title) for i, letter in enumerate(letters)] + form.letterName.choices = letter_names - # TODO: Load this data more dynamically - # I.e. use a dictionary instead of explicitly-defined fields - data = json.loads(letters[form.letterName.data - 1].contents) + form.letterName.data = 1 + selected_letter = request.args.get('letter_name') - # Ensures default value is set - form.letterName.process_data(form.letterName.data) + if selected_letter: + for i, letter in enumerate(letters): + if letter.title == selected_letter: + form.letterName.data = i + 1 + break - form.company.data = data['company'] - form.body.data = data['body'] - form.closingText.data = data['closingText'] - form.jobAndPronoun.data = data['jobAndPronoun'] - form.mySkills.data = data['mySkills'] - form.skillTypes.data = data['skillTypes'] - form.username.data = data['username'] + # TODO: Load this data more dynamically + # I.e. use a dictionary instead of explicitly-defined fields + data = json.loads(letters[form.letterName.data - 1].contents) + + # Ensures default value is set + form.letterName.process_data(form.letterName.data) + + form.company.data = data['company'] + form.body.data = data['body'] + form.closingText.data = data['closingText'] + form.jobAndPronoun.data = data['jobAndPronoun'] + form.mySkills.data = data['mySkills'] + form.skillTypes.data = data['skillTypes'] + form.username.data = data['username'] return render_index(form=form)