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:
parent
970503f424
commit
1098327ba9
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user