mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
Fix for URL validation (#9539)
* FIx for URL validation * Further fixes
This commit is contained in:
parent
9a49c9f19c
commit
8d48f9cecd
@ -36,7 +36,9 @@ class InvenTreeRestURLField(RestURLField):
|
|||||||
"""Override default validation behavior for this field type."""
|
"""Override default validation behavior for this field type."""
|
||||||
strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False)
|
strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False)
|
||||||
|
|
||||||
if not strict_urls and data is not empty and '://' not in 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
|
# Validate as if there were a schema provided
|
||||||
data = 'http://' + data
|
data = 'http://' + data
|
||||||
|
|
||||||
|
@ -435,17 +435,27 @@ class ValidatorTest(TestCase):
|
|||||||
link='www.google.com',
|
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
|
# With strict URL validation
|
||||||
InvenTreeSetting.set_setting('INVENTREE_STRICT_URLS', True, None)
|
InvenTreeSetting.set_setting('INVENTREE_STRICT_URLS', True, None)
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
Part.objects.create(
|
Part.objects.create(
|
||||||
name=f'Part {n + 1}',
|
name=f'Part {n + 2}',
|
||||||
description='Link without schema',
|
description='Link without schema',
|
||||||
category=cat,
|
category=cat,
|
||||||
link='www.google.com',
|
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):
|
class FormatTest(TestCase):
|
||||||
"""Unit tests for custom string formatting functionality."""
|
"""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)
|
# 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)
|
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
|
# Allow URLs which do not have a provided schema
|
||||||
if '://' not in value:
|
if '://' not in value:
|
||||||
# Validate as if it were http
|
# Validate as if it were http
|
||||||
|
Loading…
x
Reference in New Issue
Block a user