Skip to content

Commit 063d1fe

Browse files
Closes #18797: Support path import for certain Jinja environment parameters (#19962)
* Closes #18797: Support path import for certain Jinja environment parameters * Document dotted path support for Jinja env params
1 parent 6ba6ff3 commit 063d1fe

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

docs/models/extras/configtemplate.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Jinja2 template code, if being defined locally rather than replicated from a dat
2424

2525
A dictionary of any additional parameters to pass when instantiating the [Jinja2 environment](https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment). Jinja2 supports various optional parameters which can be used to modify its default behavior.
2626

27+
The `undefined` and `finalize` Jinja environment parameters, which must reference a Python class or function, can define a dotted path to the desired resource. For example:
28+
29+
```json
30+
{
31+
"undefined": "jinja2.StrictUndefined"
32+
}
33+
```
34+
2735
### MIME Type
2836

2937
!!! info "This field was introduced in NetBox v4.3."

docs/models/extras/exporttemplate.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ Jinja2 template code for rendering the exported data.
2626

2727
A dictionary of any additional parameters to pass when instantiating the [Jinja2 environment](https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment). Jinja2 supports various optional parameters which can be used to modify its default behavior.
2828

29+
The `undefined` and `finalize` Jinja environment parameters, which must reference a Python class or function, can define a dotted path to the desired resource. For example:
30+
31+
```json
32+
{
33+
"undefined": "jinja2.StrictUndefined"
34+
}
35+
```
36+
2937
### MIME Type
3038

3139
The MIME type to indicate in the response when rendering the export template (optional). Defaults to `text/plain`.

netbox/extras/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
JOB_ERRORED: 'job_ended',
2222
}
2323

24+
# Jinja environment parameters which support path imports
25+
JINJA_ENV_PARAMS_WITH_PATH_IMPORT = (
26+
'undefined',
27+
'finalize',
28+
)
29+
2430
# Dashboard
2531
DEFAULT_DASHBOARD = [
2632
{

netbox/extras/models/mixins.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
import importlib.util
33
import os
44
import sys
5+
56
from django.core.files.storage import storages
67
from django.db import models
7-
from django.utils.translation import gettext_lazy as _
88
from django.http import HttpResponse
9+
from django.utils.module_loading import import_string
10+
from django.utils.translation import gettext_lazy as _
911

10-
from extras.constants import DEFAULT_MIME_TYPE
12+
from extras.constants import DEFAULT_MIME_TYPE, JINJA_ENV_PARAMS_WITH_PATH_IMPORT
1113
from extras.utils import filename_from_model, filename_from_object
1214
from utilities.jinja2 import render_jinja2
1315

14-
1516
__all__ = (
1617
'PythonModuleMixin',
1718
'RenderTemplateMixin',
@@ -125,12 +126,22 @@ def get_context(self, context=None, queryset=None):
125126
class_name=self.__class__
126127
))
127128

129+
def get_environment_params(self):
130+
"""
131+
Pre-processing of any defined Jinja environment parameters (e.g. to support path resolution).
132+
"""
133+
params = self.environment_params or {}
134+
for name, value in params.items():
135+
if name in JINJA_ENV_PARAMS_WITH_PATH_IMPORT and type(value) is str:
136+
params[name] = import_string(value)
137+
return params
138+
128139
def render(self, context=None, queryset=None):
129140
"""
130141
Render the template with the provided context. The context is passed to the Jinja2 environment as a dictionary.
131142
"""
132143
context = self.get_context(context=context, queryset=queryset)
133-
env_params = self.environment_params or {}
144+
env_params = self.get_environment_params()
134145
output = render_jinja2(self.template_code, context, env_params, getattr(self, 'data_file', None))
135146

136147
# Replace CRLF-style line terminators

0 commit comments

Comments
 (0)