Skip to content

Commit 82e98df

Browse files
Add custom template loader class (inventree#4706)
- Specifically to ignore caching for generated reports and labels - Fixes inventree#4611
1 parent 3975a85 commit 82e98df

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎InvenTree/InvenTree/settings.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@
328328
'InvenTree.context.user_roles',
329329
],
330330
'loaders': [(
331-
'django.template.loaders.cached.Loader', [
331+
'InvenTree.template.InvenTreeTemplateLoader', [
332332
'plugin.template.PluginTemplateLoader',
333333
'django.template.loaders.filesystem.Loader',
334334
'django.template.loaders.app_directories.Loader',

‎InvenTree/InvenTree/template.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Custom template loader for InvenTree"""
2+
3+
import os
4+
5+
from django.conf import settings
6+
from django.template.loaders.base import Loader as BaseLoader
7+
from django.template.loaders.cached import Loader as CachedLoader
8+
9+
10+
class InvenTreeTemplateLoader(CachedLoader):
11+
"""Custom template loader which bypasses cache for PDF export"""
12+
13+
def get_template(self, template_name, skip=None):
14+
"""Return a template object for the given template name.
15+
16+
Any custom report or label templates will be forced to reload (without cache).
17+
This ensures that generated PDF reports / labels are always up-to-date.
18+
"""
19+
20+
# List of template patterns to skip cache for
21+
skip_cache_dirs = [
22+
os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'report')),
23+
os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'label')),
24+
'snippets/',
25+
]
26+
27+
# Initially load the template using the cached loader
28+
template = CachedLoader.get_template(self, template_name, skip)
29+
30+
template_path = str(template.name)
31+
32+
# If the template matches any of the skip patterns, reload it without cache
33+
if any([template_path.startswith(d) for d in skip_cache_dirs]):
34+
template = BaseLoader.get_template(self, template_name, skip)
35+
36+
return template

0 commit comments

Comments
 (0)