diff --git a/InvenTree/InvenTree/validators.py b/InvenTree/InvenTree/validators.py index 98c0532d14..543671bd5d 100644 --- a/InvenTree/InvenTree/validators.py +++ b/InvenTree/InvenTree/validators.py @@ -6,6 +6,10 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +from common.models import InvenTreeSetting + +import re + def allowable_url_schemes(): """ Return the list of allowable URL schemes. @@ -35,6 +39,17 @@ def validate_part_name(value): _('Invalid character in part name') ) +def validate_part_ipn(value): + """ Validate the Part IPN against regex rule """ + + pattern = InvenTreeSetting.get_setting('part_ipn_regex') + + if pattern: + match = re.search(pattern, value) + + if match is None: + raise ValidationError(_('IPN must match regex pattern') + " '{pat}'".format(pat=pattern)) + def validate_tree_name(value): """ Prevent illegal characters in tree item names """ diff --git a/InvenTree/part/migrations/0028_auto_20200203_1007.py b/InvenTree/part/migrations/0028_auto_20200203_1007.py new file mode 100644 index 0000000000..eca1788f06 --- /dev/null +++ b/InvenTree/part/migrations/0028_auto_20200203_1007.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.9 on 2020-02-03 10:07 + +import InvenTree.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('part', '0027_auto_20200202_1024'), + ] + + operations = [ + migrations.AlterField( + model_name='part', + name='IPN', + field=models.CharField(blank=True, help_text='Internal Part Number', max_length=100, validators=[InvenTree.validators.validate_part_ipn]), + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 2df55ad73b..fb270f06f0 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -349,7 +349,7 @@ class Part(models.Model): on_delete=models.DO_NOTHING, help_text=_('Part category')) - IPN = models.CharField(max_length=100, blank=True, help_text=_('Internal Part Number')) + IPN = models.CharField(max_length=100, blank=True, help_text=_('Internal Part Number'), validators=[validators.validate_part_ipn]) revision = models.CharField(max_length=100, blank=True, help_text=_('Part revision or version number'))