mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
form: reorder Option methods
This commit is contained in:
parent
5f4c83a4eb
commit
e4a0ad35ce
1 changed files with 52 additions and 52 deletions
|
@ -231,22 +231,6 @@ class BaseOption:
|
||||||
value = value.strip()
|
value = value.strip()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def _prompt(self, text):
|
|
||||||
prefill = ""
|
|
||||||
if self.current_value is not None:
|
|
||||||
prefill = self.humanize(self.current_value, self)
|
|
||||||
elif self.default is not None:
|
|
||||||
prefill = self.humanize(self.default, self)
|
|
||||||
self.value = Moulinette.prompt(
|
|
||||||
message=text,
|
|
||||||
is_password=self.hide_user_input_in_prompt,
|
|
||||||
confirm=False,
|
|
||||||
prefill=prefill,
|
|
||||||
is_multiline=(self.type == "text"),
|
|
||||||
autocomplete=self.choices or [],
|
|
||||||
help=_value_for_locale(self.help),
|
|
||||||
)
|
|
||||||
|
|
||||||
def ask_if_needed(self):
|
def ask_if_needed(self):
|
||||||
if self.visible and not evaluate_simple_js_expression(
|
if self.visible and not evaluate_simple_js_expression(
|
||||||
self.visible, context=self.context
|
self.visible, context=self.context
|
||||||
|
@ -301,25 +285,21 @@ class BaseOption:
|
||||||
|
|
||||||
return self.values
|
return self.values
|
||||||
|
|
||||||
def _value_pre_validator(self):
|
def _prompt(self, text):
|
||||||
if self.value in [None, ""] and not self.optional:
|
prefill = ""
|
||||||
raise YunohostValidationError("app_argument_required", name=self.name)
|
if self.current_value is not None:
|
||||||
|
prefill = self.humanize(self.current_value, self)
|
||||||
# we have an answer, do some post checks
|
elif self.default is not None:
|
||||||
if self.value not in [None, ""]:
|
prefill = self.humanize(self.default, self)
|
||||||
if self.choices and self.value not in self.choices:
|
self.value = Moulinette.prompt(
|
||||||
raise YunohostValidationError(
|
message=text,
|
||||||
"app_argument_choice_invalid",
|
is_password=self.hide_user_input_in_prompt,
|
||||||
name=self.name,
|
confirm=False,
|
||||||
value=self.value,
|
prefill=prefill,
|
||||||
choices=", ".join(str(choice) for choice in self.choices),
|
is_multiline=(self.type == "text"),
|
||||||
)
|
autocomplete=self.choices or [],
|
||||||
if self.pattern and not re.match(self.pattern["regexp"], str(self.value)):
|
help=_value_for_locale(self.help),
|
||||||
raise YunohostValidationError(
|
)
|
||||||
self.pattern["error"],
|
|
||||||
name=self.name,
|
|
||||||
value=self.value,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _format_text_for_user_input_in_cli(self):
|
def _format_text_for_user_input_in_cli(self):
|
||||||
text_for_user_input_in_cli = _value_for_locale(self.ask)
|
text_for_user_input_in_cli = _value_for_locale(self.ask)
|
||||||
|
@ -353,6 +333,26 @@ class BaseOption:
|
||||||
|
|
||||||
return text_for_user_input_in_cli
|
return text_for_user_input_in_cli
|
||||||
|
|
||||||
|
def _value_pre_validator(self):
|
||||||
|
if self.value in [None, ""] and not self.optional:
|
||||||
|
raise YunohostValidationError("app_argument_required", name=self.name)
|
||||||
|
|
||||||
|
# we have an answer, do some post checks
|
||||||
|
if self.value not in [None, ""]:
|
||||||
|
if self.choices and self.value not in self.choices:
|
||||||
|
raise YunohostValidationError(
|
||||||
|
"app_argument_choice_invalid",
|
||||||
|
name=self.name,
|
||||||
|
value=self.value,
|
||||||
|
choices=", ".join(str(choice) for choice in self.choices),
|
||||||
|
)
|
||||||
|
if self.pattern and not re.match(self.pattern["regexp"], str(self.value)):
|
||||||
|
raise YunohostValidationError(
|
||||||
|
self.pattern["error"],
|
||||||
|
name=self.name,
|
||||||
|
value=self.value,
|
||||||
|
)
|
||||||
|
|
||||||
def _value_post_validator(self):
|
def _value_post_validator(self):
|
||||||
if not self.redact:
|
if not self.redact:
|
||||||
return self.value
|
return self.value
|
||||||
|
@ -520,6 +520,15 @@ class BooleanOption(BaseOption):
|
||||||
yes_answers = ["1", "yes", "y", "true", "t", "on"]
|
yes_answers = ["1", "yes", "y", "true", "t", "on"]
|
||||||
no_answers = ["0", "no", "n", "false", "f", "off"]
|
no_answers = ["0", "no", "n", "false", "f", "off"]
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, question, context: Mapping[str, Any] = {}, hooks: Dict[str, Callable] = {}
|
||||||
|
):
|
||||||
|
super().__init__(question, context, hooks)
|
||||||
|
self.yes = question.get("yes", 1)
|
||||||
|
self.no = question.get("no", 0)
|
||||||
|
if self.default is None:
|
||||||
|
self.default = self.no
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def humanize(value, option={}):
|
def humanize(value, option={}):
|
||||||
option = option.__dict__ if isinstance(option, BaseOption) else option
|
option = option.__dict__ if isinstance(option, BaseOption) else option
|
||||||
|
@ -583,14 +592,8 @@ class BooleanOption(BaseOption):
|
||||||
choices="yes/no",
|
choices="yes/no",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(
|
def get(self, key, default=None):
|
||||||
self, question, context: Mapping[str, Any] = {}, hooks: Dict[str, Callable] = {}
|
return getattr(self, key, default)
|
||||||
):
|
|
||||||
super().__init__(question, context, hooks)
|
|
||||||
self.yes = question.get("yes", 1)
|
|
||||||
self.no = question.get("no", 0)
|
|
||||||
if self.default is None:
|
|
||||||
self.default = self.no
|
|
||||||
|
|
||||||
def _format_text_for_user_input_in_cli(self):
|
def _format_text_for_user_input_in_cli(self):
|
||||||
text_for_user_input_in_cli = super()._format_text_for_user_input_in_cli()
|
text_for_user_input_in_cli = super()._format_text_for_user_input_in_cli()
|
||||||
|
@ -600,9 +603,6 @@ class BooleanOption(BaseOption):
|
||||||
|
|
||||||
return text_for_user_input_in_cli
|
return text_for_user_input_in_cli
|
||||||
|
|
||||||
def get(self, key, default=None):
|
|
||||||
return getattr(self, key, default)
|
|
||||||
|
|
||||||
|
|
||||||
class DateOption(StringOption):
|
class DateOption(StringOption):
|
||||||
pattern = {
|
pattern = {
|
||||||
|
@ -678,6 +678,12 @@ class FileOption(BaseOption):
|
||||||
argument_type = "file"
|
argument_type = "file"
|
||||||
upload_dirs: List[str] = []
|
upload_dirs: List[str] = []
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, question, context: Mapping[str, Any] = {}, hooks: Dict[str, Callable] = {}
|
||||||
|
):
|
||||||
|
super().__init__(question, context, hooks)
|
||||||
|
self.accept = question.get("accept", "")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def clean_upload_dirs(cls):
|
def clean_upload_dirs(cls):
|
||||||
# Delete files uploaded from API
|
# Delete files uploaded from API
|
||||||
|
@ -685,12 +691,6 @@ class FileOption(BaseOption):
|
||||||
if os.path.exists(upload_dir):
|
if os.path.exists(upload_dir):
|
||||||
shutil.rmtree(upload_dir)
|
shutil.rmtree(upload_dir)
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, question, context: Mapping[str, Any] = {}, hooks: Dict[str, Callable] = {}
|
|
||||||
):
|
|
||||||
super().__init__(question, context, hooks)
|
|
||||||
self.accept = question.get("accept", "")
|
|
||||||
|
|
||||||
def _value_pre_validator(self):
|
def _value_pre_validator(self):
|
||||||
if self.value is None:
|
if self.value is None:
|
||||||
self.value = self.current_value
|
self.value = self.current_value
|
||||||
|
|
Loading…
Add table
Reference in a new issue