From 26a49929611434361182585eba5a521cff557462 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 23 Sep 2021 05:33:20 +0200 Subject: [PATCH] Refactor _normalize_domain_path into DomainQuestion + PathQuestion normalizers --- src/yunohost/app.py | 37 ++++++++++++++---------------------- src/yunohost/utils/config.py | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index b0ae7f0d5..396ce04e7 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -55,6 +55,8 @@ from yunohost.utils import packages from yunohost.utils.config import ( ConfigPanel, ask_questions_and_parse_answers, + DomainQuestion, + PathQuestion ) from yunohost.utils.i18n import _value_for_locale from yunohost.utils.error import YunohostError, YunohostValidationError @@ -441,8 +443,11 @@ def app_change_url(operation_logger, app, domain, path): old_path = app_setting(app, "path") # Normalize path and domain format - old_domain, old_path = _normalize_domain_path(old_domain, old_path) - domain, path = _normalize_domain_path(domain, path) + + domain = DomainQuestion.normalize(domain) + old_domain = DomainQuestion.normalize(old_domain) + path = PathQuestion.normalize(path) + old_path = PathQuestion.normalize(old_path) if (domain, path) == (old_domain, old_path): raise YunohostValidationError( @@ -1459,7 +1464,8 @@ def app_register_url(app, domain, path): permission_sync_to_user, ) - domain, path = _normalize_domain_path(domain, path) + domain = DomainQuestion.normalize(domain) + path = PathQuestion.normalize(path) # We cannot change the url of an app already installed simply by changing # the settings... @@ -2381,7 +2387,9 @@ def _validate_and_normalize_webpath(args_dict, app_folder): domain = domain_args[0][1] path = path_args[0][1] - domain, path = _normalize_domain_path(domain, path) + + domain = DomainQuestion.normalize(domain) + path = PathQuestion.normalize(path) # Check the url is available _assert_no_conflicting_apps(domain, path) @@ -2415,24 +2423,6 @@ def _validate_and_normalize_webpath(args_dict, app_folder): _assert_no_conflicting_apps(domain, "/", full_domain=True) -def _normalize_domain_path(domain, path): - - # We want url to be of the format : - # some.domain.tld/foo - - # Remove http/https prefix if it's there - if domain.startswith("https://"): - domain = domain[len("https://") :] - elif domain.startswith("http://"): - domain = domain[len("http://") :] - - # Remove trailing slashes - domain = domain.rstrip("/").lower() - path = "/" + path.strip("/") - - return domain, path - - def _get_conflicting_apps(domain, path, ignore_app=None): """ Return a list of all conflicting apps with a domain/path (it can be empty) @@ -2445,7 +2435,8 @@ def _get_conflicting_apps(domain, path, ignore_app=None): from yunohost.domain import _assert_domain_exists - domain, path = _normalize_domain_path(domain, path) + domain = DomainQuestion.normalize(domain) + path = PathQuestion.normalize(path) # Abort if domain is unknown _assert_domain_exists(domain) diff --git a/src/yunohost/utils/config.py b/src/yunohost/utils/config.py index e83d794ff..1a39c0da4 100644 --- a/src/yunohost/utils/config.py +++ b/src/yunohost/utils/config.py @@ -728,6 +728,10 @@ class PathQuestion(Question): argument_type = "path" default_value = "" + @staticmethod + def normalize(value, option={}): + return "/" + value.strip("/") + class BooleanQuestion(Question): argument_type = "boolean" @@ -837,6 +841,18 @@ class DomainQuestion(Question): error=m18n.n("domain_name_unknown", domain=self.value), ) + @staticmethod + def normalize(value, option={}): + if value.startswith("https://"): + value = value[len("https://"):] + elif value.startswith("http://"): + value = value[len("http://"):] + + # Remove trailing slashes + value = value.rstrip("/").lower() + + return value + class UserQuestion(Question): argument_type = "user"