Skip to content

Commit 006eb62

Browse files
RishabhJain2018deshraj
authored andcommitted
Challenges: Fix bug in leaderboard consistency for challenge hosts and participants (Cloud-CV#2156)
1 parent e85e5ca commit 006eb62

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

apps/jobs/views.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ def leaderboard(request, challenge_phase_split_id):
342342

343343
challenge_host_user = is_user_a_host_of_challenge(request.user, challenge_obj.pk)
344344

345+
# Check if challenge phase leaderboard is public for participant user or not
346+
if challenge_phase_split.visibility != ChallengePhaseSplit.PUBLIC and not challenge_host_user:
347+
response_data = {'error': 'Sorry, the leaderboard is not public!'}
348+
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
349+
345350
leaderboard_data = LeaderboardData.objects.exclude(
346351
submission__created_by__email__in=challenge_hosts_emails)
347352

@@ -356,7 +361,7 @@ def leaderboard(request, challenge_phase_split_id):
356361
'id', 'submission__participant_team__team_name',
357362
'challenge_phase_split', 'result', 'filtering_score', 'leaderboard__schema', 'submission__submitted_at')
358363

359-
if not challenge_host_user:
364+
if challenge_phase_split.visibility == ChallengePhaseSplit.PUBLIC:
360365
leaderboard_data = leaderboard_data.filter(submission__is_public=True)
361366

362367
sorted_leaderboard_data = sorted(leaderboard_data, key=lambda k: float(k['filtering_score']), reverse=True)
@@ -379,14 +384,8 @@ def leaderboard(request, challenge_phase_split_id):
379384
distinct_sorted_leaderboard_data,
380385
request,
381386
pagination_class=StandardResultSetPagination())
382-
383-
# Check if challenge phase leaderboard is public for participant user or not
384-
if challenge_phase_split.visibility != ChallengePhaseSplit.PUBLIC and not challenge_host_user:
385-
response_data = {'error': 'Sorry, the leaderboard is not public!'}
386-
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
387-
else:
388-
response_data = result_page
389-
return paginator.get_paginated_response(response_data)
387+
response_data = result_page
388+
return paginator.get_paginated_response(response_data)
390389

391390

392391
@throttle_classes([UserRateThrottle])

tests/unit/jobs/test_views.py

+57-10
Original file line numberDiff line numberDiff line change
@@ -581,20 +581,23 @@ def test_get_remaining_submission_when_submission_made_three_days_back(self):
581581
'challenge_pk': self.challenge.pk
582582
})
583583
self.submission3.status = 'cancelled'
584-
self.submission2.status = 'failed'
585584
self.submission3.save()
585+
self.submission2.status = 'failed'
586586
self.submission2.save()
587+
self.submission1.submitted_at = timezone.now() - timedelta(days=3)
588+
self.submission1.save()
589+
self.challenge.participant_teams.add(self.participant_team)
590+
self.challenge.save()
591+
592+
monthly_count = 0 if self.submission1.submitted_at.month != timezone.now().month else 1
593+
remaining_monthly_submissions = self.challenge_phase.max_submissions_per_month - monthly_count
587594

588595
expected = {
589596
'remaining_submissions_today_count': 10,
590-
'remaining_submissions_this_month_count': 19,
597+
'remaining_submissions_this_month_count': remaining_monthly_submissions,
591598
'remaining_submissions': 99
592599
}
593600

594-
self.challenge.participant_teams.add(self.participant_team)
595-
self.challenge.save()
596-
self.submission1.submitted_at = timezone.now() - timedelta(days=3)
597-
self.submission1.save()
598601
response = self.client.get(self.url, {})
599602
self.assertEqual(response.data, expected)
600603
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -1278,26 +1281,44 @@ def setUp(self):
12781281
method_name="Test Method",
12791282
method_description="Test Description",
12801283
project_url="http://testserver/",
1281-
publication_url="http://testserver/"
1284+
publication_url="http://testserver/",
1285+
is_public=True
1286+
)
1287+
1288+
self.submission_2 = Submission.objects.create(
1289+
participant_team=self.participant_team,
1290+
challenge_phase=self.challenge_phase,
1291+
created_by=self.user1,
1292+
status="submitted",
1293+
input_file=self.challenge_phase.test_annotation,
1294+
method_name="Test Method",
1295+
method_description="Test Description",
1296+
project_url="http://testserver/",
1297+
publication_url="http://testserver/",
1298+
is_public=False
12821299
)
12831300

12841301
self.private_submission = Submission.objects.create(
12851302
participant_team=self.host_participant_team,
12861303
challenge_phase=self.private_challenge_phase,
1287-
created_by=self.user,
1304+
created_by=self.user1,
12881305
status="submitted",
12891306
input_file=self.private_challenge_phase.test_annotation,
12901307
method_name="Test Method",
12911308
method_description="Test Description",
12921309
project_url="http://testserver/",
1293-
publication_url="http://testserver/"
1310+
publication_url="http://testserver/",
12941311
)
12951312

12961313
self.submission.is_public = True
12971314
self.submission.status = Submission.FINISHED
12981315
self.submission.save()
12991316

1300-
self.private_submission.is_public = True
1317+
self.submission_2.is_public = False
1318+
self.submission_2.status = Submission.FINISHED
1319+
self.submission_2.save()
1320+
1321+
self.private_submission.is_public = False
13011322
self.private_submission.status = Submission.FINISHED
13021323
self.private_submission.save()
13031324

@@ -1306,6 +1327,11 @@ def setUp(self):
13061327
'test-score': 75.0
13071328
}
13081329

1330+
self.result_json_2 = {
1331+
'score': 10.0,
1332+
'test-score': 20.0
1333+
}
1334+
13091335
self.expected_results = [self.result_json['score'], self.result_json['test-score']]
13101336
self.filtering_score = self.result_json[self.leaderboard.schema['default_order_by']]
13111337

@@ -1316,6 +1342,13 @@ def setUp(self):
13161342
result=self.result_json
13171343
)
13181344

1345+
self.leaderboard_data_2 = LeaderboardData.objects.create(
1346+
challenge_phase_split=self.challenge_phase_split,
1347+
submission=self.submission,
1348+
leaderboard=self.leaderboard,
1349+
result=self.result_json_2
1350+
)
1351+
13191352
self.private_leaderboard_data = LeaderboardData.objects.create(
13201353
challenge_phase_split=self.private_challenge_phase_split,
13211354
submission=self.private_submission,
@@ -1414,3 +1447,17 @@ def test_get_leaderboard_for_host_submissions_on_private_challenge_phase(self):
14141447
self.assertEqual(response.data['previous'], expected['previous'])
14151448
self.assertEqual(response.data['results'], expected['results'])
14161449
self.assertEqual(response.status_code, status.HTTP_200_OK)
1450+
1451+
def test_get_private_leaderboard_when_user_is_participant(self):
1452+
self.url = reverse_lazy('jobs:leaderboard',
1453+
kwargs={'challenge_phase_split_id': self.private_challenge_phase_split.id})
1454+
1455+
expected = {
1456+
'error': 'Sorry, the leaderboard is not public!'
1457+
}
1458+
1459+
self.client.force_authenticate(user=self.user1)
1460+
1461+
response = self.client.get(self.url, {})
1462+
self.assertEqual(response.data, expected)
1463+
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

0 commit comments

Comments
 (0)