From 327ad8cfc945dc71fbc90c0598fde4e05110542a Mon Sep 17 00:00:00 2001 From: Rob Duffy Date: Thu, 5 Dec 2024 03:11:12 +0100 Subject: [PATCH] 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 * Apply the path__in filter to avoid duplicating code Co-authored-by: Jeremy Stretch * Remove extra if None not in referenced_templates --------- Co-authored-by: Jeremy Stretch --- netbox/utilities/jinja2.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/jinja2.py b/netbox/utilities/jinja2.py index cefb97831..cea8c9029 100644 --- a/netbox/utilities/jinja2.py +++ b/netbox/utilities/jinja2.py @@ -28,10 +28,14 @@ class DataFileLoader(BaseLoader): 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