2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 21:15:41 +00:00

Added PART_NAME_FORMAT to Inventree settings and exposed the same in settings window with a validator

This commit is contained in:
rocheparadox
2021-10-12 19:06:23 +05:30
parent f4239fbfd3
commit 2bf51b0ac3
4 changed files with 39 additions and 29 deletions

View File

@ -9,6 +9,7 @@ from __future__ import unicode_literals
import os
import decimal
import math
import re
from django.db import models, transaction
from django.contrib.auth.models import User
@ -487,6 +488,26 @@ 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:
@ -702,6 +723,14 @@ class InvenTreeSetting(BaseInvenTreeSetting):
'validator': bool
},
'PART_NAME_FORMAT': {
'name': _('Part Name Display Format'),
'description': _('Format to display the part name'),
'default': "{{ part.IPN if part.IPN }} {{ '|' if part.IPN }} {{ part.name }} {{ '|' if part.revision }}"
" {{ part.revision }}",
'validator': validate_part_name_format
},
'REPORT_DEBUG_MODE': {
'name': _('Debug Mode'),
'description': _('Generate reports in debug mode (HTML output)'),