-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathcontests.py
More file actions
1489 lines (1190 loc) · 61.4 KB
/
Copy pathcontests.py
File metadata and controls
1489 lines (1190 loc) · 61.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
from calendar import Calendar, SUNDAY
from collections import defaultdict, namedtuple
from datetime import date, datetime, time, timedelta
from functools import partial
from operator import attrgetter, itemgetter
from django import forms
from django.conf import settings
from django.contrib.auth.context_processors import PermWrapper
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied
from django.db import IntegrityError
from django.db.models import BooleanField, Case, Count, F, FloatField, IntegerField, Max, Min, Q, Sum, Value, When
from django.db.models.expressions import CombinedExpression
from django.db.models.query import Prefetch
from django.http import Http404, HttpResponse, HttpResponseForbidden, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect, render
from django.template.defaultfilters import date as date_filter, floatformat
from django.template.loader import get_template
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe
from django.utils.timezone import make_aware
from django.utils.translation import gettext as _, gettext_lazy
from django.views.generic import FormView, ListView, TemplateView, View
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.list import BaseListView
from icalendar import Calendar as ICalendar, Event
from reversion import revisions
from judge.comments import CommentedDetailView
from judge.contest_format import ICPCContestFormat
from judge.forms import ContestAnnouncementForm, ContestCloneForm, ContestDownloadDataForm, ContestForm, \
ProposeContestProblemFormSet
from judge.models import Contest, ContestAnnouncement, ContestMoss, ContestParticipation, ContestProblem, ContestTag, \
Language, Organization, Problem, ProblemClarification, Profile, Submission
from judge.tasks import on_new_contest, prepare_contest_data, rescore_problem, run_moss
from judge.utils.celery import redirect_to_task_status, task_status_by_id, task_status_url_by_id
from judge.utils.cms import parse_csv_ranking
from judge.utils.infinite_paginator import InfinitePaginationMixin
from judge.utils.opengraph import generate_opengraph
from judge.utils.problems import _get_result_data, user_attempted_ids, user_completed_ids
from judge.utils.ranker import ranker
from judge.utils.stats import get_bar_chart, get_pie_chart, get_stacked_bar_chart
from judge.utils.views import SingleObjectFormView, TitleMixin, \
add_file_response, generic_message, paginate_query_context
__all__ = ['ContestList', 'ContestDetail', 'ContestRanking', 'ContestJoin', 'ContestLeave', 'ContestCalendar',
'ContestClone', 'ContestStats', 'ContestMossView', 'ContestMossDelete',
'ContestParticipationList', 'ContestParticipationDisqualify', 'get_contest_ranking_list',
'base_contest_ranking_list', 'ContestProblemMakePublic']
def _find_contest(request, key, private_check=True):
try:
contest = Contest.objects.get(key=key)
if private_check and not contest.is_accessible_by(request.user):
raise ObjectDoesNotExist()
except ObjectDoesNotExist:
return generic_message(request, _('No such contest'),
_('Could not find a contest with the key "%s".') % key, status=404), False
return contest, True
class ContestListMixin(object):
hide_private_contests = False
def get_queryset(self):
if self.hide_private_contests is not None:
if 'hide_private_contests' in self.request.GET:
self.hide_private_contests = self.request.session['hide_private_contests'] \
= self.request.GET.get('hide_private_contests').lower() == 'true'
else:
self.hide_private_contests = self.request.session.get('hide_private_contests', False)
queryset = Contest.get_visible_contests(self.request.user)
if self.hide_private_contests:
queryset = queryset.filter(is_organization_private=False)
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['hide_private_contests'] = self.hide_private_contests
return context
class ContestList(InfinitePaginationMixin, TitleMixin, ContestListMixin, ListView):
model = Contest
paginate_by = 20
template_name = 'contest/list.html'
title = gettext_lazy('Contests')
context_object_name = 'past_contests'
@cached_property
def _now(self):
return timezone.now()
def _get_queryset(self):
return super().get_queryset().prefetch_related(
'tags', 'organization', 'authors', 'curators', 'testers', 'view_contest_scoreboard',
)
def get_queryset(self):
self.search_query = None
query_set = self._get_queryset().order_by('-end_time', 'key').filter(end_time__lt=self._now)
if 'search' in self.request.GET:
self.search_query = search_query = ' '.join(self.request.GET.getlist('search')).strip()
if search_query:
query_set = query_set.filter(Q(key__icontains=search_query) | Q(name__icontains=search_query))
return query_set
def get_paginator(self, queryset, per_page, orphans=0, allow_empty_first_page=True, **kwargs):
return super().get_paginator(queryset, per_page, orphans, allow_empty_first_page,
count=self.get_queryset().values('id').count(), **kwargs)
def get_context_data(self, **kwargs):
context = super(ContestList, self).get_context_data(**kwargs)
present, active, future = [], [], []
finished = set()
for contest in self._get_queryset().exclude(end_time__lt=self._now):
if contest.start_time > self._now:
future.append(contest)
else:
present.append(contest)
if self.request.user.is_authenticated:
for participation in ContestParticipation.objects.filter(virtual=0, user=self.request.profile,
contest_id__in=present) \
.select_related('contest') \
.prefetch_related('contest__authors', 'contest__curators', 'contest__testers',
'contest__view_contest_scoreboard') \
.annotate(key=F('contest__key')):
if participation.ended:
finished.add(participation.contest.key)
else:
active.append(participation)
present.remove(participation.contest)
active.sort(key=attrgetter('end_time', 'key'))
present.sort(key=attrgetter('end_time', 'key'))
future.sort(key=attrgetter('start_time'))
context['active_participations'] = active
context['current_contests'] = present
context['future_contests'] = future
context['finished_contests'] = finished
context['now'] = self._now
context['first_page_href'] = '.'
context['page_suffix'] = '#past-contests'
context['search_query'] = self.search_query
context.update(paginate_query_context(self.request))
return context
class PrivateContestError(Exception):
def __init__(self, name, is_private, is_organization_private, org):
self.name = name
self.is_private = is_private
self.is_organization_private = is_organization_private
self.org = org
class ContestMixin(object):
context_object_name = 'contest'
model = Contest
slug_field = 'key'
slug_url_kwarg = 'contest'
@cached_property
def is_in_contest(self):
return self.object.is_in_contest(self.request.user)
@cached_property
def is_editor(self):
if not self.request.user.is_authenticated:
return False
return self.request.profile.id in self.object.editor_ids
@cached_property
def is_tester(self):
if not self.request.user.is_authenticated:
return False
return self.request.profile.id in self.object.tester_ids
@cached_property
def can_edit(self):
return self.object.is_editable_by(self.request.user)
@cached_property
def can_view_all_problems(self):
return self.is_in_contest or self.is_editor or self.is_tester or self.request.user.is_superuser or \
not Problem.objects.filter(contests__contest=self.object, is_public=False).exists()
def get_context_data(self, **kwargs):
context = super(ContestMixin, self).get_context_data(**kwargs)
if self.request.user.is_authenticated:
try:
context['live_participation'] = (
self.request.profile.contest_history.get(
contest=self.object,
virtual=ContestParticipation.LIVE,
)
)
except ContestParticipation.DoesNotExist:
context['live_participation'] = None
context['has_joined'] = False
else:
context['has_joined'] = True
else:
context['live_participation'] = None
context['has_joined'] = False
context['now'] = self.object._now
context['is_in_contest'] = self.is_in_contest
context['is_editor'] = self.is_editor
context['is_tester'] = self.is_tester
context['can_edit'] = self.can_edit
if not self.object.og_image or not self.object.summary:
metadata = generate_opengraph('generated-meta-contest:%d' % self.object.id,
self.object.description, 'contest')
context['meta_description'] = self.object.summary or metadata[0]
context['og_image'] = self.object.og_image or metadata[1]
context['has_moss_api_key'] = settings.MOSS_API_KEY is not None
context['logo_override_image'] = self.object.logo_override_image
if not context['logo_override_image'] and self.object.organization:
context['logo_override_image'] = self.object.organization.logo_override_image
context['is_ICPC_format'] = (self.object.format.name == ICPCContestFormat.name)
return context
def get_object(self, queryset=None):
contest = super(ContestMixin, self).get_object(queryset)
profile = self.request.profile
if (profile is not None and
ContestParticipation.objects.filter(id=profile.current_contest_id, contest_id=contest.id).exists()):
return contest
try:
contest.access_check(self.request.user)
except Contest.PrivateContest:
raise PrivateContestError(contest.name, contest.is_private, contest.is_organization_private,
contest.organization)
except Contest.Inaccessible:
raise Http404()
else:
return contest
def dispatch(self, request, *args, **kwargs):
try:
return super(ContestMixin, self).dispatch(request, *args, **kwargs)
except Http404:
key = kwargs.get(self.slug_url_kwarg, None)
if key:
return generic_message(request, _('No such contest'),
_('Could not find a contest with the key "%s".') % key)
else:
return generic_message(request, _('No such contest'),
_('Could not find such contest.'))
except PrivateContestError as e:
return render(request, 'contest/private.html', {
'error': e, 'title': _('Access to contest "%s" denied') % e.name,
}, status=403)
except PermissionDenied as e:
return generic_message(request, _('Permission denied'), e)
class ContestDetail(ContestMixin, TitleMixin, CommentedDetailView):
template_name = 'contest/contest.html'
def is_comment_locked(self):
if self.object.use_clarifications:
now = timezone.now()
if self.is_in_contest or (self.object.start_time <= now and now <= self.object.end_time):
return True
return super(ContestDetail, self).is_comment_locked()
def get_comment_page(self):
return 'c:%s' % self.object.key
def get_title(self):
return self.object.name
def get_context_data(self, **kwargs):
context = super(ContestDetail, self).get_context_data(**kwargs)
context['can_view_all_problems'] = self.can_view_all_problems
context['contest_problems'] = Problem.objects.filter(contests__contest=self.object) \
.order_by('contests__order').defer('description') \
.annotate(has_public_editorial=Case(
When(solution__is_public=True, solution__publish_on__lte=timezone.now(), then=True),
default=False,
output_field=BooleanField(),
)) \
.add_i18n_name(self.request.LANGUAGE_CODE)
# convert to problem points in contest instead of actual points
points_list = list(self.object.contest_problems.values_list('points').order_by('order'))
for idx, p in enumerate(context['contest_problems']):
p.points = points_list[idx][0]
context['metadata'] = {
'has_public_editorials': any(
problem.is_public and problem.has_public_editorial for problem in context['contest_problems']
) if self.object.ended else False,
}
context['metadata'].update(
**self.object.contest_problems
.annotate(
partials_enabled=F('partial').bitand(F('problem__partial')),
pretests_enabled=F('is_pretested').bitand(F('contest__run_pretests_only')),
)
.aggregate(
has_partials=Sum('partials_enabled', output_field=BooleanField()),
has_pretests=Sum('pretests_enabled', output_field=BooleanField()),
has_submission_cap=Sum('max_submissions'),
problem_count=Count('id'),
),
)
clarifications = ProblemClarification.objects.filter(problem__in=self.object.problems.all())
context['has_clarifications'] = clarifications.count() > 0
context['clarifications'] = clarifications.order_by('-date')
announcements = ContestAnnouncement.objects.filter(contest=self.object)
context['has_announcements'] = announcements.count() > 0
context['announcements'] = announcements.order_by('-date')
context['can_announce'] = self.object.is_editable_by(self.request.user)
authenticated = self.request.user.is_authenticated
context['completed_problem_ids'] = user_completed_ids(self.request.profile) if authenticated else []
context['attempted_problem_ids'] = user_attempted_ids(self.request.profile) if authenticated else []
context['can_download_data'] = bool(settings.DMOJ_CONTEST_DATA_DOWNLOAD)
return context
class ContestAllProblems(ContestMixin, TitleMixin, DetailView):
template_name = 'contest/contest-all-problems.html'
def get_title(self):
return self.object.name
def get_context_data(self, **kwargs):
context = super(ContestAllProblems, self).get_context_data(**kwargs)
if not self.can_view_all_problems:
raise Http404()
context['contest_problems'] = Problem.objects.filter(contests__contest=self.object) \
.order_by('contests__order') \
.add_i18n_name(self.request.LANGUAGE_CODE) \
.add_i18n_description(self.request.LANGUAGE_CODE)
# convert to problem points in contest instead of actual points
points_list = list(self.object.contest_problems.values_list('points').order_by('order'))
for idx, p in enumerate(context['contest_problems']):
p.points = points_list[idx][0]
authenticated = self.request.user.is_authenticated
context['completed_problem_ids'] = user_completed_ids(self.request.profile) if authenticated else []
context['attempted_problem_ids'] = user_attempted_ids(self.request.profile) if authenticated else []
return context
class ContestClone(ContestMixin, PermissionRequiredMixin, TitleMixin, SingleObjectFormView):
title = gettext_lazy('Clone Contest')
template_name = 'contest/clone.html'
form_class = ContestCloneForm
permission_required = 'judge.clone_contest'
permission_denied_message = _('You are not allowed to clone contests.')
def get_object(self, queryset=None):
contest = super().get_object(queryset)
if not contest.is_editable_by(self.request.user):
raise PermissionDenied(_('You are not allowed to edit this contest.'))
return contest
def form_valid(self, form):
contest = self.object
# Using list() to force QuerySets evaluation, as `contest.pk = None` affects these queries
tags = list(contest.tags.all())
organization = contest.organization
private_contestants = list(contest.private_contestants.all())
view_contest_scoreboard = list(contest.view_contest_scoreboard.all())
contest_problems = list(contest.contest_problems.all())
old_key = contest.key
contest.pk = None
contest.is_visible = False
contest.user_count = 0
contest.virtual_count = 0
contest.locked_after = None
contest.key = form.cleaned_data['key']
with revisions.create_revision(atomic=True):
contest.save()
contest.tags.set(tags)
contest.organization = organization
contest.private_contestants.set(private_contestants)
contest.view_contest_scoreboard.set(view_contest_scoreboard)
contest.authors.add(self.request.profile)
for problem in contest_problems:
problem.contest = contest
problem.pk = None
ContestProblem.objects.bulk_create(contest_problems)
revisions.set_user(self.request.user)
revisions.set_comment(_('Cloned contest from %s') % old_key)
return HttpResponseRedirect(reverse('contest_edit', args=(contest.key,)))
class ContestAnnounce(ContestMixin, TitleMixin, SingleObjectFormView):
title = gettext_lazy('Create contest announcement')
template_name = 'contest/create-announcement.html'
form_class = ContestAnnouncementForm
def get_object(self, queryset=None):
contest = super().get_object(queryset)
if not contest.is_editable_by(self.request.user):
raise PermissionDenied(_('You are not allowed to edit this contest.'))
return contest
def form_valid(self, form):
contest = self.object
announcement = form.save(commit=False)
announcement.contest = contest
announcement.save()
announcement.send()
return HttpResponseRedirect(reverse('contest_view', args=(contest.key,)))
class ContestAccessDenied(Exception):
pass
class ContestAccessCodeForm(forms.Form):
access_code = forms.CharField(max_length=255)
def __init__(self, *args, **kwargs):
super(ContestAccessCodeForm, self).__init__(*args, **kwargs)
self.fields['access_code'].widget.attrs.update({'autocomplete': 'off'})
class ContestRegister(LoginRequiredMixin, ContestMixin, SingleObjectMixin, View):
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return self.ask_for_access_code()
def post(self, request, *args, **kwargs):
self.object = self.get_object()
try:
return self.register_contest(request)
except ContestAccessDenied:
if request.POST.get('access_code'):
return self.ask_for_access_code(ContestAccessCodeForm(request.POST))
else:
return HttpResponseRedirect(request.path)
def register_contest(self, request, access_code=None):
contest = self.object
profile = request.profile
if self.is_editor or self.is_tester:
return generic_message(request, _('Cannot register'),
_('You cannot register for this contest.'))
if not request.user.is_superuser and contest.banned_users.filter(id=profile.id).exists():
return generic_message(request, _('Banned from joining'),
_('You have been declared persona non grata for this contest. '
'You are permanently barred from joining this contest.'))
if not contest.require_registration:
return generic_message(request, _('Cannot register'),
_('Registration is not required for this contest.'))
if not contest.can_register:
return generic_message(request, _('Cannot register'),
_('You cannot register for this contest now.'))
requires_access_code = (not self.can_edit and contest.access_code and access_code != contest.access_code)
if contest.ended:
return generic_message(request, _('Contest has ended'),
_('"%s" has ended.') % contest.name)
else:
if self.is_editor or self.is_tester:
return generic_message(request, _('Cannot register'),
_('You cannot register for this contest.'))
try:
ContestParticipation.objects.get(
contest=contest, user=profile, virtual=0,
)
except ContestParticipation.DoesNotExist:
if requires_access_code:
raise ContestAccessDenied()
ContestParticipation.objects.create(
contest=contest, user=profile, virtual=0,
real_start=datetime(1970, 1, 1, tzinfo=timezone.utc),
)
else:
return generic_message(request, _('Already registered'),
_('You have already registered for this contest.'))
contest._updating_stats_only = True
contest.update_user_count()
return HttpResponseRedirect(reverse('contest_view', args=(contest.key,)))
def ask_for_access_code(self, form=None):
contest = self.object
wrong_code = False
if form:
if form.is_valid():
if form.cleaned_data['access_code'] == contest.access_code:
return self.register_contest(self.request, form.cleaned_data['access_code'])
wrong_code = True
else:
form = ContestAccessCodeForm()
return render(self.request, 'contest/access_code.html', {
'form': form, 'wrong_code': wrong_code,
'title': _('Enter access code for "%s"') % contest.name,
})
class ContestJoin(LoginRequiredMixin, ContestMixin, SingleObjectMixin, View):
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return self.ask_for_access_code()
def post(self, request, *args, **kwargs):
self.object = self.get_object()
try:
return self.join_contest(request)
except ContestAccessDenied:
if request.POST.get('access_code'):
return self.ask_for_access_code(ContestAccessCodeForm(request.POST))
else:
return HttpResponseRedirect(request.path)
def join_contest(self, request, access_code=None):
contest = self.object
if not contest.can_join and not (self.is_editor or self.is_tester):
return generic_message(request, _('Contest not ongoing'),
_('"%s" is not currently ongoing.') % contest.name)
profile = request.profile
if not request.user.is_superuser and contest.banned_users.filter(id=profile.id).exists():
return generic_message(request, _('Banned from joining'),
_('You have been declared persona non grata for this contest. '
'You are permanently barred from joining this contest.'))
# Conditions for joining a contest:
# - If contest has ended, allow virtual joining iff:
# - contest.disallow_virtual is False
# - requires_access_code is False
# - If contest is ongoing, allow joining iff:
# - Not editor or tester
# - Registered if registration windows has ended
# - requires_access_code is False
# - Editors/Testers can only spectate live contests and only when requires_access_code is False.
requires_access_code = (not self.can_edit and contest.access_code and access_code != contest.access_code)
if contest.ended:
if contest.disallow_virtual:
return generic_message(request, _('Virtual joining not allowed'),
_('Virtual joining is not allowed for this contest.'))
if requires_access_code:
raise ContestAccessDenied()
while True:
virtual_id = max((ContestParticipation.objects.filter(contest=contest, user=profile)
.aggregate(virtual_id=Max('virtual'))['virtual_id'] or 0) + 1, 1)
try:
participation = ContestParticipation.objects.create(
contest=contest, user=profile, virtual=virtual_id,
real_start=timezone.now(),
)
# There is obviously a race condition here, so we keep trying until we win the race.
except IntegrityError:
pass
else:
break
else:
SPECTATE = ContestParticipation.SPECTATE
LIVE = ContestParticipation.LIVE
can_only_spectate = self.is_editor or self.is_tester
try:
participation = ContestParticipation.objects.get(
contest=contest, user=profile, virtual=(SPECTATE if can_only_spectate else LIVE),
)
except ContestParticipation.DoesNotExist:
if contest.require_registration and not contest.can_register and not can_only_spectate:
return generic_message(request, _('Not registered'),
_('You are not registered for this contest.'))
if requires_access_code:
raise ContestAccessDenied()
participation = ContestParticipation.objects.create(
contest=contest, user=profile, virtual=(SPECTATE if can_only_spectate else LIVE),
real_start=timezone.now(),
)
else:
if participation.pre_registered:
# Pre-registered. First time joining.
participation.real_start = timezone.now()
participation.save()
if participation.ended:
participation = ContestParticipation.objects.get_or_create(
contest=contest, user=profile, virtual=SPECTATE,
defaults={'real_start': timezone.now()},
)[0]
profile.current_contest = participation
profile.save()
contest._updating_stats_only = True
contest.update_user_count()
return HttpResponseRedirect(reverse('contest_view', args=(contest.key,)))
def ask_for_access_code(self, form=None):
contest = self.object
wrong_code = False
if form:
if form.is_valid():
if form.cleaned_data['access_code'] == contest.access_code:
return self.join_contest(self.request, form.cleaned_data['access_code'])
wrong_code = True
else:
form = ContestAccessCodeForm()
return render(self.request, 'contest/access_code.html', {
'form': form, 'wrong_code': wrong_code,
'title': _('Enter access code for "%s"') % contest.name,
})
class ContestLeave(LoginRequiredMixin, ContestMixin, SingleObjectMixin, View):
def dispatch(self, request, *args, **kwargs):
if request.method != 'POST':
return HttpResponseForbidden()
return super(ContestLeave, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
contest = self.get_object()
profile = request.profile
if profile.current_contest is None or profile.current_contest.contest_id != contest.id:
return generic_message(request, _('No such contest'),
_('You are not in contest "%s".') % contest.key, 404)
profile.remove_contest()
return HttpResponseRedirect(reverse('contest_view', args=(contest.key,)))
ContestDay = namedtuple('ContestDay', 'date is_pad is_today starts ends oneday')
class ContestCalendar(TitleMixin, ContestListMixin, TemplateView):
firstweekday = SUNDAY
template_name = 'contest/calendar.html'
def get(self, request, *args, **kwargs):
try:
self.year = int(kwargs['year'])
self.month = int(kwargs['month'])
except (KeyError, ValueError):
raise ImproperlyConfigured('ContestCalendar requires integer year and month')
self.today = timezone.now().date()
return self.render()
def render(self):
context = self.get_context_data()
return self.render_to_response(context)
def get_contest_data(self, start, end):
end += timedelta(days=1)
contests = self.get_queryset().filter(Q(start_time__gte=start, start_time__lt=end) |
Q(end_time__gte=start, end_time__lt=end))
starts, ends, oneday = (defaultdict(list) for i in range(3))
for contest in contests:
start_date = timezone.localtime(contest.start_time).date()
end_date = timezone.localtime(contest.end_time - timedelta(seconds=1)).date()
if start_date == end_date:
oneday[start_date].append(contest)
else:
starts[start_date].append(contest)
ends[end_date].append(contest)
return starts, ends, oneday
def get_table(self):
calendar = Calendar(self.firstweekday).monthdatescalendar(self.year, self.month)
starts, ends, oneday = self.get_contest_data(make_aware(datetime.combine(calendar[0][0], time.min)),
make_aware(datetime.combine(calendar[-1][-1], time.min)))
return [[ContestDay(
date=date, is_pad=date.month != self.month,
is_today=date == self.today, starts=starts[date], ends=ends[date], oneday=oneday[date],
) for date in week] for week in calendar]
def get_context_data(self, **kwargs):
context = super(ContestCalendar, self).get_context_data(**kwargs)
try:
month = date(self.year, self.month, 1)
except ValueError:
raise Http404()
else:
context['title'] = _('Contests in %(month)s') % {'month': date_filter(month, _('F Y'))}
dates = Contest.objects.aggregate(min=Min('start_time'), max=Max('end_time'))
min_month = (self.today.year, self.today.month)
if dates['min'] is not None:
min_month = dates['min'].year, dates['min'].month
max_month = (self.today.year, self.today.month)
if dates['max'] is not None:
max_month = max((dates['max'].year, dates['max'].month), (self.today.year, self.today.month))
month = (self.year, self.month)
if month < min_month or month > max_month:
# 404 is valid because it merely declares the lack of existence, without any reason
raise Http404()
context['now'] = timezone.now()
context['calendar'] = self.get_table()
context['curr_month'] = date(self.year, self.month, 1)
if month > min_month:
context['prev_month'] = date(self.year - (self.month == 1), 12 if self.month == 1 else self.month - 1, 1)
else:
context['prev_month'] = None
if month < max_month:
context['next_month'] = date(self.year + (self.month == 12), 1 if self.month == 12 else self.month + 1, 1)
else:
context['next_month'] = None
return context
class ContestICal(TitleMixin, ContestListMixin, BaseListView):
def generate_ical(self):
cal = ICalendar()
cal.add('prodid', '-//DMOJ//NONSGML Contests Calendar//')
cal.add('version', '2.0')
now = timezone.now().astimezone(timezone.utc)
domain = self.request.get_host()
for contest in self.get_queryset():
event = Event()
event.add('uid', f'contest-{contest.key}@{domain}')
event.add('summary', contest.name)
event.add('location', self.request.build_absolute_uri(contest.get_absolute_url()))
event.add('dtstart', contest.start_time.astimezone(timezone.utc))
event.add('dtend', contest.end_time.astimezone(timezone.utc))
event.add('dtstamp', now)
cal.add_component(event)
return cal.to_ical()
def render_to_response(self, context, **kwargs):
return HttpResponse(self.generate_ical(), content_type='text/calendar')
class ContestStats(TitleMixin, ContestMixin, DetailView):
template_name = 'contest/stats.html'
def get_title(self):
return _('%s Statistics') % self.object.name
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if not self.object.can_see_full_submission_list(self.request.user):
raise Http404()
queryset = Submission.objects.filter(contest_object=self.object, date__gt=self.object.start_time)
ac_count = Count(Case(When(result='AC', then=Value(1)), output_field=IntegerField()))
ac_rate = CombinedExpression(ac_count / Count('problem'), '*', Value(100.0), output_field=FloatField())
status_count_queryset = list(
queryset.values('problem__code', 'result').annotate(count=Count('result'))
.values_list('problem__code', 'result', 'count'),
)
labels, codes = [], []
contest_problems = self.object.contest_problems.order_by('order').values_list('problem__name', 'problem__code')
if contest_problems:
labels, codes = zip(*contest_problems)
num_problems = len(labels)
status_counts = [[] for i in range(num_problems)]
for problem_code, result, count in status_count_queryset:
if problem_code in codes:
status_counts[codes.index(problem_code)].append((result, count))
result_data = defaultdict(partial(list, [0] * num_problems))
for i in range(num_problems):
for category in _get_result_data(defaultdict(int, status_counts[i]))['categories']:
result_data[category['code']][i] = category['count']
language_id_to_name = {id: name for id, name in Language.objects.values_list('id', 'name')}
def id_to_name(data):
return (language_id_to_name[data[0]], data[1])
stats = {
'problem_status_count': get_stacked_bar_chart(
labels, result_data, settings.DMOJ_STATS_SUBMISSION_RESULT_COLORS,
),
'problem_ac_rate': get_bar_chart(
queryset.values('contest__problem__order', 'problem__name').annotate(ac_rate=ac_rate)
.order_by('contest__problem__order').values_list('problem__name', 'ac_rate'),
),
'language_count': get_pie_chart(
list(map(id_to_name, queryset.values('language_id').annotate(count=Count('language_id'))
.filter(count__gt=0).order_by('-count').values_list('language_id', 'count'))),
),
'language_ac_rate': get_bar_chart(
list(map(id_to_name, queryset.values('language_id').annotate(ac_rate=ac_rate)
.filter(ac_rate__gt=0).values_list('language_id', 'ac_rate'))),
),
}
context['stats'] = mark_safe(json.dumps(stats))
return context
ContestRankingProfile = namedtuple(
'ContestRankingProfile',
'id user css_class username points cumtime tiebreaker organization participation '
'participation_rating problem_cells result_cell virtual display_name',
)
BestSolutionData = namedtuple('BestSolutionData', 'code points time state is_pretested')
def make_contest_ranking_profile(contest, participation, contest_problems, first_solves, frozen=False):
def display_user_problem(contest_problem):
# When the contest format is changed, `format_data` might be invalid.
# This will cause `display_user_problem` to error, so we display '???' instead.
try:
return contest.format.display_user_problem(participation, contest_problem, first_solves, frozen)
except (KeyError, TypeError, ValueError):
return mark_safe('<td>???</td>')
user = participation.user
return ContestRankingProfile(
id=user.id,
user=user.user,
css_class=user.css_class,
username=user.username,
points=participation.score if not frozen else participation.frozen_score,
cumtime=participation.cumtime if not frozen else participation.frozen_cumtime,
tiebreaker=participation.tiebreaker if not frozen else participation.frozen_tiebreaker,
organization=user.organization,
participation_rating=participation.rating.rating if hasattr(participation, 'rating') else None,
problem_cells=[display_user_problem(contest_problem) for contest_problem in contest_problems],
result_cell=contest.format.display_participation_result(participation, frozen),
participation=participation,
virtual=participation.virtual,
display_name=user.display_name,
)
def base_contest_ranking_list(contest, problems, queryset, frozen=False):
queryset = queryset.select_related('user__user', 'rating').defer('user__about', 'user__organizations__about')
first_solves, total_ac = contest.format.get_first_solves_and_total_ac(problems, queryset, frozen)
users = [make_contest_ranking_profile(contest, participation, problems, first_solves, frozen) for participation
in queryset]
return users, total_ac
def base_contest_ranking_queryset(contest):
return contest.users.filter(virtual__gt=ContestParticipation.SPECTATE) \
.prefetch_related(Prefetch('user__organizations',
queryset=Organization.objects.filter(is_unlisted=False))) \
.annotate(submission_count=Count('submission')) \
.order_by('is_disqualified', '-score', 'cumtime', 'tiebreaker', '-submission_count')
def base_contest_frozen_ranking_queryset(contest):
return contest.users.filter(virtual__gt=ContestParticipation.SPECTATE) \
.prefetch_related(Prefetch('user__organizations',
queryset=Organization.objects.filter(is_unlisted=False))) \
.annotate(submission_count=Count('submission')) \
.order_by('is_disqualified', '-frozen_score', 'frozen_cumtime', 'frozen_tiebreaker', '-submission_count')
def contest_ranking_list(contest, problems, frozen=False):
return base_contest_ranking_list(contest, problems, base_contest_ranking_queryset(contest), frozen=frozen)
def get_contest_ranking_list(request, contest, participation=None, ranking_list=contest_ranking_list, ranker=ranker):
problems = list(contest.contest_problems.select_related('problem').defer('problem__description').order_by('order'))
users, total_ac = ranking_list(contest, problems)
users = ranker(users, key=attrgetter('points', 'cumtime', 'tiebreaker'))
return users, problems, total_ac
class ContestRankingBase(ContestMixin, TitleMixin, DetailView):
template_name = 'contest/ranking.html'
ranking_table_template = get_template('contest/ranking-table.html')
tab = None
def get_title(self):
raise NotImplementedError()
def get_content_title(self):
return self.object.name
def get_ranking_list(self):
raise NotImplementedError()
@property
def is_frozen(self):
return False
def check_can_see_own_scoreboard(self):
if not self.object.can_see_own_scoreboard(self.request.user):
raise Http404()
def get_rendered_ranking_table(self):
users, problems, total_ac = self.get_ranking_list()
return self.ranking_table_template.render(request=self.request, context={
'table_id': 'ranking-table',
'users': users,
'problems': problems,
'total_ac': total_ac,
'contest': self.object,
'has_rating': self.object.ratings.exists(),
'is_frozen': self.is_frozen,
'perms': PermWrapper(self.request.user),
'can_edit': self.can_edit,
'is_ICPC_format': (self.object.format.name == ICPCContestFormat.name),
})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
self.check_can_see_own_scoreboard()
context['rendered_ranking_table'] = self.get_rendered_ranking_table()
context['tab'] = self.tab
return context
def get(self, request, *args, **kwargs):
if 'raw' in request.GET:
self.object = self.get_object()
self.check_can_see_own_scoreboard()
return HttpResponse(self.get_rendered_ranking_table(), content_type='text/plain')
return super().get(request, *args, **kwargs)
class ContestRanking(ContestRankingBase):
tab = 'ranking'
show_virtual = False
def get_title(self):
return _('%s Rankings') % self.object.name
@cached_property
def is_frozen(self):
return self.object.is_frozen and not self.can_edit
@property
def cache_key(self):
return f'contest_ranking_cache_{self.object.key}_{self.show_virtual}_{self.is_frozen}_' \
f'{self.request.LANGUAGE_CODE}'
@property
def bypass_cache_ranking(self):
return self.object.scoreboard_cache_timeout == 0 or self.can_edit or \
(self.request.user.is_authenticated and not self.object.can_see_full_scoreboard(self.request.user))
def get_ranking_queryset(self):
if self.is_frozen:
queryset = base_contest_frozen_ranking_queryset(self.object)
else:
queryset = base_contest_ranking_queryset(self.object)
if not self.show_virtual: