Skip to content

Commit 0f9bddb

Browse files
Report bugfix (#9013) (#9014)
- Ensure default label templates exist - Ensure default report templates exist (cherry picked from commit 2a6434e) Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
1 parent b0fc42d commit 0f9bddb

File tree

1 file changed

+37
-33
lines changed
  • src/backend/InvenTree/report

1 file changed

+37
-33
lines changed

src/backend/InvenTree/report/apps.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.apps import AppConfig
88
from django.core.exceptions import AppRegistryNotReady
99
from django.core.files.base import ContentFile
10+
from django.core.files.storage import default_storage
1011
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
1112

1213
from maintenance_mode.core import maintenance_mode_on, set_maintenance_mode
@@ -55,6 +56,18 @@ def ready(self):
5556

5657
set_maintenance_mode(False)
5758

59+
def file_from_template(self, dir_name: str, file_name: str) -> ContentFile:
60+
"""Construct a new ContentFile from a template file."""
61+
logger.info('Creating %s template file: %s', dir_name, file_name)
62+
63+
return ContentFile(
64+
Path(__file__)
65+
.parent.joinpath('templates', dir_name, file_name)
66+
.open('r')
67+
.read(),
68+
os.path.basename(file_name),
69+
)
70+
5871
def create_default_labels(self):
5972
"""Create default label templates."""
6073
# Test if models are ready
@@ -106,29 +119,25 @@ def create_default_labels(self):
106119
]
107120

108121
for template in label_templates:
109-
# Ignore matching templates which are already in the database
110-
if report.models.LabelTemplate.objects.filter(
111-
name=template['name']
112-
).exists():
113-
continue
114-
115122
filename = template.pop('file')
116123

117-
template_file = Path(__file__).parent.joinpath(
118-
'templates', 'label', filename
119-
)
120-
121-
if not template_file.exists():
122-
logger.warning("Missing template file: '%s'", template['name'])
124+
# Template already exists in the database - check that the file exists too
125+
if existing_template := report.models.LabelTemplate.objects.filter(
126+
name=template['name'], model_type=template['model_type']
127+
).first():
128+
if not default_storage.exists(existing_template.template.name):
129+
# The file does not exist in the storage system - add it in
130+
existing_template.template = self.file_from_template(
131+
'label', filename
132+
)
133+
existing_template.save()
123134
continue
124135

125-
# Read the existing template file
126-
data = template_file.open('r').read()
127-
136+
# Otherwise, create a new entry
128137
try:
129138
# Create a new entry
130139
report.models.LabelTemplate.objects.create(
131-
**template, template=ContentFile(data, os.path.basename(filename))
140+
**template, template=self.file_from_template('label', filename)
132141
)
133142
logger.info("Creating new label template: '%s'", template['name'])
134143
except Exception:
@@ -202,29 +211,24 @@ def create_default_reports(self):
202211
]
203212

204213
for template in report_templates:
205-
# Ignore matching templates which are already in the database
206-
if report.models.ReportTemplate.objects.filter(
207-
name=template['name']
208-
).exists():
209-
continue
210-
211214
filename = template.pop('file')
212215

213-
template_file = Path(__file__).parent.joinpath(
214-
'templates', 'report', filename
215-
)
216-
217-
if not template_file.exists():
218-
logger.warning("Missing template file: '%s'", template['name'])
216+
# Template already exists in the database - check that the file exists too
217+
if existing_template := report.models.ReportTemplate.objects.filter(
218+
name=template['name'], model_type=template['model_type']
219+
).first():
220+
if not default_storage.exists(existing_template.template.name):
221+
# The file does not exist in the storage system - add it in
222+
existing_template.template = self.file_from_template(
223+
'report', filename
224+
)
225+
existing_template.save()
219226
continue
220227

221-
# Read the existing template file
222-
data = template_file.open('r').read()
223-
224-
# Create a new entry
228+
# Otherwise, create a new entry
225229
try:
226230
report.models.ReportTemplate.objects.create(
227-
**template, template=ContentFile(data, os.path.basename(filename))
231+
**template, template=self.file_from_template('report', filename)
228232
)
229233
logger.info("Created new report template: '%s'", template['name'])
230234
except Exception:

0 commit comments

Comments
 (0)