2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

move out templete renderer into helper

This commit is contained in:
Matthias 2022-05-11 13:32:29 +02:00
parent 970503f424
commit 1098327ba9
No known key found for this signature in database
GPG Key ID: AB6D0E6C4CB65093
3 changed files with 30 additions and 32 deletions

View File

@ -11,9 +11,8 @@ from django.db.utils import OperationalError, ProgrammingError
import InvenTree.helpers import InvenTree.helpers
from plugin.helpers import MixinImplementationError, MixinNotImplementedError from plugin.helpers import MixinImplementationError, MixinNotImplementedError, render_template
from plugin.models import PluginConfig, PluginSetting from plugin.models import PluginConfig, PluginSetting
from plugin.template import render_template
from plugin.urls import PLUGIN_BASE from plugin.urls import PLUGIN_BASE

View File

@ -8,12 +8,17 @@ import sysconfig
import traceback import traceback
import inspect import inspect
import pkgutil import pkgutil
import logging
from django import template
from django.conf import settings from django.conf import settings
from django.core.exceptions import AppRegistryNotReady from django.core.exceptions import AppRegistryNotReady
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
logger = logging.getLogger('inventree')
# region logging / errors # region logging / errors
class IntegrationPluginError(Exception): class IntegrationPluginError(Exception):
""" """
@ -217,3 +222,26 @@ def get_plugins(pkg, baseclass):
return plugins return plugins
# endregion # endregion
# region templates
def render_template(plugin, template_file, context=None):
"""
Locate and render a template file, available in the global template context.
"""
try:
tmp = template.loader.get_template(template_file)
except template.TemplateDoesNotExist:
logger.error(f"Plugin {plugin.slug} could not locate template '{template_file}'")
return f"""
<div class='alert alert-block alert-danger'>
Template file <em>{template_file}</em> does not exist.
</div>
"""
# Render with the provided context
html = tmp.render(context)
return html
# endregion

View File

@ -1,19 +1,12 @@
""" """Load templates for loaded plugins"""
load templates for loaded plugins
"""
import logging
from pathlib import Path from pathlib import Path
from django import template
from django.template.loaders.filesystem import Loader as FilesystemLoader from django.template.loaders.filesystem import Loader as FilesystemLoader
from plugin import registry from plugin import registry
logger = logging.getLogger('inventree')
class PluginTemplateLoader(FilesystemLoader): class PluginTemplateLoader(FilesystemLoader):
""" """
A custom template loader which allows loading of templates from installed plugins. A custom template loader which allows loading of templates from installed plugins.
@ -38,25 +31,3 @@ class PluginTemplateLoader(FilesystemLoader):
template_dirs.append(new_path) template_dirs.append(new_path)
return tuple(template_dirs) return tuple(template_dirs)
def render_template(plugin, template_file, context=None):
"""
Locate and render a template file, available in the global template context.
"""
try:
tmp = template.loader.get_template(template_file)
except template.TemplateDoesNotExist:
logger.error(f"Plugin {plugin.slug} could not locate template '{template_file}'")
return f"""
<div class='alert alert-block alert-danger'>
Template file <em>{template_file}</em> does not exist.
</div>
"""
# Render with the provided context
html = tmp.render(context)
return html