Skip to content

Commit 3485e2e

Browse files
authored
Merge pull request #601 from xchem/m2ms-1393-download-file-name
feat: filename attribute to RHS download serializer (issue 1393)
2 parents b7a4f55 + d173f1b commit 3485e2e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

viewer/serializers.py

+1
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ class DiscoursePostWriteSerializer(serializers.Serializer):
657657

658658
class DictToCsvSerializer(serializers.Serializer):
659659
title = serializers.CharField(max_length=200)
660+
filename = serializers.CharField()
660661
dict = serializers.DictField()
661662

662663

viewer/views.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1312,11 +1312,14 @@ def list(self, request):
13121312
"""Method to handle GET request"""
13131313
file_url = request.GET.get('file_url')
13141314

1315+
filename = str(Path(file_url).name)
1316+
13151317
if file_url and os.path.isfile(file_url):
13161318
with open(file_url, encoding='utf8') as csvfile:
13171319
# return file and tidy up.
13181320
response = HttpResponse(csvfile, content_type='text/csv')
1319-
response['Content-Disposition'] = 'attachment; filename=download.csv'
1321+
# response['Content-Disposition'] = 'attachment; filename=download.csv'
1322+
response['Content-Disposition'] = f'attachment; filename={filename}'
13201323
shutil.rmtree(os.path.dirname(file_url), ignore_errors=True)
13211324
return response
13221325
else:
@@ -1327,11 +1330,14 @@ def create(self, request):
13271330
logger.info('+ DictToCsv.post')
13281331
input_dict = request.data['dict']
13291332
input_title = request.data['title']
1333+
filename = request.data.get('filename', 'download.csv')
13301334

13311335
if not input_dict:
13321336
return Response({"message": "Please enter Dictionary"})
13331337
else:
1334-
filename_url = create_csv_from_dict(input_dict, input_title)
1338+
filename_url = create_csv_from_dict(
1339+
input_dict, input_title, filename=filename
1340+
)
13351341

13361342
return Response({"file_url": filename_url})
13371343

0 commit comments

Comments
 (0)