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

fix(backend): Better error handling for report template generation (#9534)

found in https://github.com/inventree/InvenTree/actions/runs/14535795056/job/40783805508?pr=9523
This commit is contained in:
Matthias Mair 2025-04-20 16:00:05 +02:00 committed by GitHub
parent 88102ad9aa
commit 1dae1bc906
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,6 @@
import logging import logging
import os import os
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
@ -15,6 +14,7 @@ from maintenance_mode.core import maintenance_mode_on, set_maintenance_mode
import InvenTree.exceptions import InvenTree.exceptions
import InvenTree.ready import InvenTree.ready
from InvenTree.config import get_base_dir
logger = structlog.getLogger('inventree') logger = structlog.getLogger('inventree')
@ -74,16 +74,21 @@ class ReportConfig(AppConfig):
pass pass
def file_from_template(self, dir_name: str, file_name: str) -> ContentFile: def file_from_template(self, dir_name: str, file_name: str) -> ContentFile:
"""Construct a new ContentFile from a template file.""" """Construct a new ContentFile from a template file.
logger.info('Creating %s template file: %s', dir_name, file_name)
return ContentFile( Args:
Path(__file__) dir_name (str): The name of the directory containing the template
.parent.joinpath('templates', dir_name, file_name) file_name (str): The name of the template file
.open('r') Returns:
.read(), ContentFile: The ContentFile object containing the template
os.path.basename(file_name), """
logger.info('Creating %s template file: %s', dir_name, file_name)
file = get_base_dir().joinpath('report', 'templates', dir_name, file_name)
if not file.exists():
raise FileNotFoundError(
'Template file %s does not exist in %s', file_name, file
) )
return ContentFile(file.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."""
@ -239,6 +244,7 @@ class ReportConfig(AppConfig):
existing_template.template = self.file_from_template( existing_template.template = self.file_from_template(
'report', filename 'report', filename
) )
existing_template.save() existing_template.save()
continue continue