From 67b2d02a7fcbf2e1672b5cd9b09c4dcff5a5412b Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 25 Sep 2023 16:18:12 +1000 Subject: [PATCH] Allow Base URL to exist without TLD (#5615) - Custom validator just for this setting --- InvenTree/common/models.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 0f8a3ed3b6..c871371e6d 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -74,8 +74,20 @@ class MetaMixin(models.Model): ) -class EmptyURLValidator(URLValidator): - """Validator for filed with url - that can be empty.""" +class BaseURLValidator(URLValidator): + """Validator for the InvenTree base URL: + + - Allow empty value + - Allow value without specified TLD (top level domain) + """ + + def __init__(self, schemes=None, **kwargs): + """Custom init routine""" + + super().__init__(schemes, **kwargs) + + # Override default host_re value - allow optional tld regex + self.host_re = '(' + self.hostname_re + self.domain_re + f'({self.tld_re})?' + '|localhost)' def __call__(self, value): """Make sure empty values pass.""" @@ -1018,7 +1030,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'INVENTREE_BASE_URL': { 'name': _('Base URL'), 'description': _('Base URL for server instance'), - 'validator': EmptyURLValidator(), + 'validator': BaseURLValidator(), 'default': '', 'after_save': update_instance_url, },