mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-25 18:08:38 -06:00
Fixes #1859: Implemented support for line breaks within CSV fields
This commit is contained in:
parent
59dcbce417
commit
60c03a646c
@ -1,61 +0,0 @@
|
|||||||
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 in [None, 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:
|
|
||||||
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
|
|
@ -1,7 +1,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
import itertools
|
from io import StringIO
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
@ -245,14 +245,10 @@ class CSVDataField(forms.CharField):
|
|||||||
|
|
||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
|
|
||||||
# Python 2's csv module has problems with Unicode
|
|
||||||
if not isinstance(value, str):
|
|
||||||
value = value.encode('utf-8')
|
|
||||||
|
|
||||||
records = []
|
records = []
|
||||||
reader = csv.reader(value.splitlines())
|
reader = csv.reader(StringIO(value))
|
||||||
|
|
||||||
# Consume and valdiate the first line of CSV data as column headers
|
# Consume and validate the first line of CSV data as column headers
|
||||||
headers = next(reader)
|
headers = next(reader)
|
||||||
for f in self.required_fields:
|
for f in self.required_fields:
|
||||||
if f not in headers:
|
if f not in headers:
|
||||||
|
@ -1,5 +1,65 @@
|
|||||||
from __future__ import unicode_literals
|
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 in [None, 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:
|
||||||
|
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):
|
def foreground_color(bg_color):
|
||||||
"""
|
"""
|
||||||
|
@ -20,7 +20,7 @@ from django.views.generic import View
|
|||||||
from django_tables2 import RequestConfig
|
from django_tables2 import RequestConfig
|
||||||
|
|
||||||
from extras.models import CustomField, CustomFieldValue, ExportTemplate, UserAction
|
from extras.models import CustomField, CustomFieldValue, ExportTemplate, UserAction
|
||||||
from utilities.csv import queryset_to_csv
|
from utilities.utils import queryset_to_csv
|
||||||
from utilities.forms import BootstrapMixin, CSVDataField
|
from utilities.forms import BootstrapMixin, CSVDataField
|
||||||
from .error_handlers import handle_protectederror
|
from .error_handlers import handle_protectederror
|
||||||
from .forms import ConfirmationForm
|
from .forms import ConfirmationForm
|
||||||
|
Loading…
Reference in New Issue
Block a user