mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 20:12:00 -06:00
Add support for exporting job log as CSV
This commit is contained in:
parent
a483c85165
commit
62343b124d
@ -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
|
||||
|
@ -40,7 +40,14 @@
|
||||
{% if table %}
|
||||
<div class="card">
|
||||
<div class="table-responsive" id="object_list">
|
||||
<h2 class="card-header">{% trans "Log" %}</h2>
|
||||
<h2 class="card-header d-flex justify-content-between">
|
||||
{% trans "Log" %}
|
||||
<div>
|
||||
<a href="?export=log" class="btn btn-primary lh-1" role="button">
|
||||
<i class="mdi mdi-download" aria-hidden="true"></i> {% trans "Download" %}
|
||||
</a>
|
||||
</div>
|
||||
</h2>
|
||||
<div class="htmx-container table-responsive"
|
||||
hx-get="{% url 'extras:script_result' job_pk=job.pk %}?embedded=True&log=True&log_threshold={{log_threshold}}"
|
||||
hx-target="this"
|
||||
@ -55,9 +62,9 @@
|
||||
<div class="card mb-3">
|
||||
<h2 class="card-header d-flex justify-content-between">
|
||||
{% trans "Output" %}
|
||||
{% if job.completed and job.data and job.data.output %}
|
||||
{% if job.data.output %}
|
||||
<div>
|
||||
<a href="?export=True" class="btn btn-primary lh-1" role="button">
|
||||
<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>
|
||||
|
Loading…
Reference in New Issue
Block a user