Skip to content

Commit

Permalink
Fixes #17490: Config Template unable to dynamically include templates (
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
robduffy2010 and jeremystretch authored Dec 5, 2024
1 parent 1e845e6 commit 327ad8c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions netbox/utilities/jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ def get_source(self, environment, template):
raise TemplateNotFound(template)

# Find and pre-fetch referenced templates
if referenced_templates := find_referenced_templates(environment.parse(template_source)):
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
DataFile.objects.filter(source=self.data_source, path__in=referenced_templates)
df.path: df.data_as_string for df in related_files
})

return template_source, template, lambda: True
Expand Down

0 comments on commit 327ad8c

Please sign in to comment.