1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #851: Resolve encoding issues during import/export with Python 3

This commit is contained in:
Jeremy Stretch
2017-02-01 11:49:54 -05:00
parent 5eb3c1a67b
commit 0eba5a0de3
2 changed files with 25 additions and 13 deletions

View File

@@ -236,14 +236,15 @@ class CSVDataField(forms.CharField):
if not self.help_text:
self.help_text = 'Enter one line per record in CSV format.'
def utf_8_encoder(self, unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
def to_python(self, value):
# Return a list of dictionaries, each representing an individual record
"""
Return a list of dictionaries, each representing an individual record
"""
# Python 2's csv module has problems with Unicode
if not isinstance(value, str):
value = value.encode('utf-8')
records = []
reader = csv.reader(self.utf_8_encoder(value.splitlines()))
reader = csv.reader(value.splitlines())
for i, row in enumerate(reader, start=1):
if row:
if len(row) < len(self.columns):