From 7dab7d730da7bf302df3cd183935bdc2023b2ecd Mon Sep 17 00:00:00 2001 From: Omripresent <6192223+Omripresent@users.noreply.github.com> Date: Thu, 19 Jun 2025 13:31:54 -0400 Subject: [PATCH] Fixes: #19492: Add Save Button to Script Output Window (#19721) * Add condition to ScriptResultView.get function to generate a download file of job output if job is completed * Update template script_result.html adding a download button to trigger output download in ScriptResultView.get * Simplify conditional logic; tweak timestamp format --------- Co-authored-by: Jeremy Stretch --- netbox/extras/views.py | 11 ++++++++++- netbox/templates/extras/htmx/script_result.html | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index ca4596fe8..ea465a4a4 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1476,7 +1476,16 @@ class ScriptResultView(TableMixin, generic.ObjectView): table = None job = get_object_or_404(Job.objects.all(), pk=kwargs.get('job_pk')) - if job.completed: + # 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 + + elif job.completed: table = self.get_table(job, request, bulk_actions=False) log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_INFO) diff --git a/netbox/templates/extras/htmx/script_result.html b/netbox/templates/extras/htmx/script_result.html index 3a5964823..e6b4d1823 100644 --- a/netbox/templates/extras/htmx/script_result.html +++ b/netbox/templates/extras/htmx/script_result.html @@ -53,7 +53,16 @@ {# 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 %}