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

1"""Custom template loader for InvenTree.""" 

2 

3import os 

4 

5from django.conf import settings 

6from django.template.loaders.base import Loader as BaseLoader 

7from django.template.loaders.cached import Loader as CachedLoader 

8 

9 

10class 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 # 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 ] 

25 

26 # Initially load the template using the cached loader 

27 template = CachedLoader.get_template(self, template_name, skip) 

28 

29 template_path = str(template.name) 

30 

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) 

34 

35 return template