|
7 | 7 | from django.apps import AppConfig
|
8 | 8 | from django.core.exceptions import AppRegistryNotReady
|
9 | 9 | from django.core.files.base import ContentFile
|
| 10 | +from django.core.files.storage import default_storage |
10 | 11 | from django.db.utils import IntegrityError, OperationalError, ProgrammingError
|
11 | 12 |
|
12 | 13 | from maintenance_mode.core import maintenance_mode_on, set_maintenance_mode
|
@@ -55,6 +56,18 @@ def ready(self):
|
55 | 56 |
|
56 | 57 | set_maintenance_mode(False)
|
57 | 58 |
|
| 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 | + |
58 | 71 | def create_default_labels(self):
|
59 | 72 | """Create default label templates."""
|
60 | 73 | # Test if models are ready
|
@@ -106,29 +119,25 @@ def create_default_labels(self):
|
106 | 119 | ]
|
107 | 120 |
|
108 | 121 | 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 |
| - |
115 | 122 | filename = template.pop('file')
|
116 | 123 |
|
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() |
123 | 134 | continue
|
124 | 135 |
|
125 |
| - # Read the existing template file |
126 |
| - data = template_file.open('r').read() |
127 |
| - |
| 136 | + # Otherwise, create a new entry |
128 | 137 | try:
|
129 | 138 | # Create a new entry
|
130 | 139 | report.models.LabelTemplate.objects.create(
|
131 |
| - **template, template=ContentFile(data, os.path.basename(filename)) |
| 140 | + **template, template=self.file_from_template('label', filename) |
132 | 141 | )
|
133 | 142 | logger.info("Creating new label template: '%s'", template['name'])
|
134 | 143 | except Exception:
|
@@ -202,29 +211,24 @@ def create_default_reports(self):
|
202 | 211 | ]
|
203 | 212 |
|
204 | 213 | 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 |
| - |
211 | 214 | filename = template.pop('file')
|
212 | 215 |
|
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() |
219 | 226 | continue
|
220 | 227 |
|
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 |
225 | 229 | try:
|
226 | 230 | report.models.ReportTemplate.objects.create(
|
227 |
| - **template, template=ContentFile(data, os.path.basename(filename)) |
| 231 | + **template, template=self.file_from_template('report', filename) |
228 | 232 | )
|
229 | 233 | logger.info("Created new report template: '%s'", template['name'])
|
230 | 234 | except Exception:
|
|
0 commit comments