2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Report bugfix (#9013)

- Ensure default label templates exist
- Ensure default report templates exist
This commit is contained in:
Oliver 2025-02-02 23:28:28 +11:00 committed by GitHub
parent 1b5019ba52
commit 2a6434ead8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,7 @@ from pathlib import Path
from django.apps import AppConfig from django.apps import AppConfig
from django.core.exceptions import AppRegistryNotReady from django.core.exceptions import AppRegistryNotReady
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.db.utils import IntegrityError, OperationalError, ProgrammingError from django.db.utils import IntegrityError, OperationalError, ProgrammingError
from maintenance_mode.core import maintenance_mode_on, set_maintenance_mode from maintenance_mode.core import maintenance_mode_on, set_maintenance_mode
@ -55,6 +56,18 @@ class ReportConfig(AppConfig):
set_maintenance_mode(False) set_maintenance_mode(False)
def file_from_template(self, dir_name: str, file_name: str) -> ContentFile:
"""Construct a new ContentFile from a template file."""
logger.info('Creating %s template file: %s', dir_name, file_name)
return ContentFile(
Path(__file__)
.parent.joinpath('templates', dir_name, file_name)
.open('r')
.read(),
os.path.basename(file_name),
)
def create_default_labels(self): def create_default_labels(self):
"""Create default label templates.""" """Create default label templates."""
# Test if models are ready # Test if models are ready
@ -106,29 +119,25 @@ class ReportConfig(AppConfig):
] ]
for template in label_templates: for template in label_templates:
# Ignore matching templates which are already in the database
if report.models.LabelTemplate.objects.filter(
name=template['name']
).exists():
continue
filename = template.pop('file') filename = template.pop('file')
template_file = Path(__file__).parent.joinpath( # Template already exists in the database - check that the file exists too
'templates', 'label', filename if existing_template := report.models.LabelTemplate.objects.filter(
name=template['name'], model_type=template['model_type']
).first():
if not default_storage.exists(existing_template.template.name):
# The file does not exist in the storage system - add it in
existing_template.template = self.file_from_template(
'label', filename
) )
existing_template.save()
if not template_file.exists():
logger.warning("Missing template file: '%s'", template['name'])
continue continue
# Read the existing template file # Otherwise, create a new entry
data = template_file.open('r').read()
try: try:
# Create a new entry # Create a new entry
report.models.LabelTemplate.objects.create( report.models.LabelTemplate.objects.create(
**template, template=ContentFile(data, os.path.basename(filename)) **template, template=self.file_from_template('label', filename)
) )
logger.info("Creating new label template: '%s'", template['name']) logger.info("Creating new label template: '%s'", template['name'])
except Exception: except Exception:
@ -202,29 +211,24 @@ class ReportConfig(AppConfig):
] ]
for template in report_templates: for template in report_templates:
# Ignore matching templates which are already in the database
if report.models.ReportTemplate.objects.filter(
name=template['name']
).exists():
continue
filename = template.pop('file') filename = template.pop('file')
template_file = Path(__file__).parent.joinpath( # Template already exists in the database - check that the file exists too
'templates', 'report', filename if existing_template := report.models.ReportTemplate.objects.filter(
name=template['name'], model_type=template['model_type']
).first():
if not default_storage.exists(existing_template.template.name):
# The file does not exist in the storage system - add it in
existing_template.template = self.file_from_template(
'report', filename
) )
existing_template.save()
if not template_file.exists():
logger.warning("Missing template file: '%s'", template['name'])
continue continue
# Read the existing template file # Otherwise, create a new entry
data = template_file.open('r').read()
# Create a new entry
try: try:
report.models.ReportTemplate.objects.create( report.models.ReportTemplate.objects.create(
**template, template=ContentFile(data, os.path.basename(filename)) **template, template=self.file_from_template('report', filename)
) )
logger.info("Created new report template: '%s'", template['name']) logger.info("Created new report template: '%s'", template['name'])
except Exception: except Exception: