From a5639c380f79b60585a325823db997f2dd9a7b18 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 20 Oct 2020 00:13:43 +1100 Subject: [PATCH] Add regex validator to build order reference field --- InvenTree/InvenTree/validators.py | 42 +++++++++++++++++++ .../migrations/0019_auto_20201019_1302.py | 23 ++++++++++ InvenTree/build/models.py | 5 +++ 3 files changed, 70 insertions(+) create mode 100644 InvenTree/build/migrations/0019_auto_20201019_1302.py diff --git a/InvenTree/InvenTree/validators.py b/InvenTree/InvenTree/validators.py index 548bce12ab..be96affbbd 100644 --- a/InvenTree/InvenTree/validators.py +++ b/InvenTree/InvenTree/validators.py @@ -52,6 +52,48 @@ def validate_part_ipn(value): raise ValidationError(_('IPN must match regex pattern') + " '{pat}'".format(pat=pattern)) +def validate_build_order_reference(value): + """ + Validate the 'reference' field of a BuildOrder + """ + + pattern = common.models.InvenTreeSetting.get_setting('buildorder_reference_regex') + + if pattern: + match = re.search(pattern, value) + + if match is None: + raise ValidationError(_('Reference must match pattern') + f" '{pattern}'") + + +def validate_purchase_order_reference(value): + """ + Validate the 'reference' field of a PurchaseOrder + """ + + pattern = common.models.InvenTreeSetting.get_setting('purchaseorder_reference_regex') + + if pattern: + match = re.search(pattern, value) + + if match is None: + raise ValidationError(_('Reference must match pattern') + f" '{pattern}'") + + +def validate_sales_order_reference(value): + """ + Validate the 'reference' field of a SalesOrder + """ + + pattern = common.models.InvenTreeSetting.get_setting('salesorder_reference_regex') + + if pattern: + match = re.search(pattern, value) + + if match is None: + raise ValidationError(_('Reference must match pattern') + f" '{pattern}'") + + def validate_tree_name(value): """ Prevent illegal characters in tree item names """ diff --git a/InvenTree/build/migrations/0019_auto_20201019_1302.py b/InvenTree/build/migrations/0019_auto_20201019_1302.py new file mode 100644 index 0000000000..c767d3a3d9 --- /dev/null +++ b/InvenTree/build/migrations/0019_auto_20201019_1302.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.7 on 2020-10-19 13:02 + +import InvenTree.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('build', '0018_build_reference'), + ] + + operations = [ + migrations.AlterModelOptions( + name='build', + options={'verbose_name': 'Build Order', 'verbose_name_plural': 'Build Orders'}, + ), + migrations.AlterField( + model_name='build', + name='reference', + field=models.CharField(help_text='Build Order Reference', max_length=64, unique=True, validators=[InvenTree.validators.validate_build_order_reference], verbose_name='Reference'), + ), + ] diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 40666d5fce..62f6cdea09 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -23,6 +23,8 @@ from mptt.models import MPTTModel, TreeForeignKey from InvenTree.status_codes import BuildStatus from InvenTree.helpers import increment +from InvenTree.validators import validate_build_order_reference + import InvenTree.fields from stock import models as StockModels @@ -81,6 +83,9 @@ class Build(MPTTModel): blank=False, help_text=_('Build Order Reference'), verbose_name=_('Reference'), + validators=[ + validate_build_order_reference + ] ) title = models.CharField(