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 <jstretch@netboxlabs.com>
This commit is contained in:
Omripresent 2025-06-19 13:31:54 -04:00 committed by GitHub
parent c660f1c019
commit 7dab7d730d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 2 deletions

View File

@ -1476,7 +1476,16 @@ class ScriptResultView(TableMixin, generic.ObjectView):
table = None table = None
job = get_object_or_404(Job.objects.all(), pk=kwargs.get('job_pk')) 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) table = self.get_table(job, request, bulk_actions=False)
log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_INFO) log_threshold = request.GET.get('log_threshold', LogLevelChoices.LOG_INFO)

View File

@ -53,7 +53,16 @@
{# Script output. Legacy reports will not have this. #} {# Script output. Legacy reports will not have this. #}
{% if 'output' in job.data %} {% if 'output' in job.data %}
<div class="card mb-3"> <div class="card mb-3">
<h2 class="card-header">{% trans "Output" %}</h2> <h2 class="card-header d-flex justify-content-between">
{% trans "Output" %}
{% if job.completed %}
<div>
<a href="?export=output" class="btn btn-primary lh-1" role="button">
<i class="mdi mdi-download" aria-hidden="true"></i> {% trans "Download" %}
</a>
</div>
{% endif %}
</h2>
{% if job.data.output %} {% if job.data.output %}
<pre class="card-body font-monospace">{{ job.data.output }}</pre> <pre class="card-body font-monospace">{{ job.data.output }}</pre>
{% else %} {% else %}