Просмотр исходного кода

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>
Omripresent 7 месяцев назад
Родитель
Сommit
7dab7d730d
2 измененных файлов с 20 добавлено и 2 удалено
  1. 10 1
      netbox/extras/views.py
  2. 10 1
      netbox/templates/extras/htmx/script_result.html

+ 10 - 1
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)

+ 10 - 1
netbox/templates/extras/htmx/script_result.html

@@ -53,7 +53,16 @@
     {# Script output. Legacy reports will not have this. #}
     {% if 'output' in job.data %}
       <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 %}
           <pre class="card-body font-monospace">{{ job.data.output }}</pre>
         {% else %}