From a144f110bcd13c3b1e102cd4dac8553774cf0ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20L=2E=20Redrejo=20Rodr=C3=ADguez?= Date: Thu, 26 Dec 2024 20:48:17 +0100 Subject: [PATCH] flushing to ensure all data from the TextIOWrapper is written to the underlying BytesIO buffer --- kolibri/core/auth/management/commands/bulkexportusers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kolibri/core/auth/management/commands/bulkexportusers.py b/kolibri/core/auth/management/commands/bulkexportusers.py index 08b7a5e41e4..97ad1004031 100644 --- a/kolibri/core/auth/management/commands/bulkexportusers.py +++ b/kolibri/core/auth/management/commands/bulkexportusers.py @@ -166,7 +166,8 @@ def csv_file_generator(facility, filepath, overwrite=True): csv_file = io.BytesIO() with csv_file as f: - writer = csv.DictWriter(io.TextIOWrapper(f, encoding="utf-8"), header_labels) + buffer = io.TextIOWrapper(f, encoding="utf-8") + writer = csv.DictWriter(buffer, header_labels) logger.info("Creating users csv file {filename}".format(filename=filepath)) writer.writeheader() usernames = set() @@ -207,6 +208,7 @@ def csv_file_generator(facility, filepath, overwrite=True): usernames.add(item["username"]) yield item + buffer.flush() # Ensure all data is written to the underlying BytesIO f.seek(0) file = file_storage.save(filename, f)