Move queryset_to_csv() utility into ObjectListView to allow overriding by individual views

This commit is contained in:
Jeremy Stretch
2018-12-05 16:35:59 -05:00
parent ab4cb46d94
commit 2a07e8f3f0
2 changed files with 29 additions and 31 deletions

View File

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