Parcourir la source

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

Jeremy Stretch il y a 8 ans
Parent
commit
0eba5a0de3
2 fichiers modifiés avec 25 ajouts et 13 suppressions
  1. 7 6
      netbox/utilities/forms.py
  2. 18 7
      netbox/utilities/utils.py

+ 7 - 6
netbox/utilities/forms.py

@@ -236,14 +236,15 @@ class CSVDataField(forms.CharField):
         if not self.help_text:
         if not self.help_text:
             self.help_text = 'Enter one line per record in CSV format.'
             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):
     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 = []
         records = []
-        reader = csv.reader(self.utf_8_encoder(value.splitlines()))
+        reader = csv.reader(value.splitlines())
         for i, row in enumerate(reader, start=1):
         for i, row in enumerate(reader, start=1):
             if row:
             if row:
                 if len(row) < len(self.columns):
                 if len(row) < len(self.columns):

+ 18 - 7
netbox/utilities/utils.py

@@ -1,15 +1,26 @@
+import six
+
+
 def csv_format(data):
 def csv_format(data):
     """
     """
     Encapsulate any data which contains a comma within double quotes.
     Encapsulate any data which contains a comma within double quotes.
     """
     """
     csv = []
     csv = []
-    for d in data:
-        if d in [None, False]:
+    for value in data:
+
+        # Represent None or False with empty string
+        if value in [None, False]:
             csv.append(u'')
             csv.append(u'')
-        elif type(d) not in (str, unicode):
-            csv.append(u'{}'.format(d))
-        elif u',' in d:
-            csv.append(u'"{}"'.format(d))
+            continue
+
+        # Force conversion to string first so we can check for any commas
+        if not isinstance(value, six.string_types):
+            value = u'{}'.format(value)
+
+        # Double-quote the value if it contains a comma
+        if u',' in value:
+            csv.append(u'"{}"'.format(value))
         else:
         else:
-            csv.append(d)
+            csv.append(u'{}'.format(value))
+
     return u','.join(csv)
     return u','.join(csv)