diff --git a/InvenTree/InvenTree/fields.py b/InvenTree/InvenTree/fields.py index caf625f2b8..d754737cfa 100644 --- a/InvenTree/InvenTree/fields.py +++ b/InvenTree/InvenTree/fields.py @@ -4,7 +4,6 @@ import sys from decimal import Decimal from django import forms -from django.core import validators from django.db import models as models from django.utils.translation import gettext_lazy as _ @@ -15,7 +14,7 @@ from rest_framework.fields import URLField as RestURLField import InvenTree.helpers -from .validators import allowable_url_schemes +from .validators import AllowedURLValidator, allowable_url_schemes class InvenTreeRestURLField(RestURLField): @@ -34,7 +33,7 @@ class InvenTreeRestURLField(RestURLField): class InvenTreeURLField(models.URLField): """Custom URL field which has custom scheme validators.""" - default_validators = [validators.URLValidator(schemes=allowable_url_schemes())] + default_validators = [AllowedURLValidator()] def __init__(self, **kwargs): """Initialization method for InvenTreeURLField""" diff --git a/InvenTree/InvenTree/validators.py b/InvenTree/InvenTree/validators.py index ecc624d966..82368e90ac 100644 --- a/InvenTree/InvenTree/validators.py +++ b/InvenTree/InvenTree/validators.py @@ -4,6 +4,7 @@ import re from decimal import Decimal, InvalidOperation from django.conf import settings +from django.core import validators from django.core.exceptions import FieldDoesNotExist, ValidationError from django.utils.translation import gettext_lazy as _ @@ -37,6 +38,14 @@ def allowable_url_schemes(): return schemes +class AllowedURLValidator(validators.URLValidator): + """Custom URL validator to allow for custom schemes.""" + def __call__(self, value): + """Validate the URL.""" + self.schemes = allowable_url_schemes() + super().__call__(value) + + def validate_part_name(value): """Prevent some illegal characters in part names.""" for c in ['|', '#', '$', '{', '}']: diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index cf0773b246..eb9111fae0 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -4,6 +4,7 @@ import datetime from django.core.exceptions import ValidationError from django.db.models import Sum +from django.test import override_settings from build.models import Build from InvenTree.helpers import InvenTreeTestCase @@ -140,7 +141,7 @@ class StockTest(StockTestBase): item.save() item.full_clean() - # Check that valid URLs pass + # Check that valid URLs pass - and check custon schemes for good_url in [ 'https://test.com', 'https://digikey.com/datasheets?file=1010101010101.bin', @@ -163,6 +164,14 @@ class StockTest(StockTestBase): item.link = long_url item.save() + @override_settings(EXTRA_URL_SCHEMES=['ssh']) + def test_exteneded_schema(self): + """Test that extended URL schemes are allowed""" + item = StockItem.objects.get(pk=1) + item.link = 'ssh://user:pwd@deb.org:223' + item.save() + item.full_clean() + def test_expiry(self): """Test expiry date functionality for StockItem model.""" today = datetime.datetime.now().date()