From 8cad687e43e848df1d2f7571c8b5ed5d62ec2301 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Thu, 14 Oct 2021 09:23:21 +0530 Subject: [PATCH] Moved part name format validation to InvenTree.validators.py from common.models validation to check if a field exists in part model --- InvenTree/InvenTree/validators.py | 31 +++++++++++++++++++++++++++++++ InvenTree/common/models.py | 22 +--------------------- InvenTree/common/test_views.py | 2 +- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/InvenTree/InvenTree/validators.py b/InvenTree/InvenTree/validators.py index 1b6a6b3f0b..6ad49bc837 100644 --- a/InvenTree/InvenTree/validators.py +++ b/InvenTree/InvenTree/validators.py @@ -5,6 +5,7 @@ Custom field validators for InvenTree from django.conf import settings from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ +from django.core.exceptions import FieldDoesNotExist from moneyed import CURRENCIES @@ -156,3 +157,33 @@ def validate_overage(value): raise ValidationError( _("Overage must be an integer value or a percentage") ) + + +def validate_part_name_format(self): + """ + Validate part name format. + Make sure that each template container has a field of Part Model + """ + + jinja_template_regex = re.compile('{{.*?}}') + field_name_regex = re.compile('(?<=part\\.)[A-z]*') + for jinja_template in jinja_template_regex.findall(str(self)): + # make sure at least one and only one field is present inside the parser + field_names = field_name_regex.findall(jinja_template) + if len(field_names) < 1: + raise ValidationError({ + 'value': 'At least one field must be present inside a jinja template container i.e {{}}' + }) + + # Make sure that the field_name exists in Part model + from part.models import Part + + for field_name in field_names: + try: + Part._meta.get_field(field_name) + except FieldDoesNotExist: + raise ValidationError({ + 'value': f'{field_name} does not exist in Part Model' + }) + + return True diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 8ccbf99986..f85f6d9607 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -488,26 +488,6 @@ class InvenTreeSetting(BaseInvenTreeSetting): even if that key does not exist. """ - def validate_part_name_format(self): - """ - Validate part name format. - Make sure that each template container has a field of Part Model - """ - - jinja_template_regex = re.compile('{{.*?}}') - field_name_regex = re.compile('(?<=part\\.)[A-z]*') - for jinja_template in jinja_template_regex.findall(str(self)): - # make sure at least one and only one field is present inside the parser - field_name = field_name_regex.findall(jinja_template) - if len(field_name) < 1: - raise ValidationError({ - 'value': 'At least one field must be present inside a jinja template container i.e {{}}' - }) - - # TODO: Make sure that the field_name exists in Part model - - return True - """ Dict of all global settings values: @@ -728,7 +708,7 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'description': _('Format to display the part name'), 'default': "{{ part.IPN if part.IPN }}{{ ' | ' if part.IPN }}{{ part.name }}{{ ' | ' if part.revision }}" "{{ part.revision if part.revision }}", - 'validator': validate_part_name_format + 'validator': InvenTree.fields.validate_part_name_format }, 'REPORT_DEBUG_MODE': { diff --git a/InvenTree/common/test_views.py b/InvenTree/common/test_views.py index 0f592e9bb3..76a0a4516e 100644 --- a/InvenTree/common/test_views.py +++ b/InvenTree/common/test_views.py @@ -150,7 +150,7 @@ class SettingsViewTest(TestCase): url = self.get_url(setting.pk) # Try posting an invalid part name format - invalid_values = ['{{asset.IPN}}', '{{part}}', '{{"|"}}'] + invalid_values = ['{{asset.IPN}}', '{{part}}', '{{"|"}}', '{{part.falcon}}'] for invalid_value in invalid_values: self.post(url, {'value': invalid_value}, valid=False)