From 1098327ba9926b7af85c8d437807b7d5e03ee448 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 11 May 2022 13:32:29 +0200 Subject: [PATCH] move out templete renderer into helper --- InvenTree/plugin/base/integration/mixins.py | 3 +- InvenTree/plugin/helpers.py | 28 +++++++++++++++++++ InvenTree/plugin/template.py | 31 +-------------------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/InvenTree/plugin/base/integration/mixins.py b/InvenTree/plugin/base/integration/mixins.py index 5bae993e61..32bdd00681 100644 --- a/InvenTree/plugin/base/integration/mixins.py +++ b/InvenTree/plugin/base/integration/mixins.py @@ -11,9 +11,8 @@ from django.db.utils import OperationalError, ProgrammingError 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.template import render_template from plugin.urls import PLUGIN_BASE diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index f1753b1b45..011a8f84f0 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -8,12 +8,17 @@ import sysconfig import traceback import inspect import pkgutil +import logging +from django import template from django.conf import settings from django.core.exceptions import AppRegistryNotReady from django.db.utils import IntegrityError +logger = logging.getLogger('inventree') + + # region logging / errors class IntegrationPluginError(Exception): """ @@ -217,3 +222,26 @@ def get_plugins(pkg, baseclass): return plugins # 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""" +
+ Template file {template_file} does not exist. +
+ """ + + # Render with the provided context + html = tmp.render(context) + + return html +# endregion diff --git a/InvenTree/plugin/template.py b/InvenTree/plugin/template.py index 53ee7bb6db..f33b5f13fb 100644 --- a/InvenTree/plugin/template.py +++ b/InvenTree/plugin/template.py @@ -1,19 +1,12 @@ -""" -load templates for loaded plugins -""" +"""Load templates for loaded plugins""" -import logging from pathlib import Path -from django import template from django.template.loaders.filesystem import Loader as FilesystemLoader from plugin import registry -logger = logging.getLogger('inventree') - - class PluginTemplateLoader(FilesystemLoader): """ A custom template loader which allows loading of templates from installed plugins. @@ -38,25 +31,3 @@ class PluginTemplateLoader(FilesystemLoader): template_dirs.append(new_path) 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""" -
- Template file {template_file} does not exist. -
- """ - - # Render with the provided context - html = tmp.render(context) - - return html