mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
form: force option type to 'select' if there's 'choices'
This commit is contained in:
parent
3f417bb9b3
commit
3a5d353c4b
2 changed files with 20 additions and 10 deletions
|
@ -1989,10 +1989,10 @@ def test_option_default_type_with_choices_is_select():
|
||||||
}
|
}
|
||||||
answers = {"some_choices": "a", "some_legacy": "a"}
|
answers = {"some_choices": "a", "some_legacy": "a"}
|
||||||
|
|
||||||
options = ask_questions_and_parse_answers(questions, answers)
|
options, form = ask_questions_and_parse_answers(questions, answers)
|
||||||
for option in options:
|
for option in options:
|
||||||
assert option.type == "select"
|
assert option.type == "select"
|
||||||
assert option.value == "a"
|
assert form[option.id] == "a"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip # we should do something with this example
|
@pytest.mark.skip # we should do something with this example
|
||||||
|
|
|
@ -1305,15 +1305,25 @@ class OptionsModel(BaseModel):
|
||||||
def options_dict_to_list(
|
def options_dict_to_list(
|
||||||
options: dict[str, Any], optional: bool = False
|
options: dict[str, Any], optional: bool = False
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
return [
|
options_list = []
|
||||||
option
|
|
||||||
| {
|
for id_, data in options.items():
|
||||||
"id": option.get("id", id_),
|
option = data | {
|
||||||
"type": option.get("type", "select" if "choices" in option else "string"),
|
"id": data.get("id", id_),
|
||||||
"optional": option.get("optional", optional),
|
"type": data.get("type", OptionType.select if "choices" in data else OptionType.string),
|
||||||
|
"optional": data.get("optional", optional),
|
||||||
}
|
}
|
||||||
for id_, option in options.items()
|
|
||||||
]
|
# LEGACY (`choices` in option `string` used to be valid)
|
||||||
|
if "choices" in option and option["type"] == OptionType.string:
|
||||||
|
logger.warning(
|
||||||
|
f"Packagers: option {id_} has 'choices' but has type 'string', use 'select' instead to remove this warning."
|
||||||
|
)
|
||||||
|
option["type"] = OptionType.select
|
||||||
|
|
||||||
|
options_list.append(option)
|
||||||
|
|
||||||
|
return options_list
|
||||||
|
|
||||||
def __init__(self, **kwargs) -> None:
|
def __init__(self, **kwargs) -> None:
|
||||||
super().__init__(options=self.options_dict_to_list(kwargs))
|
super().__init__(options=self.options_dict_to_list(kwargs))
|
||||||
|
|
Loading…
Add table
Reference in a new issue