diff --git a/src/backend/InvenTree/InvenTree/fields.py b/src/backend/InvenTree/InvenTree/fields.py index 7657a1a5c0..bc501fec46 100644 --- a/src/backend/InvenTree/InvenTree/fields.py +++ b/src/backend/InvenTree/InvenTree/fields.py @@ -36,9 +36,11 @@ class InvenTreeRestURLField(RestURLField): """Override default validation behavior for this field type.""" strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False) - if not strict_urls and data is not empty and '://' not in data: - # Validate as if there were a schema provided - data = 'http://' + data + if not strict_urls and data is not empty and data is not None: + data = str(data).strip() + if '://' not in data: + # Validate as if there were a schema provided + data = 'http://' + data return super().run_validation(data=data) diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index a428647161..85f2ca2802 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -435,17 +435,27 @@ class ValidatorTest(TestCase): link='www.google.com', ) + # Check that a blank URL is acceptable + Part.objects.create( + name=f'Part {n + 1}', description='Missing link', category=cat, link='' + ) + # With strict URL validation InvenTreeSetting.set_setting('INVENTREE_STRICT_URLS', True, None) with self.assertRaises(ValidationError): Part.objects.create( - name=f'Part {n + 1}', + name=f'Part {n + 2}', description='Link without schema', category=cat, link='www.google.com', ) + # Check that a blank URL is acceptable + Part.objects.create( + name=f'Part {n + 3}', description='Missing link', category=cat, link='' + ) + class FormatTest(TestCase): """Unit tests for custom string formatting functionality.""" diff --git a/src/backend/InvenTree/InvenTree/validators.py b/src/backend/InvenTree/InvenTree/validators.py index 6b54d99a97..6a0fe6cd9d 100644 --- a/src/backend/InvenTree/InvenTree/validators.py +++ b/src/backend/InvenTree/InvenTree/validators.py @@ -65,7 +65,10 @@ class AllowedURLValidator(validators.URLValidator): # Determine if 'strict' URL validation is required (i.e. if the URL must have a schema prefix) strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False) - if not strict_urls: + if value is not None: + value = str(value).strip() + + if value and not strict_urls: # Allow URLs which do not have a provided schema if '://' not in value: # Validate as if it were http