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']) @writing_blueprint.route('/', methods=['GET'])
def index_get(): def index_get():
email_address = session.get('username') email_address = session.get('username')
if not email_address:
return render_index()
form = CLForm() form = CLForm()
if email_address: user = db.get_user(email_address)
user = db.get_user(email_address) letters = db.get_user_letters(user.id)
letters = db.get_user_letters(user.id) if len(letters) == 0:
if len(letters) > 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 letter_names = [(i + 1, letter.title) for i, letter in enumerate(letters)]
selected_letter = request.args.get('letter_name') form.letterName.choices = letter_names
if selected_letter:
for i, letter in enumerate(letters):
if letter.title == selected_letter:
form.letterName.data = i + 1
break
# TODO: Load this data more dynamically form.letterName.data = 1
# I.e. use a dictionary instead of explicitly-defined fields selected_letter = request.args.get('letter_name')
data = json.loads(letters[form.letterName.data - 1].contents)
# Ensures default value is set if selected_letter:
form.letterName.process_data(form.letterName.data) for i, letter in enumerate(letters):
if letter.title == selected_letter:
form.letterName.data = i + 1
break
form.company.data = data['company'] # TODO: Load this data more dynamically
form.body.data = data['body'] # I.e. use a dictionary instead of explicitly-defined fields
form.closingText.data = data['closingText'] data = json.loads(letters[form.letterName.data - 1].contents)
form.jobAndPronoun.data = data['jobAndPronoun']
form.mySkills.data = data['mySkills'] # Ensures default value is set
form.skillTypes.data = data['skillTypes'] form.letterName.process_data(form.letterName.data)
form.username.data = data['username']
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) return render_index(form=form)