[enh] Validate server side specific input html5 field

This commit is contained in:
ljf 2021-09-12 19:06:25 +02:00
parent f8fed701b9
commit ca5d7b32dc
2 changed files with 52 additions and 6 deletions

View file

@ -146,6 +146,11 @@
"config_missing_init_value": "Config panel question '{question}' should be initialize with a value during install or upgrade.",
"config_no_panel": "No config panel found.",
"config_unknown_filter_key": "The filter key '{filter_key}' is incorrect.",
"config_validate_color": "Should be a valid RGB hexadecimal color",
"config_validate_date": "Should be a valid date like in the format YYYY-MM-DD",
"config_validate_email": "Should be a valid email",
"config_validate_time": "Should be a valid time like XX:YY",
"config_validate_url": "Should be a valid web URL",
"config_version_not_supported": "Config panel versions '{version}' are not supported.",
"confirm_app_install_danger": "DANGER! This app is known to be still experimental (if not explicitly not working)! You should probably NOT install it unless you know what you are doing. NO SUPPORT will be provided if this app doesn't work or breaks your system... If you are willing to take that risk anyway, type '{answers}'",
"confirm_app_install_thirdparty": "DANGER! This app is not part of YunoHost's app catalog. Installing third-party apps may compromise the integrity and security of your system. You should probably NOT install it unless you know what you are doing. NO SUPPORT will be provided if this app doesn't work or breaks your system... If you are willing to take that risk anyway, type '{answers}'",

View file

@ -388,6 +388,7 @@ class ConfigPanel:
class Question(object):
hide_user_input_in_prompt = False
operation_logger = None
pattern = None
def __init__(self, question, user_answers):
self.name = question["name"]
@ -396,7 +397,7 @@ class Question(object):
self.current_value = question.get("current_value")
self.optional = question.get("optional", False)
self.choices = question.get("choices", [])
self.pattern = question.get("pattern")
self.pattern = question.get("pattern", self.pattern)
self.ask = question.get("ask", {"en": self.name})
self.help = question.get("help")
self.value = user_answers.get(self.name)
@ -536,6 +537,46 @@ class StringQuestion(Question):
argument_type = "string"
default_value = ""
class EmailQuestion(StringQuestion):
pattern = {
"regexp": "^.+@.+",
"error": "config_validate_email"
}
class URLQuestion(StringQuestion):
pattern = {
"regexp": "^https?://.*$",
"error": "config_validate_url"
}
class DateQuestion(StringQuestion):
pattern = {
"regexp": "^\d{4}-\d\d-\d\d$",
"error": "config_validate_date"
}
def _prevalidate(self):
from datetime import datetime
super()._prevalidate()
if self.value not in [None, ""]:
try:
datetime.strptime(self.value, '%Y-%m-%d')
except ValueError:
raise YunohostValidationError("config_validate_date")
class TimeQuestion(StringQuestion):
pattern = {
"regexp": "^(1[12]|0?\d):[0-5]\d$",
"error": "config_validate_time"
}
class ColorQuestion(StringQuestion):
pattern = {
"regexp": "^#[ABCDEFabcdef\d]{3,6}$",
"error": "config_validate_color"
}
class TagsQuestion(Question):
argument_type = "tags"
@ -880,11 +921,11 @@ ARGUMENTS_TYPE_PARSERS = {
"text": StringQuestion,
"select": StringQuestion,
"tags": TagsQuestion,
"email": StringQuestion,
"url": StringQuestion,
"date": StringQuestion,
"time": StringQuestion,
"color": StringQuestion,
"email": EmailQuestion,
"url": URLQuestion,
"date": DateQuestion,
"time": TimeQuestion,
"color": ColorQuestion,
"password": PasswordQuestion,
"path": PathQuestion,
"boolean": BooleanQuestion,