From 62343b124deb93b2f443523946386c32a2dd5b88 Mon Sep 17 00:00:00 2001 From: Omri Abu <6192223+Omripresent@users.noreply.github.com> Date: Fri, 16 May 2025 16:18:11 -0400 Subject: [PATCH] Add support for exporting job log as CSV --- netbox/extras/views.py | 29 ++++++++++++++++--- .../templates/extras/htmx/script_result.html | 13 +++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/netbox/extras/views.py b/netbox/extras/views.py index ff365e6de..6d0ba94ea 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -1,3 +1,6 @@ +import csv +from io import StringIO + from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.contenttypes.models import ContentType @@ -1479,16 +1482,34 @@ 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 + if job.completed and job.data: + # If a direct export output has been requested, return the job data content as a # downloadable file. - if request.GET.get('export'): - content = job.data.get('output') + if request.GET.get('export') == 'output' and job.data.get('output'): + content = job.data['output'].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 + # If a direct export log has been requested, return the job log content as a + # downloadable CSV file. + if request.GET.get('export') == 'log' and table: + # Filter generator for visible columns + def column_filter(data): + for item in data: + yield dict((k, v) for k, v, in item.items() if k in table.columns.names()) + # Write a CSV to a string buffer for content + buf = StringIO() + writer = csv.DictWriter(buf, fieldnames=table.columns.names()) + writer.writeheader() + writer.writerows(column_filter(table.data)) + buf.seek(0) + response = HttpResponse(buf.read().encode(), content_type='text/csv') + filename = f"{job.object.name or 'script-log'}_{job.completed.strftime('%Y-%m-%d-%H-%M-%S')}.csv" + 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 8811ca6ba..5e35290a9 100644 --- a/netbox/templates/extras/htmx/script_result.html +++ b/netbox/templates/extras/htmx/script_result.html @@ -40,7 +40,14 @@ {% if table %}
-

{% trans "Log" %}

+

+ {% trans "Log" %} +
+ + {% trans "Download" %} + +
+

{% trans "Output" %} - {% if job.completed and job.data and job.data.output %} + {% if job.data.output %}
- + {% trans "Download" %}