Try to init CLForm choices sooner.

This commit is contained in:
Sage Vaillancourt 2022-09-29 12:53:22 -04:00
parent 04c16b50fe
commit ef0b6503cb
1 changed files with 38 additions and 28 deletions

View File

@ -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,31 +275,31 @@ 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)
data = form.to_cl_data() if not form.validate():
return render_index(form=form)
email_address = session.get('username') data = form.to_cl_data()
if email_address:
user = db.get_user(email_address)
letters = db.get_user_letters(user.id)
letter_json = jsonify(data).get_data(True)
if len(letters) == 0:
db.add_letter(user.id, 'My Cover Letter', letter_json)
else:
letter = letters[data.selectedLetter]
# TODO: Support title editing
db.edit_letter(letter.id, letter.title, letter_json)
try: if email_address:
resp = data.generate_pdf() user = db.get_user(email_address)
except ValueError as e: letters = db.get_user_letters(user.id)
resp = render_index(form=form, letter_errors=e.args[0]) letter_json = jsonify(data).get_data(True)
if len(letters) == 0:
db.add_letter(user.id, 'My Cover Letter', letter_json)
else:
letter = letters[data.selectedLetter]
# TODO: Support title editing
db.edit_letter(letter.id, letter.title, letter_json)
# Save entered data as cookies on user's machine try:
for pair in data.get_pairs(): resp = data.generate_pdf()
resp.set_cookie(pair[0], urllib.parse.quote(pair[1])) except ValueError as e:
resp = render_index(form=form, letter_errors=e.args[0])
return resp # Save entered data as cookies on user's machine
for pair in data.get_pairs():
resp.set_cookie(pair[0], urllib.parse.quote(pair[1]))
return render_index(form=form) return resp