Skip to content

Commit

Permalink
feat: add byte order mark to talpa export for better excel support
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke committed Nov 14, 2023
1 parent beab384 commit 269bcfb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/benefit/applications/services/csv_export_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def get_csv_string_lines_generator(
io = StringIO()
csv_writer = csv.writer(io, delimiter=self.CSV_DELIMITER, quoting=quoting)
line_length_set = set()

# Add BOM as the first item in the generator
yield '\ufeff'

for line in self.get_csv_cell_list_lines_generator():
line_length_set.add(len(line))
assert len(line_length_set) == 1, "Each CSV line must have same colum count"
Expand Down
7 changes: 6 additions & 1 deletion backend/benefit/applications/tests/test_talpa_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def test_talpa_csv_date(pruned_applications_csv_service_with_one_application):
)
assert csv_lines[1][12] == '"2021-08-27"'

def _has_bom(filename):
with open(filename, 'rb') as f:
return f.read(3) == b'\xef\xbb\xbf'

def test_write_talpa_csv_file(
pruned_applications_csv_service_with_one_application, tmp_path
Expand All @@ -112,7 +115,9 @@ def test_write_talpa_csv_file(
application.save()
output_file = tmp_path / "output.csv"
pruned_applications_csv_service_with_one_application.write_csv_file(output_file)
with open(output_file, encoding="utf-8") as f:
with open(output_file, encoding="utf-8-sig") as f:
contents = f.read()
assert contents.startswith('"Hakemusnumero";"Työnantajan tyyppi"')
assert "äöÄÖtest" in contents

assert _has_bom(output_file)

0 comments on commit 269bcfb

Please sign in to comment.