mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
Refactor ConfigTemplate.render()
This commit is contained in:
parent
aa6440014a
commit
74d6db9319
@ -4,7 +4,7 @@ from django.db import models
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from jinja2.exceptions import TemplateError
|
from jinja2.loaders import BaseLoader
|
||||||
from jinja2.sandbox import SandboxedEnvironment
|
from jinja2.sandbox import SandboxedEnvironment
|
||||||
|
|
||||||
from extras.querysets import ConfigContextQuerySet
|
from extras.querysets import ConfigContextQuerySet
|
||||||
@ -231,20 +231,34 @@ class ConfigTemplate(SyncedDataMixin, ExportTemplatesMixin, TagsMixin, ChangeLog
|
|||||||
Render the contents of the template.
|
Render the contents of the template.
|
||||||
"""
|
"""
|
||||||
context = context or {}
|
context = context or {}
|
||||||
template_path = self.data_file.path
|
|
||||||
|
|
||||||
# Initialize the template loader & cache the base template code
|
# Initialize the Jinja2 environment and instantiate the Template
|
||||||
loader = ConfigTemplateLoader(data_source=self.data_source)
|
environment = self._get_environment()
|
||||||
loader.cache_templates({
|
if self.data_file:
|
||||||
template_path: self.template_code
|
template = environment.get_template(self.data_file.path)
|
||||||
})
|
else:
|
||||||
|
template = environment.from_string(self.template_code)
|
||||||
|
|
||||||
# Initialize the Jinja2 environment
|
output = template.render(**context)
|
||||||
environment = SandboxedEnvironment(loader=loader)
|
|
||||||
environment.filters.update(get_config().JINJA2_FILTERS)
|
|
||||||
|
|
||||||
# Render the template
|
|
||||||
output = environment.get_template(template_path).render(**context)
|
|
||||||
|
|
||||||
# Replace CRLF-style line terminators
|
# Replace CRLF-style line terminators
|
||||||
return output.replace('\r\n', '\n')
|
return output.replace('\r\n', '\n')
|
||||||
|
|
||||||
|
def _get_environment(self):
|
||||||
|
"""
|
||||||
|
Instantiate and return a Jinja2 environment suitable for rendering the ConfigTemplate.
|
||||||
|
"""
|
||||||
|
# Initialize the template loader & cache the base template code (if applicable)
|
||||||
|
if self.data_file:
|
||||||
|
loader = ConfigTemplateLoader(data_source=self.data_source)
|
||||||
|
loader.cache_templates({
|
||||||
|
self.data_file.path: self.template_code
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
loader = BaseLoader()
|
||||||
|
|
||||||
|
# Initialize the environment
|
||||||
|
environment = SandboxedEnvironment(loader=loader)
|
||||||
|
environment.filters.update(get_config().JINJA2_FILTERS)
|
||||||
|
|
||||||
|
return environment
|
||||||
|
Loading…
Reference in New Issue
Block a user