Refactor index_get() with early returns.

This commit is contained in:
Sage Vaillancourt 2022-09-27 22:50:12 -04:00
parent 1b32206a23
commit 58bebc2109
1 changed files with 30 additions and 25 deletions

View File

@ -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)