Skip to content

Commit

Permalink
Make the CSV file encoding compatible with Excel (#321)
Browse files Browse the repository at this point in the history
Enforce UTF-8 BOM for CSV exports
  • Loading branch information
danniel authored Sep 24, 2024
1 parent 98a48b4 commit 2955ac6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
11 changes: 7 additions & 4 deletions backend/donations/views/cron.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codecs
import csv
import logging
import operator
Expand Down Expand Up @@ -86,11 +87,12 @@ def get(self, request):
logger.info("Found {} donations".format(len(donors)))

response = HttpResponse(
content_type="text/csv",
content_type="text/csv; charset=utf-8-sig",
headers={"Content-Disposition": 'attachment; filename="export_donor.csv"'},
)
response.write(codecs.BOM_UTF8)

writer = csv.writer(response, quoting=csv.QUOTE_ALL)
writer = csv.writer(response, dialect=csv.excel)
writer.writerow(fields)

for donor in donors:
Expand Down Expand Up @@ -131,11 +133,12 @@ def get(self, request):
)

response = HttpResponse(
content_type="text/csv",
content_type="text/csv; charset=utf-8-sig",
headers={"Content-Disposition": 'attachment; filename="export_ngo.csv"'},
)
response.write(codecs.BOM_UTF8)

writer = csv.writer(response, quoting=csv.QUOTE_ALL)
writer = csv.writer(response, dialect=csv.excel)
writer.writerow(fields)

for ngo in Ngo.objects.all().values(*fields):
Expand Down
4 changes: 3 additions & 1 deletion backend/donations/views/donations_download.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codecs
import csv
import io
import logging
Expand Down Expand Up @@ -152,7 +153,8 @@ def _package_donations(tmp_dir_name: str, donations: QuerySet[Donor], ngo: Ngo,
)

csv_output = io.StringIO()
csv_writer = csv.writer(csv_output, quoting=csv.QUOTE_ALL)
csv_output.write(codecs.BOM_UTF8)
csv_writer = csv.writer(csv_output, dialect=csv.excel)
csv_writer.writerow(
[
_("no."),
Expand Down
11 changes: 7 additions & 4 deletions local_tools/export.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import codecs
import csv
import logging
import os
Expand Down Expand Up @@ -134,8 +135,9 @@


def write_query_data_to_csv(csv_name, current_kind, items):
with open(csv_name, "a") as f:
writer = csv.writer(f)
with open(csv_name, "a", encoding="utf-8-sig") as f:
f.write(codecs.BOM_UTF8)
writer = csv.writer(f, dialect=csv.excel)

for item in items:
new_row: List[Any] = []
Expand Down Expand Up @@ -238,8 +240,9 @@ def retrieve_data(csv_name, kind_data):


def create_kind_csv_file(csv_name, current_kind):
with open(csv_name, "w") as f:
writer = csv.writer(f)
with open(csv_name, "w", encoding="utf-8-sig") as f:
f.write(codecs.BOM_UTF8)
writer = csv.writer(f, dialect=csv.excel)
writer.writerow(current_kind["webapp_to_django"].values())

logger.info(f"Created {csv_name}")
Expand Down

0 comments on commit 2955ac6

Please sign in to comment.