mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-27 19:16:44 +00:00
Fix for URL validation (#9539)
* FIx for URL validation * Further fixes
This commit is contained in:
parent
9a49c9f19c
commit
8d48f9cecd
@ -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)
|
||||
|
||||
|
@ -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."""
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user