Coverage for src/backend/InvenTree/plugin/template.py: 87%

11 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Load templates for loaded plugins.""" 

2 

3from django.template.loaders.filesystem import Loader as FilesystemLoader 

4 

5from plugin import registry 

6 

7 

8class PluginTemplateLoader(FilesystemLoader): 

9 """A custom template loader which allows loading of templates from installed plugins. 

10 

11 Each plugin can register templates simply by providing a 'templates' directory in its root path. 

12 

13 The convention is that each 'templates' directory contains a subdirectory with the same name as the plugin, 

14 e.g. templates/myplugin/my_template.html 

15 

16 In this case, the template can then be loaded (from any plugin!) by loading "myplugin/my_template.html". 

17 

18 The separate plugin-named directories help keep the templates separated and uniquely identifiable. 

19 """ 

20 

21 def get_dirs(self): 

22 """Returns all template dir paths in plugins.""" 

23 dirname = 'templates' 

24 template_dirs = [] 

25 

26 for plugin in registry.plugins.values(): 

27 new_path = plugin.path().joinpath(dirname) 

28 if new_path.is_dir(): 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true

29 template_dirs.append(new_path) 

30 

31 return tuple(template_dirs)