diff --git a/CHANGELOG.md b/CHANGELOG.md index b8e84f4d2..a1a6d0dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ v2.4.6 (FUTURE) ## Bug Fixes +* [#2393](https://github.com/digitalocean/netbox/issues/2393) - Fix Unicode support for CSV import under Python 2 * [#2483](https://github.com/digitalocean/netbox/issues/2483) - Set max item count of API-populated form fields to MAX_PAGE_SIZE * [#2484](https://github.com/digitalocean/netbox/issues/2484) - Local config context not available on the Virtual Machine Edit Form * [#2485](https://github.com/digitalocean/netbox/issues/2485) - Fix cancel button when assigning a service to a device/VM diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 47ba3744f..f54b418ca 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -4,6 +4,7 @@ import csv from io import StringIO import json import re +import sys from django import forms from django.conf import settings @@ -150,6 +151,11 @@ def add_blank_choice(choices): return ((None, '---------'),) + tuple(choices) +def utf8_encoder(data): + for line in data: + yield line.encode('utf-8') + + # # Widgets # @@ -303,7 +309,12 @@ class CSVDataField(forms.CharField): def to_python(self, value): records = [] - reader = csv.reader(StringIO(value)) + + # Python 2 hack for Unicode support in the CSV reader + if sys.version_info[0] < 3: + reader = csv.reader(utf8_encoder(StringIO(value))) + else: + reader = csv.reader(StringIO(value)) # Consume and validate the first line of CSV data as column headers headers = next(reader)