IPN must match regex validator (if one is provided)

This commit is contained in:
Oliver Walters 2020-02-03 21:09:24 +11:00
parent 356b6cf15b
commit 9cef038d6a
3 changed files with 35 additions and 1 deletions

View File

@ -6,6 +6,10 @@ from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from common.models import InvenTreeSetting
import re
def allowable_url_schemes(): def allowable_url_schemes():
""" Return the list of allowable URL schemes. """ Return the list of allowable URL schemes.
@ -35,6 +39,17 @@ def validate_part_name(value):
_('Invalid character in part name') _('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): def validate_tree_name(value):
""" Prevent illegal characters in tree item names """ """ Prevent illegal characters in tree item names """

View File

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

View File

@ -349,7 +349,7 @@ class Part(models.Model):
on_delete=models.DO_NOTHING, on_delete=models.DO_NOTHING,
help_text=_('Part category')) 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')) revision = models.CharField(max_length=100, blank=True, help_text=_('Part revision or version number'))