Files
netbox/netbox/utilities/jinja2.py
Rob Duffy 327ad8cfc9 Fixes #17490: Config Template unable to dynamically include templates (#18106)
* Fixes #17490: Config Template unable to dynamically include templates

* Cast the generator returned by find_referenced_templates() to an iterable to avoid exhausting it on the check for None

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Apply the path__in filter to avoid duplicating code

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>

* Remove extra if None not in referenced_templates

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
2024-12-04 21:11:12 -05:00

58 lines
1.9 KiB
Python

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',
)
class DataFileLoader(BaseLoader):
"""
Custom Jinja2 loader to facilitate populating template content from DataFiles.
"""
def __init__(self, data_source):
self.data_source = data_source
self._template_cache = {}
def get_source(self, environment, template):
DataFile = apps.get_model('core', 'DataFile')
# Retrieve template content from cache
try:
template_source = self._template_cache[template]
except KeyError:
raise TemplateNotFound(template)
# Find and pre-fetch referenced templates
if referenced_templates := tuple(find_referenced_templates(environment.parse(template_source))):
related_files = DataFile.objects.filter(source=self.data_source)
# None indicates the use of dynamic resolution. If dependent files are statically
# defined, we can filter by path for optimization.
if None not in referenced_templates:
related_files = related_files.filter(path__in=referenced_templates)
self.cache_templates({
df.path: df.data_as_string for df in related_files
})
return template_source, template, lambda: True
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)