def process_view(self, request, view_func, view_args, view_kwargs): contest = None if getattr(settings, 'ONLY_DEFAULT_CONTEST', False): if not hasattr(settings, 'DEFAULT_CONTEST'): raise ImproperlyConfigured("ONLY_DEFAULT_CONTEST set, but no " "DEFAULT_CONTEST in settings") try: contest = Contest.objects.get(id=settings.DEFAULT_CONTEST) except Contest.DoesNotExist: raise ImproperlyConfigured("ONLY_DEFAULT_CONTEST set, but " "DEFAULT_CONTEST (%s) does not exist" % (settings.DEFAULT_CONTEST,)) if view_kwargs.get('contest_id', contest.id) != contest.id: raise Http404 if not contest and 'contest_id' in view_kwargs: contest = get_object_or_404(Contest, id=view_kwargs['contest_id']) if not contest and 'contest_id' in request.session: contest = self._get_contest(request.session['contest_id']) if not contest and getattr(settings, 'DEFAULT_CONTEST', None): contest = self._get_contest(getattr(settings, 'DEFAULT_CONTEST')) if not contest: contests = visible_contests(request) if contests: contest = contests[0] activate_contest(request, contest)
def contest_selection(context): request = context['request'] to_show = getattr(settings, 'NUM_RECENT_CONTESTS', 5) rcontests = recent_contests(request) contests = list(visible_contests(request).difference(rcontests)) contests.sort(key=lambda x: x.creation_date, reverse=True) contests = (rcontests + contests)[:to_show+1] default_contest = None if rcontests: default_contest = rcontests[0] elif contests: default_contest = contests[0] contest_selection_context = { 'contests': contests[:to_show], 'default_contest': default_contest, 'more_contests': len(contests) > to_show, 'is_teacher': request.user.has_perm('teachers.teacher'), 'is_inactive_teacher': request.user.is_authenticated and bool(Teacher.objects.filter(user=request.user, is_active=False)) } return RequestContext(request, contest_selection_context)
def contest_selection(context): request = context['request'] to_show = getattr(settings, 'NUM_RECENT_CONTESTS', 5) rcontests = recent_contests(request) contests = list(visible_contests(request).difference(rcontests)) contests.sort(key=lambda x: x.creation_date, reverse=True) contests = (rcontests + contests)[:to_show + 1] default_contest = None if rcontests: default_contest = rcontests[0] elif contests: default_contest = contests[0] contest_selection_context = { 'contests': contests[:to_show], 'default_contest': default_contest, 'more_contests': len(contests) > to_show, 'is_teacher': request.user.has_perm('teachers.teacher'), 'is_inactive_teacher': request.user.is_authenticated and bool(Teacher.objects.filter(user=request.user, is_active=False)) } return RequestContext(request, contest_selection_context)
def process_view(self, request, view_func, view_args, view_kwargs): contest = None if getattr(settings, 'ONLY_DEFAULT_CONTEST', False): if not hasattr(settings, 'DEFAULT_CONTEST'): raise ImproperlyConfigured("ONLY_DEFAULT_CONTEST set, but no " "DEFAULT_CONTEST in settings") try: contest = Contest.objects.get(id=settings.DEFAULT_CONTEST) except Contest.DoesNotExist: raise ImproperlyConfigured( "ONLY_DEFAULT_CONTEST set, but " "DEFAULT_CONTEST (%s) does not exist" % (settings.DEFAULT_CONTEST, )) if view_kwargs.get('contest_id', contest.id) != contest.id: raise Http404 if not contest and 'contest_id' in view_kwargs: contest = get_object_or_404(Contest, id=view_kwargs['contest_id']) if not contest and 'contest_id' in request.session: contest = self._get_contest(request.session['contest_id']) if not contest and getattr(settings, 'DEFAULT_CONTEST', None): contest = self._get_contest(getattr(settings, 'DEFAULT_CONTEST')) if not contest: contests = visible_contests(request) if contests: contest = contests[0] activate_contest(request, contest)
def render(self, request, m): rcontests = recent_contests(request) contests = list(visible_contests(request).difference(rcontests)) contests.sort(key=lambda x: x.creation_date, reverse=True) contests = (rcontests + contests)[:self.TO_SHOW + 1] default_contest = None if rcontests: default_contest = rcontests[0] elif contests: default_contest = contests[0] context = { 'contests': contests[:self.TO_SHOW], 'default_contest': default_contest, 'more_contests': len(contests) > self.TO_SHOW, 'is_teacher': request.user.has_perm('teachers.teacher'), 'is_inactive_teacher': request.user.is_authenticated() and bool( Teacher.objects.filter(user=request.user, is_active=False)) } return render_to_string('teachers/widgets/contest-selection.html', RequestContext(request, context))
def main_page_view(request): navbar_links = navbar_links_registry.template_context(request) to_show = getattr(settings, 'NUM_RECENT_CONTESTS', 7) rcontests = recent_contests(request) contests = list(visible_contests(request).difference(rcontests)) contests.sort(key=lambda x: x.creation_date, reverse=True) contests = (rcontests + contests)[:to_show] submissions = [] show_scores = False if request.user.is_authenticated: queryset = Submission.objects.filter( user=request.user).order_by('-date') # current_contest = request.contest # # for s in queryset: # request.contest = "lakdnasdn"#s.problem_instance.contest # submissions.append(submission_template_context(request, s)) # # request.contest = current_contest to_show = getattr(settings, 'NUM_PANEL_SUBMISSIONS', 7) # limit queryset size, because filtering all submissions is slow queryset = queryset[:to_show] limit_queryset_ids = [submission.id for submission in queryset] queryset = Submission.objects.filter( id__in=limit_queryset_ids).select_related( 'user', 'problem_instance', 'problem_instance__contest', 'problem_instance__round', 'problem_instance__problem', ) submissions_list = filter_my_all_visible_submissions(request, queryset) if django.VERSION >= (1, 11): submissions_list = submissions_list.order_by('-date') else: submissions_list.sort(reverse=True, key=lambda s: s.date) submissions = [ submission_template_context(request, s) for s in submissions_list ] show_scores = any(s['can_see_score'] for s in submissions) context = { 'navbar_links': navbar_links, 'contests': contests, 'submissions': submissions, 'show_scores': show_scores, } return TemplateResponse(request, 'main-page.html', context)
def recent_contests(request): if request.real_user.is_anonymous: ids = request.session.get('recent_contests', []) mapping = Contest.objects.in_bulk(ids) return [c for c in (mapping.get(id) for id in ids) if c is not None and c != request.contest] else: c_views = ContestView.objects.filter(user=request.real_user) \ .select_related('contest') c_views = c_views[:getattr(settings, 'NUM_RECENT_CONTESTS', 5)] return [cv.contest for cv in c_views if cv.contest in visible_contests(request)]
def recent_contests(request): if request.real_user.is_anonymous(): ids = request.session.get('recent_contests', []) mapping = Contest.objects.in_bulk(ids) return [c for c in (mapping.get(id) for id in ids) if c is not None and c != request.contest] else: c_views = ContestView.objects.filter(user=request.real_user) \ .select_related('contest') c_views = c_views[:getattr(settings, 'NUM_RECENT_CONTESTS', 5)] return [cv.contest for cv in c_views if cv.contest in visible_contests(request)]
def process_view(self, request, view_func, view_args, view_kwargs): contest = None if not contest and 'contest_id' in view_kwargs: contest = get_object_or_404(Contest, id=view_kwargs['contest_id']) if not contest and 'contest_id' in request.session: contest = self._get_contest(request.session['contest_id']) if not contest and getattr(settings, 'DEFAULT_CONTEST', None): contest = self._get_contest(getattr(settings, 'DEFAULT_CONTEST')) if not contest: contests = visible_contests(request) if contests: contest = contests[0] activate_contest(request, contest)
def main_page_view(request): navbar_links = navbar_links_registry.template_context(request) to_show = getattr(settings, 'NUM_RECENT_CONTESTS', 7) rcontests = recent_contests(request) contests = list(visible_contests(request).difference(rcontests)) contests.sort(key=lambda x: x.creation_date, reverse=True) contests = (rcontests + contests)[:to_show] submissions = [] show_scores = False if request.user.is_authenticated: queryset = Submission.objects \ .filter(user=request.user) \ .order_by('-date') \ .select_related('user', 'problem_instance', 'problem_instance__contest', 'problem_instance__round', 'problem_instance__problem') # current_contest = request.contest # # for s in queryset: # request.contest = "lakdnasdn"#s.problem_instance.contest # submissions.append(submission_template_context(request, s)) # # request.contest = current_contest submissions = [ submission_template_context(request, s) for s in queryset ] to_show = getattr(settings, 'NUM_PANEL_SUBMISSIONS', 7) submissions = submissions[:to_show] show_scores = any(s['can_see_score'] for s in submissions) context = { 'navbar_links': navbar_links, 'contests': contests, 'submissions': submissions, 'show_scores': show_scores, } return TemplateResponse(request, 'main-page.html', context)
def render(self, request, m): contests = list(visible_contests(request)[:self.TO_SHOW]) rcontests = recent_contests(request) contests = rcontests + [c for c in contests if c not in rcontests] default_contest = None if rcontests: default_contest = rcontests[0] elif contests: default_contest = contests[0] context = { 'contests': contests[:self.TO_SHOW-1], 'default_contest': default_contest, 'more_contests': len(contests) > self.TO_SHOW-1, 'is_teacher': request.user.has_perm('teachers.teacher'), 'is_inactive_teacher': request.user.is_authenticated() and bool(Teacher.objects.filter(user=request.user, is_active=False)), } return render_to_string('portals/widgets/contest_selection.html', RequestContext(request, context))
def render(self, request, m): rcontests = recent_contests(request) contests = list(visible_contests(request).difference(rcontests)) contests.sort(key=lambda x: x.creation_date, reverse=True) contests = (rcontests + contests)[:self.TO_SHOW+1] default_contest = None if rcontests: default_contest = rcontests[0] elif contests: default_contest = contests[0] context = { 'contests': contests[:self.TO_SHOW], 'default_contest': default_contest, 'more_contests': len(contests) > self.TO_SHOW, 'is_teacher': request.user.has_perm('teachers.teacher'), 'is_inactive_teacher': request.user.is_authenticated() and bool(Teacher.objects.filter(user=request.user, is_active=False)) } return render_to_string('portals/widgets/contest_selection.html', RequestContext(request, context))
def teacher_dashboard_view(request): contest_context = [] min_date = datetime.today() - timedelta(days=7) contests = [contest for contest in visible_contests(request)] are_contests_limited = len(contests) > MAX_CONTESTS_ON_PAGE visible_contests_count = len(contests) contests = [ x for x in contests if is_teachers(x) and can_admin_contest(request.user, x) ] if len(contests) < visible_contests_count: are_contests_limited = True contests.sort(key=lambda x: x.creation_date, reverse=True) contests = contests[:MAX_CONTESTS_ON_PAGE] if 'oioioi.portals' in settings.INSTALLED_APPS: has_portal = global_portal_exists(request) else: has_portal = False for contest in contests: scores = [ result.score.to_int() for result in UserResultForContest.objects.filter( contest=contest).all() if result.score is not None ] max_score = 0 for problem_inst in ProblemInstance.objects.filter(contest=contest): user_results = \ UserResultForProblem.objects.filter( problem_instance=problem_inst, submission_report__isnull=False) if user_results.count() > 0: for result in user_results: score_report = result.submission_report.score_report if score_report_is_valid(score_report): max_score += score_report.max_score.to_int() break contest_dict = { 'id': contest.id, 'name': contest.name, 'round_count': Round.objects.filter(contest=contest).count(), 'task_count': ProblemInstance.objects.filter(contest=contest).count(), 'user_count': User.objects.filter(participant__contest=contest).count(), 'submission_count': Submission.objects.filter( problem_instance__contest=contest).count(), 'recent_submission_count': Submission.objects.filter(problem_instance__contest=contest, date__gte=min_date).count(), 'recent_question_count': Message.objects.filter(contest=contest, kind='QUESTION', date__gte=min_date).count(), 'max_score': max_score, 'scores': scores, } contest_context.append(contest_dict) context = { 'contests': contest_context, 'are_contests_limited': are_contests_limited, 'has_portal': has_portal } if has_portal: context['portal_path'] = Portal.objects.filter(owner=None)[0] \ .root.get_path() return TemplateResponse(request, 'simpleui/main_dashboard/dashboard.html', context)
def select_contest_view(request): contests = visible_contests(request) return TemplateResponse(request, "contests/select_contest.html", {"contests": contests})
def select_contest_view(request): contests = visible_contests(request) return TemplateResponse(request, 'contests/select_contest.html', {'contests': contests})
def select_contest_view(request): contests = visible_contests(request) contests = sorted(contests, key=lambda x: x.creation_date, reverse=True) return TemplateResponse(request, 'contests/select_contest.html', {'contests': contests})
def process_request(self, request): contest = None contest_id = None m = contest_re.match(request.path) if m is not None: contest_id = m.group(1) contest = get_object_or_404(Contest, id=contest_id) activate_contest(request, contest) if contest or settings.CONTEST_MODE == ContestMode.neutral: return # There was no contest, but CONTEST_MODE tells us we need # to try to put the user into a contest. recent_contests = request.session.get('recent_contests', []) if recent_contests: contest = self._get_contest(recent_contests[0]) if not contest and getattr(settings, 'DEFAULT_CONTEST', None): contest = self._get_contest(getattr(settings, 'DEFAULT_CONTEST')) if not contest: visible = visible_contests(request) if visible: # Get most recent visible contest contest = max(visible, key=lambda c: c.creation_date) if not contest: return # We found a contest. We will try to regenerate our URL, # this time using our contest's id. try: res = resolve(request.path) except Http404: # We still allow the request to continue, because # it could be a static URL. return if not res.url_name: return nonglobal = False if res.namespaces and res.namespaces[0] == 'noncontest': # It certainly isn't a global url because it has # a noncontest version. It could still be a neutral URL. nonglobal = True res.namespaces = res.namespaces[1:] assert not 'contest_id' in res.kwargs res.kwargs['contest_id'] = contest.id # If there is a contest-prefixed version of this url, # reverse will return it. assert not res.namespaces or res.namespaces[0] != 'contest' name = res.url_name if res.namespaces: name = ':'.join(res.namespaces + [name]) try: new_path = reverse(name, args=res.args, kwargs=res.kwargs) assert contest_re.match(new_path) if request.GET: new_path += '?' + request.GET.urlencode() return HttpResponseRedirect(new_path) except NoReverseMatch: if nonglobal: # That wasn't a global url and it doesn't have # a contest version. It must be a noncontest-only url. if settings.CONTEST_MODE == ContestMode.contest_only \ and not request.user.is_superuser: raise PermissionDenied
def teacher_dashboard_view(request): contest_context = [] min_date = datetime.today() - timedelta(days=7) contests = [contest for contest in visible_contests(request)] are_contests_limited = len(contests) > MAX_CONTESTS_ON_PAGE visible_contests_count = len(contests) contests = [x for x in contests if is_teachers(x) and can_admin_contest(request.user, x)] if len(contests) < visible_contests_count: are_contests_limited = True contests.sort(key=lambda x: x.creation_date, reverse=True) contests = contests[:MAX_CONTESTS_ON_PAGE] if 'oioioi.portals' in settings.INSTALLED_APPS: has_portal = main_page_from_default_global_portal(request) else: has_portal = False for contest in contests: scores = [result.score.to_int() for result in UserResultForContest.objects.filter(contest=contest).all() if result.score is not None] max_score = 0 for problem_inst in ProblemInstance.objects.filter(contest=contest): user_results = \ UserResultForProblem.objects.filter( problem_instance=problem_inst, submission_report__isnull=False) if user_results.count() > 0: for result in user_results: score_report = result.submission_report.score_report if score_report_is_valid(score_report): max_score += score_report.max_score.to_int() break contest_dict = { 'id': contest.id, 'name': contest.name, 'round_count': Round.objects.filter(contest=contest).count(), 'task_count': ProblemInstance.objects.filter( contest=contest).count(), 'user_count': User.objects.filter( participant__contest=contest).count(), 'submission_count': Submission.objects.filter( problem_instance__contest=contest).count(), 'recent_submission_count': Submission.objects.filter( problem_instance__contest=contest, date__gte=min_date ).count(), 'recent_question_count': Message.objects.filter( contest=contest, kind='QUESTION', date__gte=min_date ).count(), 'max_score': max_score, 'scores': scores, } contest_context.append(contest_dict) context = { 'contests': contest_context, 'are_contests_limited': are_contests_limited, 'has_portal': has_portal } if has_portal: context['portal_path'] = Portal.objects.filter(owner=None, link_name='default')[0]\ .root.get_path() return TemplateResponse(request, 'simpleui/main_dashboard/dashboard.html', context)
def select_contest_view(request): contests = visible_contests(request) contests = sorted(contests, key=lambda x: x.creation_date, reverse=True) return TemplateResponse( request, 'contests/select_contest.html', {'contests': contests} )
def user_dashboard_view(request): contest_context = [] min_date = datetime.today() - timedelta(days=7) contests = [contest for contest in visible_contests(request)] are_contests_limited = len(contests) > MAX_CONTESTS_ON_PAGE visible_contests_count = len(contests) contests = [x for x in contests if can_admin_contest(request.user, x)] if len(contests) < visible_contests_count: are_contests_limited = True contests.sort(key=lambda x: x.creation_date, reverse=True) contests = contests[:MAX_CONTESTS_ON_PAGE] if 'oioioi.portals' in settings.INSTALLED_APPS: has_portal = main_page_from_default_global_portal(request) else: has_portal = False for contest in contests: scores = [ result.score.to_int() for result in UserResultForContest.objects.filter( contest=contest).all() if result.score is not None ] max_score = 0 for problem_inst in ProblemInstance.objects.filter(contest=contest): user_results = UserResultForProblem.objects.filter( problem_instance=problem_inst, submission_report__isnull=False) if user_results.count() > 0: for result in user_results: score_report = result.submission_report.score_report if score_report_is_valid(score_report): max_score += score_report.max_score.to_int() break contest_dict = { 'id': contest.id, 'name': contest.name, 'round_count': Round.objects.filter(contest=contest).count(), 'task_count': ProblemInstance.objects.filter(contest=contest).count(), 'submission_count': Submission.objects.filter( problem_instance__contest=contest).count(), 'recent_submission_count': Submission.objects.filter(problem_instance__contest=contest, date__gte=min_date).count(), 'recent_question_count': Message.objects.filter(contest=contest, kind='QUESTION', date__gte=min_date).count(), 'max_score': max_score, 'scores': scores, 'contest_controller': contest.controller, 'dashboard_url': reverse('simpleui_contest_dashboard', kwargs={'contest_id': contest.id}), 'public_contest': isinstance( contest.controller.registration_controller(), PublicContestRegistrationController, ), } if not contest_dict['public_contest']: contest_dict['user_count'] = ( contest.controller.registration_controller( ).filter_participants(User.objects.all()).count()) contest_context.append(contest_dict) context = { 'contests': contest_context, 'are_contests_limited': are_contests_limited, 'has_portal': has_portal, 'can_create_contest': can_create_contest(request), } if has_portal: context['portal_path'] = Portal.objects.filter( owner=None, link_name='default')[0].root.get_path() return TemplateResponse(request, 'simpleui/main_dashboard/dashboard.html', context)