From 5f47ba4fb2ac9fc637ca2c786ed01751918259d3 Mon Sep 17 00:00:00 2001 From: Omri Abu <6192223+Omripresent@users.noreply.github.com> Date: Wed, 18 Jun 2025 10:07:44 -0400 Subject: [PATCH 1/3] Add condition to ScriptResultView.get function to generate a download file of job output if job is completed --- netbox/extras/views.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index ca4596fe883..6939c19bf56 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1479,6 +1479,16 @@ def get(self, request, **kwargs): if job.completed: table = self.get_table(job, request, bulk_actions=False) + if job.completed: + # If a direct export output has been requested, return the job data content as a + # downloadable file. + if request.GET.get('export') == 'output': + content = (job.data.get("output") or "").encode() + response = HttpResponse(content, content_type='text') + filename = f"{job.object.name or 'script-output'}_{job.completed.strftime('%Y-%m-%d-%H-%M-%S')}.txt" + response['Content-Disposition'] = f'attachment; filename="{filename}"' + return response + log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_INFO) if log_threshold not in LOG_LEVEL_RANK: log_threshold = LogLevelChoices.LOG_INFO From 5d71a4e3e215fd89ece6404bfdcd1c04b888ad07 Mon Sep 17 00:00:00 2001 From: Omri Abu <6192223+Omripresent@users.noreply.github.com> Date: Wed, 18 Jun 2025 10:08:58 -0400 Subject: [PATCH 2/3] Update template script_result.html adding a download button to trigger output download in ScriptResultView.get --- netbox/templates/extras/htmx/script_result.html | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/netbox/templates/extras/htmx/script_result.html b/netbox/templates/extras/htmx/script_result.html index 3a5964823da..e6b4d18235f 100644 --- a/netbox/templates/extras/htmx/script_result.html +++ b/netbox/templates/extras/htmx/script_result.html @@ -53,7 +53,16 @@

{% trans "Log" %}

{# Script output. Legacy reports will not have this. #} {% if 'output' in job.data %}
-

{% trans "Output" %}

+

+ {% trans "Output" %} + {% if job.completed %} +
+ + {% trans "Download" %} + +
+ {% endif %} +

{% if job.data.output %}
{{ job.data.output }}
{% else %} From e5bf33fc47e2e4bf6deb0cff66e96668bcb758e4 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 19 Jun 2025 13:10:10 -0400 Subject: [PATCH 3/3] Simplify conditional logic; tweak timestamp format --- netbox/extras/views.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index 6939c19bf56..ea465a4a40f 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1476,18 +1476,17 @@ def get(self, request, **kwargs): table = None job = get_object_or_404(Job.objects.all(), pk=kwargs.get('job_pk')) - if job.completed: - table = self.get_table(job, request, bulk_actions=False) + # If a direct export output has been requested, return the job data content as a + # downloadable file. + if job.completed and request.GET.get('export') == 'output': + content = (job.data.get("output") or "").encode() + response = HttpResponse(content, content_type='text') + filename = f"{job.object.name or 'script-output'}_{job.completed.strftime('%Y-%m-%d_%H%M%S')}.txt" + response['Content-Disposition'] = f'attachment; filename="{filename}"' + return response - if job.completed: - # If a direct export output has been requested, return the job data content as a - # downloadable file. - if request.GET.get('export') == 'output': - content = (job.data.get("output") or "").encode() - response = HttpResponse(content, content_type='text') - filename = f"{job.object.name or 'script-output'}_{job.completed.strftime('%Y-%m-%d-%H-%M-%S')}.txt" - response['Content-Disposition'] = f'attachment; filename="{filename}"' - return response + elif job.completed: + table = self.get_table(job, request, bulk_actions=False) log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_INFO) if log_threshold not in LOG_LEVEL_RANK: