1
- import io
2
1
import json
3
2
import os
4
3
from datetime import datetime as dt
5
4
6
5
from django .core .exceptions import PermissionDenied
6
+ from django .core .files .storage import DefaultStorage
7
7
from django .http import Http404
8
8
from django .http import HttpResponse
9
9
from django .http .response import FileResponse
@@ -187,34 +187,31 @@ def download_csv_file(request, csv_type, facility_id):
187
187
).replace ("-" , "_" ),
188
188
}
189
189
190
+ file_storage = DefaultStorage ()
191
+
190
192
if csv_type in CSV_EXPORT_FILENAMES .keys ():
191
193
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 ]
196
196
)
197
197
else :
198
198
log_request = _get_log_request (csv_type , facility_id )
199
199
if log_request :
200
200
start = log_request .selected_start_date .isoformat ()
201
201
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 ]
208
205
)
209
206
else :
210
- filepath = None
207
+ filename = None
211
208
212
209
# 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 ):
214
211
raise Http404 ("There is no csv export file for {} available" .format (csv_type ))
215
212
216
213
# generate a file response
217
- response = FileResponse (io .open (filepath , "rb" ))
214
+ response = FileResponse (file_storage .open (filename , "rb" ))
218
215
# set the content-type by guessing from the filename
219
216
response .headers ["Content-Type" ] = "text/csv"
220
217
@@ -234,6 +231,6 @@ def download_csv_file(request, csv_type, facility_id):
234
231
translation .deactivate ()
235
232
236
233
# 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 )
238
235
239
236
return response
0 commit comments