Move utilities.utils.render_jinja2() to utilities.jinja2

This commit is contained in:
Jeremy Stretch 2024-03-21 13:21:45 -04:00
parent c30d22335a
commit 1d3efc90c0
3 changed files with 18 additions and 12 deletions

View File

@ -24,7 +24,8 @@ from netbox.models.features import (
)
from utilities.html import clean_html
from utilities.querysets import RestrictedQuerySet
from utilities.utils import dict_to_querydict, render_jinja2
from utilities.jinja2 import render_jinja2
from utilities.utils import dict_to_querydict
__all__ = (
'Bookmark',

View File

@ -1,6 +1,9 @@
from django.apps import apps
from jinja2 import BaseLoader, TemplateNotFound
from jinja2.meta import find_referenced_templates
from jinja2.sandbox import SandboxedEnvironment
from netbox.config import get_config
__all__ = (
'DataFileLoader',
@ -35,3 +38,16 @@ class DataFileLoader(BaseLoader):
def cache_templates(self, templates):
self._template_cache.update(templates)
#
# Utility functions
#
def render_jinja2(template_code, context):
"""
Render a Jinja2 template with the provided context. Return the rendered content.
"""
environment = SandboxedEnvironment()
environment.filters.update(get_config().JINJA2_FILTERS)
return environment.from_string(source=template_code).render(**context)

View File

@ -8,9 +8,7 @@ from django.http import QueryDict
from django.utils import timezone
from django.utils.datastructures import MultiValueDict
from django.utils.timezone import localtime
from jinja2.sandbox import SandboxedEnvironment
from netbox.config import get_config
from .string import title
@ -133,15 +131,6 @@ def drange(start, end, step=decimal.Decimal(1)):
start += step
def render_jinja2(template_code, context):
"""
Render a Jinja2 template with the provided context. Return the rendered content.
"""
environment = SandboxedEnvironment()
environment.filters.update(get_config().JINJA2_FILTERS)
return environment.from_string(source=template_code).render(**context)
def prepare_cloned_fields(instance):
"""
Generate a QueryDict comprising attributes from an object's clone() method.