Apply the path__in filter to avoid duplicating code

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Rob Duffy 2024-12-02 20:26:19 +01:00 committed by GitHub
parent 9930fef28b
commit 5173a490b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,14 +30,13 @@ class DataFileLoader(BaseLoader):
# Find and pre-fetch referenced templates
if referenced_templates := tuple(find_referenced_templates(environment.parse(template_source))):
if None in referenced_templates:
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)
})
else:
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