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
2 changed files with 20 additions and 2 deletions

View File

@@ -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)