Skip to content

Commit ebc6d37

Browse files
committed
views.download_csv_file to use DefaultStorage
1 parent c7c169c commit ebc6d37

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

kolibri/plugins/facility/views.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import io
21
import json
32
import os
43
from datetime import datetime as dt
54

65
from django.core.exceptions import PermissionDenied
6+
from django.core.files.storage import DefaultStorage
77
from django.http import Http404
88
from django.http import HttpResponse
99
from django.http.response import FileResponse
@@ -187,34 +187,31 @@ def download_csv_file(request, csv_type, facility_id):
187187
).replace("-", "_"),
188188
}
189189

190+
file_storage = DefaultStorage()
191+
190192
if csv_type in CSV_EXPORT_FILENAMES.keys():
191193
if csv_type == "user":
192-
filepath = os.path.join(
193-
conf.KOLIBRI_HOME,
194-
"log_export",
195-
CSV_EXPORT_FILENAMES[csv_type].format(facility.name, facility.id[:4]),
194+
filename = CSV_EXPORT_FILENAMES[csv_type].format(
195+
facility.name, facility.id[:4]
196196
)
197197
else:
198198
log_request = _get_log_request(csv_type, facility_id)
199199
if log_request:
200200
start = log_request.selected_start_date.isoformat()
201201
end = log_request.selected_end_date.isoformat()
202-
filepath = os.path.join(
203-
conf.KOLIBRI_HOME,
204-
"log_export",
205-
CSV_EXPORT_FILENAMES[csv_type].format(
206-
facility.name, facility.id[:4], start[:10], end[:10]
207-
),
202+
203+
filename = CSV_EXPORT_FILENAMES[csv_type].format(
204+
facility.name, facility.id[:4], start[:10], end[:10]
208205
)
209206
else:
210-
filepath = None
207+
filename = None
211208

212209
# if the file does not exist on disk, return a 404
213-
if filepath is None or not os.path.exists(filepath):
210+
if not file_storage.exists(filename):
214211
raise Http404("There is no csv export file for {} available".format(csv_type))
215212

216213
# generate a file response
217-
response = FileResponse(io.open(filepath, "rb"))
214+
response = FileResponse(file_storage.open(filename, "rb"))
218215
# set the content-type by guessing from the filename
219216
response.headers["Content-Type"] = "text/csv"
220217

@@ -234,6 +231,6 @@ def download_csv_file(request, csv_type, facility_id):
234231
translation.deactivate()
235232

236233
# set the content-length to the file size
237-
response.headers["Content-Length"] = os.path.getsize(filepath)
234+
response.headers["Content-Length"] = file_storage.size(filename)
238235

239236
return response

0 commit comments

Comments
 (0)