Add custom template loader class (#4706)

- Specifically to ignore caching for generated reports and labels
- Fixes https://github.com/inventree/InvenTree/issues/4611
This commit is contained in:
Oliver 2023-04-27 13:28:00 +10:00 committed by GitHub
parent 3975a85742
commit 82e98dffb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -328,7 +328,7 @@ TEMPLATES = [
'InvenTree.context.user_roles',
],
'loaders': [(
'django.template.loaders.cached.Loader', [
'InvenTree.template.InvenTreeTemplateLoader', [
'plugin.template.PluginTemplateLoader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',

View File

@ -0,0 +1,36 @@
"""Custom template loader for InvenTree"""
import os
from django.conf import settings
from django.template.loaders.base import Loader as BaseLoader
from django.template.loaders.cached import Loader as CachedLoader
class InvenTreeTemplateLoader(CachedLoader):
"""Custom template loader which bypasses cache for PDF export"""
def get_template(self, template_name, skip=None):
"""Return a template object for the given template name.
Any custom report or label templates will be forced to reload (without cache).
This ensures that generated PDF reports / labels are always up-to-date.
"""
# List of template patterns to skip cache for
skip_cache_dirs = [
os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'report')),
os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'label')),
'snippets/',
]
# Initially load the template using the cached loader
template = CachedLoader.get_template(self, template_name, skip)
template_path = str(template.name)
# If the template matches any of the skip patterns, reload it without cache
if any([template_path.startswith(d) for d in skip_cache_dirs]):
template = BaseLoader.get_template(self, template_name, skip)
return template