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):
|
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(
|
letterName = SelectField(
|
||||||
'Letter Name:',
|
'Letter Name:',
|
||||||
[validators.optional()],
|
[validators.optional()],
|
||||||
|
@ -139,6 +144,14 @@ 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)]:
|
||||||
|
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'])
|
@writing_blueprint.route('/', methods=['GET'])
|
||||||
def index_get() -> Response:
|
def index_get() -> Response:
|
||||||
|
@ -147,12 +160,9 @@ def index_get() -> Response:
|
||||||
return render_index()
|
return render_index()
|
||||||
|
|
||||||
form = CLForm()
|
form = CLForm()
|
||||||
user = db.get_user(email_address)
|
letter_names = letter_choices(email_address)
|
||||||
letters = db.get_user_letters(user.id)
|
if len(letter_names) == 0:
|
||||||
if len(letters) == 0:
|
|
||||||
return render_index()
|
return render_index()
|
||||||
|
|
||||||
letter_names = [(i + 1, letter.title) for i, letter in enumerate(letters)]
|
|
||||||
form.letterName.choices = letter_names
|
form.letterName.choices = letter_names
|
||||||
|
|
||||||
form.letterName.data = 1
|
form.letterName.data = 1
|
||||||
|
@ -265,11 +275,13 @@ def git_update() -> None:
|
||||||
|
|
||||||
@writing_blueprint.route('/', methods=['POST'])
|
@writing_blueprint.route('/', methods=['POST'])
|
||||||
def generate_pdf() -> Response:
|
def generate_pdf() -> Response:
|
||||||
form = CLForm(request.form)
|
email_address = session.get('username')
|
||||||
if form.validate():
|
form = CLForm(request.form, email_address=email_address)
|
||||||
|
if not form.validate():
|
||||||
|
return render_index(form=form)
|
||||||
|
|
||||||
data = form.to_cl_data()
|
data = form.to_cl_data()
|
||||||
|
|
||||||
email_address = session.get('username')
|
|
||||||
if email_address:
|
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)
|
||||||
|
@ -291,5 +303,3 @@ def generate_pdf() -> Response:
|
||||||
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
|
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
return render_index(form=form)
|
|
||||||
|
|
Loading…
Reference in New Issue