Repository: Cloud-CV/EvalAI

Issue:
Add Team members column to exported csv file.
**Current behaviour:** There is no column for participant team members in the exported `csv` file.
**Expected behaviour:** Add a column for `participant team members` in the `csv` file.


Discussion:

Resolving patch:
diff --git a/apps/challenges/views.py b/apps/challenges/views.py
--- a/apps/challenges/views.py
+++ b/apps/challenges/views.py
@@ -760,11 +760,14 @@ def download_all_submissions(request, challenge_pk, challenge_phase_pk, file_typ
     if file_type == 'csv':
         if is_user_a_host_of_challenge(user=request.user, challenge_pk=challenge_pk):
             submissions = Submission.objects.filter(challenge_phase__challenge=challenge).order_by('-submitted_at')
+            submissions = ChallengeSubmissionManagementSerializer(submissions, many=True, context={'request': request})
             response = HttpResponse(content_type='text/csv')
             response['Content-Disposition'] = 'attachment; filename=all_submissions.csv'
             writer = csv.writer(response)
             writer.writerow(['id',
                              'Team Name',
+                             'Team Members',
+                             'Team Members Email Id',
                              'Challenge Phase',
                              'Status',
                              'Created By',
@@ -777,20 +780,22 @@ def download_all_submissions(request, challenge_pk, challenge_phase_pk, file_typ
                              'Submission Result File',
                              'Submission Metadata File',
                              ])
-            for submission in submissions:
-                writer.writerow([submission.id,
-                                 submission.participant_team,
-                                 submission.challenge_phase,
-                                 submission.status,
-                                 submission.created_by,
-                                 submission.execution_time,
-                                 submission.submission_number,
-                                 submission.input_file,
-                                 submission.stdout_file,
-                                 submission.stderr_file,
-                                 submission.created_at,
-                                 submission.submission_result_file,
-                                 submission.submission_metadata_file,
+            for submission in submissions.data:
+                writer.writerow([submission['id'],
+                                 submission['participant_team'],
+                                 ",".join(username['username'] for username in submission['participant_team_members']),
+                                 ",".join(email['email'] for email in submission['participant_team_members']),
+                                 submission['challenge_phase'],
+                                 submission['status'],
+                                 submission['created_by'],
+                                 submission['execution_time'],
+                                 submission['submission_number'],
+                                 submission['input_file'],
+                                 submission['stdout_file'],
+                                 submission['stderr_file'],
+                                 submission['created_at'],
+                                 submission['submission_result_file'],
+                                 submission['submission_metadata_file'],
                                  ])
             return response
 
@@ -803,7 +808,7 @@ def download_all_submissions(request, challenge_pk, challenge_phase_pk, file_typ
             # Filter submissions on the basis of challenge phase for a participant.
             submissions = Submission.objects.filter(participant_team=participant_team_pk,
                                                     challenge_phase=challenge_phase).order_by('-submitted_at')
-
+            submissions = ChallengeSubmissionManagementSerializer(submissions, many=True, context={'request': request})
             response = HttpResponse(content_type='text/csv')
             response['Content-Disposition'] = 'attachment; filename=all_submissions.csv'
             writer = csv.writer(response)
@@ -817,16 +822,16 @@ def download_all_submissions(request, challenge_pk, challenge_phase_pk, file_typ
                              'Stderr File',
                              'Submitted At',
                              ])
-            for submission in submissions:
-                writer.writerow([submission.participant_team,
-                                 submission.method_name,
-                                 submission.status,
-                                 submission.execution_time,
-                                 submission.input_file,
-                                 submission.submission_result_file,
-                                 submission.stdout_file,
-                                 submission.stderr_file,
-                                 submission.created_at,
+            for submission in submissions.data:
+                writer.writerow([submission['participant_team'],
+                                 submission['method_name'],
+                                 submission['status'],
+                                 submission['execution_time'],
+                                 submission['input_file'],
+                                 submission['submission_result_file'],
+                                 submission['stdout_file'],
+                                 submission['stderr_file'],
+                                 submission['created_at'],
                                  ])
             return response
         else:
diff --git a/apps/jobs/serializers.py b/apps/jobs/serializers.py
--- a/apps/jobs/serializers.py
+++ b/apps/jobs/serializers.py
@@ -67,13 +67,15 @@ class ChallengeSubmissionManagementSerializer(serializers.ModelSerializer):
     challenge_phase = serializers.SerializerMethodField()
     created_by = serializers.SerializerMethodField()
     participant_team_members_email_ids = serializers.SerializerMethodField()
+    created_at = serializers.SerializerMethodField()
+    participant_team_members = serializers.SerializerMethodField()
 
     class Meta:
         model = Submission
         fields = ('id', 'participant_team', 'challenge_phase', 'created_by', 'status', 'is_public',
                   'submission_number', 'submitted_at', 'execution_time', 'input_file', 'stdout_file',
                   'stderr_file', 'submission_result_file', 'submission_metadata_file',
-                  'participant_team_members_email_ids',)
+                  'participant_team_members_email_ids', 'created_at', 'method_name', 'participant_team_members',)
 
     def get_participant_team(self, obj):
         return obj.participant_team.team_name
@@ -93,6 +95,18 @@ def get_participant_team_members_email_ids(self, obj):
         participant_ids = Participant.objects.filter(team=participant_team).values_list('user_id', flat=True)
         return list(User.objects.filter(id__in=participant_ids).values_list('email', flat=True))
 
+    def get_created_at(self, obj):
+        return obj.created_at
+
+    def get_participant_team_members(self, obj):
+        try:
+            participant_team = ParticipantTeam.objects.get(team_name=obj.participant_team.team_name)
+        except ParticipantTeam.DoesNotExist:
+            return 'Participant team does not exist'
+
+        participant_ids = Participant.objects.filter(team=participant_team).values_list('user_id', flat=True)
+        return list(User.objects.filter(id__in=participant_ids).values('username', 'email'))
+
 
 class SubmissionCount(object):
     def __init__(self, submission_count):


Test patch:
diff --git a/tests/unit/challenges/test_views.py b/tests/unit/challenges/test_views.py
--- a/tests/unit/challenges/test_views.py
+++ b/tests/unit/challenges/test_views.py
@@ -1555,7 +1555,7 @@ def setUp(self):
             email='user5@test.com',)
 
         self.user6 = User.objects.create(
-            username='not a challenge host',
+            username='participant',
             password='secret password',
             email='user6@test.com',)
 
@@ -1681,7 +1681,6 @@ def test_get_all_submissions_when_user_is_host_of_challenge(self):
                                 kwargs={'challenge_pk': self.challenge5.pk,
                                         'challenge_phase_pk': self.challenge_phase5.pk})
         self.client.force_authenticate(user=self.user5)
-
         expected = [
             {
                 "id": self.submission.id,
@@ -1699,6 +1698,9 @@ def test_get_all_submissions_when_user_is_host_of_challenge(self):
                 "submission_result_file": None,
                 "submission_metadata_file": None,
                 "participant_team_members_email_ids": ['user6@test.com'],
+                "participant_team_members": [{'username': 'participant', 'email': 'user6@test.com'}],
+                "created_at": self.submission.created_at,
+                "method_name": self.submission.method_name,
             }
         ]
         response = self.client.get(self.url, {})