From 4ac8353099f3a3e04d4f4def93662c90d3118e7d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 14 Sep 2019 00:04:08 +1000 Subject: [PATCH] Create a custom URL field, which allows the user-specified validators - Ref: https://stackoverflow.com/questions/41756572/django-urlfield-with-custom-scheme - Apply this to the URL field in the Part model --- InvenTree/InvenTree/fields.py | 27 +++++++++++++++++++ .../migrations/0023_auto_20190913_1401.py | 19 +++++++++++++ InvenTree/part/models.py | 3 ++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 InvenTree/InvenTree/fields.py create mode 100644 InvenTree/part/migrations/0023_auto_20190913_1401.py diff --git a/InvenTree/InvenTree/fields.py b/InvenTree/InvenTree/fields.py new file mode 100644 index 0000000000..7a2fa4b89a --- /dev/null +++ b/InvenTree/InvenTree/fields.py @@ -0,0 +1,27 @@ +""" Custom fields used in InvenTree """ + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from .validators import allowable_url_schemes + +from django.forms.fields import URLField as FormURLField +from django.db import models as models +from django.core import validators + + +class InvenTreeURLFormField(FormURLField): + """ Custom URL form field with custom scheme validators """ + + default_validators = [validators.URLValidator(schemes=allowable_url_schemes())] + + +class InvenTreeURLField(models.URLField): + """ Custom URL field which has custom scheme validators """ + + default_validators = [validators.URLValidator(schemes=allowable_url_schemes())] + + def formfield(self, **kwargs): + return super().formfield(**{ + 'form_class': InvenTreeURLFormField + }) diff --git a/InvenTree/part/migrations/0023_auto_20190913_1401.py b/InvenTree/part/migrations/0023_auto_20190913_1401.py new file mode 100644 index 0000000000..8d5c5992d6 --- /dev/null +++ b/InvenTree/part/migrations/0023_auto_20190913_1401.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.5 on 2019-09-13 14:01 + +import InvenTree.fields +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0022_auto_20190908_0918'), + ] + + operations = [ + migrations.AlterField( + model_name='part', + name='URL', + field=InvenTree.fields.InvenTreeURLField(blank=True, help_text='Link to extenal URL'), + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index f57da3cfdb..76d9f606d0 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -34,6 +34,7 @@ import hashlib from InvenTree import helpers from InvenTree import validators from InvenTree.models import InvenTreeTree +from InvenTree.fields import InvenTreeURLField from InvenTree.status_codes import BuildStatus, StockStatus, OrderStatus @@ -353,7 +354,7 @@ class Part(models.Model): revision = models.CharField(max_length=100, blank=True, help_text='Part revision or version number') - URL = models.URLField(blank=True, help_text='Link to extenal URL') + URL = InvenTreeURLField(blank=True, help_text='Link to extenal URL') image = models.ImageField(upload_to=rename_part_image, max_length=255, null=True, blank=True)