mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
836478c166
* merge branch develop * bugfix, signals for virtualization's class wasn't correctly defined * updated webhooks for 2.4 and cleanup * updated docs to cover changes to supervisor config * review changes and further cleanup * updated redis connection settings * cleanup settings
85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
from __future__ import unicode_literals
|
|
|
|
import datetime
|
|
import six
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
def csv_format(data):
|
|
"""
|
|
Encapsulate any data which contains a comma within double quotes.
|
|
"""
|
|
csv = []
|
|
for value in data:
|
|
|
|
# Represent None or False with empty string
|
|
if value is None or value is False:
|
|
csv.append('')
|
|
continue
|
|
|
|
# Convert dates to ISO format
|
|
if isinstance(value, (datetime.date, datetime.datetime)):
|
|
value = value.isoformat()
|
|
|
|
# Force conversion to string first so we can check for any commas
|
|
if not isinstance(value, six.string_types):
|
|
value = '{}'.format(value)
|
|
|
|
# Double-quote the value if it contains a comma
|
|
if ',' in value or '\n' in value:
|
|
csv.append('"{}"'.format(value))
|
|
else:
|
|
csv.append('{}'.format(value))
|
|
|
|
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.
|
|
"""
|
|
bg_color = bg_color.strip('#')
|
|
r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
|
|
if r * 0.299 + g * 0.587 + b * 0.114 > 186:
|
|
return '000000'
|
|
else:
|
|
return 'ffffff'
|
|
|
|
|
|
def dynamic_import(name):
|
|
"""
|
|
Dynamically import a class from an absolute path string
|
|
"""
|
|
components = name.split('.')
|
|
mod = __import__(components[0])
|
|
for comp in components[1:]:
|
|
mod = getattr(mod, comp)
|
|
return mod
|