Coverage for src/backend/InvenTree/InvenTree/template.py: 86%
12 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Custom template loader for InvenTree."""
3import os
5from django.conf import settings
6from django.template.loaders.base import Loader as BaseLoader
7from django.template.loaders.cached import Loader as CachedLoader
10class InvenTreeTemplateLoader(CachedLoader):
11 """Custom template loader which bypasses cache for PDF export."""
13 def get_template(self, template_name, skip=None):
14 """Return a template object for the given template name.
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 # List of template patterns to skip cache for
20 skip_cache_dirs = [
21 os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'report')),
22 os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'label')),
23 'snippets/',
24 ]
26 # Initially load the template using the cached loader
27 template = CachedLoader.get_template(self, template_name, skip)
29 template_path = str(template.name)
31 # If the template matches any of the skip patterns, reload it without cache
32 if any(template_path.startswith(d) for d in skip_cache_dirs): 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true
33 template = BaseLoader.get_template(self, template_name, skip)
35 return template