Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Handle CSV files with UTF-8 BOM #498

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed
- Change facility list CSV download to request one page at a time [#496](https://github.com/open-apparel-registry/open-apparel-registry/pull/496)
- Handle CSV files that include a byte order mark [#498](https://github.com/open-apparel-registry/open-apparel-registry/pull/498)

### Deprecated

Expand Down
13 changes: 13 additions & 0 deletions src/django/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def setUp(self):
'facilities.csv',
b'\n'.join([s.encode() for s in self.test_csv_rows]),
content_type='text/csv')
self.test_file_with_bom = SimpleUploadedFile(
'facilities_with_bom.csv',
b'\n'.join([self.test_csv_rows[0].encode('utf-8-sig')] +
[s.encode() for s in self.test_csv_rows[1:]]),
content_type='text/csv')

def post_header_only_file(self, **kwargs):
if kwargs is None:
Expand All @@ -60,6 +65,14 @@ def post_header_only_file(self, **kwargs):
{'file': csv_file, **kwargs},
format='multipart')

def test_can_post_file_with_bom(self):
response = self.client.post(reverse('facility-list-list'),
{'file': self.test_file_with_bom},
format='multipart')
self.assertEqual(response.status_code, status.HTTP_200_OK)
new_list = FacilityList.objects.last()
self.assertEqual(self.test_csv_rows[0], new_list.header)

def test_creates_list_and_items(self):
previous_list_count = FacilityList.objects.all().count()
previous_item_count = FacilityListItem.objects.all().count()
Expand Down
4 changes: 2 additions & 2 deletions src/django/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def create(self, request):
'Uploaded file exceeds the maximum size of {:.1f}MB.'.format(
mb))
try:
header = csv_file.readline().decode().rstrip()
header = csv_file.readline().decode(encoding='utf-8-sig').rstrip()
except UnicodeDecodeError:
ROLLBAR = getattr(settings, 'ROLLBAR', {})
if ROLLBAR:
Expand Down Expand Up @@ -671,7 +671,7 @@ def create(self, request):
items.append(FacilityListItem(
row_index=(idx - 1),
facility_list=new_list,
raw_data=line.decode().rstrip()
raw_data=line.decode(encoding='utf-8-sig').rstrip()
))
except UnicodeDecodeError:
ROLLBAR = getattr(settings, 'ROLLBAR', {})
Expand Down