Try to init CLForm choices sooner.
This commit is contained in:
parent
04c16b50fe
commit
ef0b6503cb
|
@ -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,11 +275,13 @@ def git_update() -> None:
|
|||
|
||||
@writing_blueprint.route('/', methods=['POST'])
|
||||
def generate_pdf() -> Response:
|
||||
form = CLForm(request.form)
|
||||
if form.validate():
|
||||
email_address = session.get('username')
|
||||
form = CLForm(request.form, email_address=email_address)
|
||||
if not form.validate():
|
||||
return render_index(form=form)
|
||||
|
||||
data = form.to_cl_data()
|
||||
|
||||
email_address = session.get('username')
|
||||
if email_address:
|
||||
user = db.get_user(email_address)
|
||||
letters = db.get_user_letters(user.id)
|
||||
|
@ -291,5 +303,3 @@ def generate_pdf() -> Response:
|
|||
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
|
||||
|
||||
return resp
|
||||
|
||||
return render_index(form=form)
|
||||
|
|
Loading…
Reference in New Issue