Преглед изворни кода

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

Jeremy Stretch пре 1 година
родитељ
комит
1d3efc90c0
3 измењених фајлова са 18 додато и 12 уклоњено
  1. 2 1
      netbox/extras/models/models.py
  2. 16 0
      netbox/utilities/jinja2.py
  3. 0 11
      netbox/utilities/utils.py

+ 2 - 1
netbox/extras/models/models.py

@@ -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',

+ 16 - 0
netbox/utilities/jinja2.py

@@ -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)

+ 0 - 11
netbox/utilities/utils.py

@@ -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.