mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
form: rework context/values/hooks in prompt_or_validate_form
This commit is contained in:
parent
73b795be1d
commit
98d3b4ffc8
1 changed files with 10 additions and 10 deletions
|
@ -1471,9 +1471,6 @@ def prompt_or_validate_form(
|
||||||
context: Context = {},
|
context: Context = {},
|
||||||
hooks: Hooks = {},
|
hooks: Hooks = {},
|
||||||
) -> FormModel:
|
) -> FormModel:
|
||||||
answers = {**prefilled_answers}
|
|
||||||
values = {}
|
|
||||||
|
|
||||||
for option in options:
|
for option in options:
|
||||||
interactive = Moulinette.interface.type == "cli" and os.isatty(1)
|
interactive = Moulinette.interface.type == "cli" and os.isatty(1)
|
||||||
|
|
||||||
|
@ -1505,7 +1502,7 @@ def prompt_or_validate_form(
|
||||||
if isinstance(option, BaseInputOption):
|
if isinstance(option, BaseInputOption):
|
||||||
# FIXME normalized needed, form[option.id] should already be normalized
|
# FIXME normalized needed, form[option.id] should already be normalized
|
||||||
# only update the context with the value
|
# only update the context with the value
|
||||||
context[option.id] = form[option.id]
|
context[option.id] = option.normalize(form[option.id])
|
||||||
|
|
||||||
# FIXME here we could error out
|
# FIXME here we could error out
|
||||||
if option.id in prefilled_answers:
|
if option.id in prefilled_answers:
|
||||||
|
@ -1542,7 +1539,8 @@ def prompt_or_validate_form(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Normalize and validate
|
# Normalize and validate
|
||||||
values[option.id] = form[option.id] = option.normalize(value, option)
|
form[option.id] = option.normalize(value, option)
|
||||||
|
context[option.id] = form[option.id]
|
||||||
except (ValidationError, YunohostValidationError) as e:
|
except (ValidationError, YunohostValidationError) as e:
|
||||||
# If in interactive cli, re-ask the current question
|
# If in interactive cli, re-ask the current question
|
||||||
if i < 4 and interactive:
|
if i < 4 and interactive:
|
||||||
|
@ -1562,11 +1560,13 @@ def prompt_or_validate_form(
|
||||||
# Search for post actions in hooks
|
# Search for post actions in hooks
|
||||||
post_hook = f"post_ask__{option.id}"
|
post_hook = f"post_ask__{option.id}"
|
||||||
if post_hook in hooks:
|
if post_hook in hooks:
|
||||||
values.update(hooks[post_hook](option))
|
# Hooks looks like they can return multiple values, validate those
|
||||||
# FIXME reapply new values to form to validate it
|
values = hooks[post_hook](option)
|
||||||
|
for option_id, value in values.items():
|
||||||
answers.update(values)
|
option = next(opt for opt in options if option.id == option_id)
|
||||||
context.update(values)
|
if option and isinstance(option, BaseInputOption):
|
||||||
|
form[option.id] = option.normalize(value, option)
|
||||||
|
context[option.id] = form[option.id]
|
||||||
|
|
||||||
return form
|
return form
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue