mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 03:42:25 -06:00
Merge v2.5 work
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import six
|
||||
|
||||
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
|
||||
|
||||
|
||||
def csv_format(data):
|
||||
@@ -26,7 +25,7 @@ def csv_format(data):
|
||||
value = value.isoformat()
|
||||
|
||||
# Force conversion to string first so we can check for any commas
|
||||
if not isinstance(value, six.string_types):
|
||||
if not isinstance(value, str):
|
||||
value = '{}'.format(value)
|
||||
|
||||
# Double-quote the value if it contains a comma
|
||||
@@ -38,32 +37,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.
|
||||
@@ -123,3 +96,21 @@ def deepmerge(original, new):
|
||||
else:
|
||||
merged[key] = val
|
||||
return merged
|
||||
|
||||
|
||||
def to_meters(length, unit):
|
||||
"""
|
||||
Convert the given length to meters.
|
||||
"""
|
||||
length = int(length)
|
||||
if length < 0:
|
||||
raise ValueError("Length must be a positive integer")
|
||||
if unit == LENGTH_UNIT_METER:
|
||||
return length
|
||||
if unit == LENGTH_UNIT_CENTIMETER:
|
||||
return length / 100
|
||||
if unit == LENGTH_UNIT_FOOT:
|
||||
return length * 0.3048
|
||||
if unit == LENGTH_UNIT_INCH:
|
||||
return length * 0.3048 * 12
|
||||
raise ValueError("Unknown unit {}. Must be 'm', 'cm', 'ft', or 'in'.".format(unit))
|
||||
|
||||
Reference in New Issue
Block a user