enable templates for plugins

This commit is contained in:
Matthias 2021-10-03 13:39:11 +02:00
parent 04eee50653
commit 9695d50bf4
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
3 changed files with 29 additions and 3 deletions

View File

@ -320,7 +320,6 @@ TEMPLATES = [
os.path.join(MEDIA_ROOT, 'report'),
os.path.join(MEDIA_ROOT, 'label'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
@ -333,6 +332,13 @@ TEMPLATES = [
'InvenTree.context.status_codes',
'InvenTree.context.user_roles',
],
'loaders': [(
'django.template.loaders.cached.Loader', [
'django.template.loaders.app_directories.Loader',
'django.template.loaders.filesystem.Loader',
'plugins.loader.PluginTemplateLoader',
])
],
},
},
]

View File

@ -214,6 +214,8 @@ class IntegrationPlugin(MixinBase, plugin.InvenTreePlugin):
def __init__(self):
super().__init__()
self.add_mixin('base')
self.def_path = inspect.getfile(self.__class__)
self.path = os.path.dirname(self.def_path)
self.set_sign_values()
@ -230,8 +232,7 @@ class IntegrationPlugin(MixinBase, plugin.InvenTreePlugin):
def get_plugin_commit(self):
"""get last git commit for plugin"""
path = inspect.getfile(self.__class__)
return get_git_log(path)
return get_git_log(self.def_path)
def set_sign_values(self):
"""add the last commit of the plugins class file into plugins context"""

View File

@ -0,0 +1,19 @@
"""
load templates for loaded plugins
"""
from django.conf import settings
from django.template.loaders.filesystem import Loader as FilesystemLoader
from pathlib import Path
class PluginTemplateLoader(FilesystemLoader):
def get_dirs(self):
dirname = 'templates'
template_dirs = []
for plugin in settings.INTEGRATION_PLUGINS:
new_path = Path(plugin.path) / dirname
if Path(new_path).is_dir():
template_dirs.append(new_path)
return tuple(template_dirs)