Correct letter_choices behavior

This commit is contained in:
Sage Vaillancourt 2022-09-29 12:54:59 -04:00
parent ef0b6503cb
commit 31f6737f92
1 changed files with 7 additions and 5 deletions

View File

@ -21,7 +21,9 @@ class CLForm(Form):
def __init__(self, form: dict[str, str] = None, email_address: str = None, **kwargs): def __init__(self, form: dict[str, str] = None, email_address: str = None, **kwargs):
super().__init__(form, **kwargs) super().__init__(form, **kwargs)
if email_address: if email_address:
self.letterName.choices = letter_choices(email_address) user = db.get_user(email_address)
letters = db.get_user_letters(user.id)
self.letterName.choices = letter_choices(letters)
letterName = SelectField( letterName = SelectField(
'Letter Name:', 'Letter Name:',
@ -144,9 +146,7 @@ def add_letter() -> Response:
return redirect(f'/?letter_name={new_letter_name}') return redirect(f'/?letter_name={new_letter_name}')
def letter_choices(email_address: str) -> list[(int, str)]: def letter_choices(letters: list[db.Letter]) -> list[(int, str)]:
user = db.get_user(email_address)
letters = db.get_user_letters(user.id)
if len(letters) == 0: if len(letters) == 0:
return [] return []
@ -160,7 +160,9 @@ def index_get() -> Response:
return render_index() return render_index()
form = CLForm() form = CLForm()
letter_names = letter_choices(email_address) user = db.get_user(email_address)
letters = db.get_user_letters(user.id)
letter_names = letter_choices(letters)
if len(letter_names) == 0: if len(letter_names) == 0:
return render_index() return render_index()
form.letterName.choices = letter_names form.letterName.choices = letter_names