mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 03:42:25 -06:00
Move queryset_to_csv() utility into ObjectListView to allow overriding by individual views
This commit is contained in:
@@ -2,7 +2,6 @@ import datetime
|
||||
import json
|
||||
|
||||
from django.core.serializers import serialize
|
||||
from django.http import HttpResponse
|
||||
|
||||
from dcim.constants import LENGTH_UNIT_CENTIMETER, LENGTH_UNIT_FOOT, LENGTH_UNIT_INCH, LENGTH_UNIT_METER
|
||||
|
||||
@@ -36,32 +35,6 @@ def csv_format(data):
|
||||
return ','.join(csv)
|
||||
|
||||
|
||||
def queryset_to_csv(queryset):
|
||||
"""
|
||||
Export a queryset of objects as CSV, using the model's to_csv() method.
|
||||
"""
|
||||
output = []
|
||||
|
||||
# Start with the column headers
|
||||
headers = ','.join(queryset.model.csv_headers)
|
||||
output.append(headers)
|
||||
|
||||
# Iterate through the queryset
|
||||
for obj in queryset:
|
||||
data = csv_format(obj.to_csv())
|
||||
output.append(data)
|
||||
|
||||
# Build the HTTP response
|
||||
response = HttpResponse(
|
||||
'\n'.join(output),
|
||||
content_type='text/csv'
|
||||
)
|
||||
filename = 'netbox_{}.csv'.format(queryset.model._meta.verbose_name_plural)
|
||||
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def foreground_color(bg_color):
|
||||
"""
|
||||
Return the ideal foreground color (black or white) for a given background color in hexadecimal RGB format.
|
||||
|
||||
Reference in New Issue
Block a user