Skip to content

Commit 902fafd

Browse files
Backend: dynamically generate CSV filenames based on challenge and phase details for submission download (#4811)
* Enhance submission download functionality: dynamically generate CSV filenames based on challenge and phase details. * Refactor loop to use 'let' for block scoping and add initial state for page and phases in challenge controller tests
1 parent fbcaeaf commit 902fafd

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

apps/challenges/views.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,7 @@ def download_all_submissions(
23002300

23012301
response = HttpResponse(content_type="text/csv")
23022302
response["Content-Disposition"] = (
2303-
f"attachment; filename={filename}"
2303+
f'attachment; filename="{filename}"'
23042304
)
23052305
writer = csv.writer(response)
23062306
writer.writerow(
@@ -2447,9 +2447,15 @@ def download_all_submissions(
24472447
submissions = ChallengeSubmissionManagementSerializer(
24482448
submissions, many=True, context={"request": request}
24492449
)
2450+
2451+
# Use helper function to generate filename
2452+
filename = get_submissions_csv_filename(
2453+
challenge, challenge_phase
2454+
)
2455+
24502456
response = HttpResponse(content_type="text/csv")
24512457
response["Content-Disposition"] = (
2452-
"attachment; filename=all_submissions.csv"
2458+
f'attachment; filename="{filename}"'
24532459
)
24542460
writer = csv.writer(response)
24552461
writer.writerow(
@@ -2539,7 +2545,7 @@ def download_all_submissions(
25392545

25402546
response = HttpResponse(content_type="text/csv")
25412547
response["Content-Disposition"] = (
2542-
f"attachment; filename={filename}"
2548+
f'attachment; filename="{filename}"'
25432549
)
25442550
writer = csv.writer(response)
25452551
fields = [fields_to_export[field] for field in request.data]

frontend/src/js/controllers/challengeCtrl.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,17 @@
21732173

21742174
vm.downloadChallengeSubmissions = function() {
21752175
if (vm.phaseId) {
2176+
// Generate dynamic filename based on challenge and phase info
2177+
var challengeName = vm.page.title.replace(/\s+/g, '_').replace(/\//g, '_');
2178+
var phaseName = '';
2179+
for (var i = 0; i < vm.phases.results.length; i++) {
2180+
if (vm.phases.results[i].id == vm.phaseId) {
2181+
phaseName = vm.phases.results[i].name.replace(/\s+/g, '_').replace(/\//g, '_');
2182+
break;
2183+
}
2184+
}
2185+
var filename = 'all_submissions_' + challengeName + '_' + vm.challengeId + '_' + phaseName + '_' + vm.phaseId + '.csv';
2186+
21762187
parameters.url = "challenges/" + vm.challengeId + "/phase/" + vm.phaseId + "/download_all_submissions/" + vm.fileSelected + "/";
21772188
if (vm.fieldsToGet === undefined || vm.fieldsToGet.length === 0) {
21782189
parameters.method = "GET";
@@ -2182,7 +2193,7 @@
21822193
var anchor = angular.element('<a/>');
21832194
anchor.attr({
21842195
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(details),
2185-
download: 'all_submissions.csv'
2196+
download: filename
21862197
})[0].click();
21872198
},
21882199
onError: function(response) {
@@ -2195,7 +2206,7 @@
21952206
else {
21962207
parameters.method = "POST";
21972208
var fieldsExport = [];
2198-
for(var i = 0 ; i < vm.fields.length ; i++) {
2209+
for(let i = 0 ; i < vm.fields.length ; i++) {
21992210
if (vm.fieldsToGet.includes(vm.fields[i].id)) {
22002211
fieldsExport.push(vm.fields[i].id);
22012212
}
@@ -2207,7 +2218,7 @@
22072218
var anchor = angular.element('<a/>');
22082219
anchor.attr({
22092220
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(details),
2210-
download: 'all_submissions.csv'
2221+
download: filename
22112222
})[0].click();
22122223
},
22132224
onError: function(response) {

frontend/tests/controllers-test/challengeCtrl.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,6 +3516,12 @@ describe('Unit tests for challenge controller', function () {
35163516
{ id: 'participant_team' },
35173517
{ id: 'status' }
35183518
];
3519+
vm.page = { title: 'Test Challenge' };
3520+
vm.phases = {
3521+
results: [
3522+
{ id: 2, name: 'Test Phase' }
3523+
]
3524+
};
35193525
});
35203526

35213527
it('should send GET request and trigger download when fieldsToGet is undefined', function () {

0 commit comments

Comments
 (0)