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
This commit is contained in:
Oliver Walters 2019-09-14 00:04:08 +10:00
parent ee17d5d3c3
commit 4ac8353099
3 changed files with 48 additions and 1 deletions

View File

@ -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
})

View File

@ -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'),
),
]

View File

@ -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)