mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Moved part name format validation to InvenTree.validators.py from common.models
validation to check if a field exists in part model
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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': {
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user