From a483c85165b50d9feca2443cf9cdcbd26ab60afe Mon Sep 17 00:00:00 2001 From: Omri Abu <6192223+Omripresent@users.noreply.github.com> Date: Fri, 16 May 2025 14:59:09 -0400 Subject: [PATCH] Implements #19492: Add Save Button to Script Output Window Add download button to custom script output header if job is completed and there's output content Add view condition of export query string and respond with a file download including the job output content --- netbox/extras/views.py | 10 ++++++++++ netbox/templates/extras/htmx/script_result.html | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index ae9337779..ff365e6de 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1479,6 +1479,16 @@ class ScriptResultView(TableMixin, generic.ObjectView): if job.completed: table = self.get_table(job, request, bulk_actions=False) + if job.completed and job.data and job.data.get('output'): + # If a direct export has been requested, return the job data content as a + # downloadable file. + if request.GET.get('export'): + content = job.data.get('output') + 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 diff --git a/netbox/templates/extras/htmx/script_result.html b/netbox/templates/extras/htmx/script_result.html index 3a5964823..8811ca6ba 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 and job.data and job.data.output %} +
+ + {% trans "Download" %} + +
+ {% endif %} +

{% if job.data.output %}
{{ job.data.output }}
{% else %}