def admin_time(request, next_page=None): if 'next' in request.REQUEST: next_page = request.REQUEST['next'] if request.method == 'POST': if 'reset-button' in request.POST: if 'admin_time' in request.session: del request.session['admin_time'] return safe_redirect(request, next_page) elif is_real_superuser(request): admin_time = re.findall(r'\d+', request.POST['admin-time']) admin_time = map(int, admin_time) try: admin_time = datetime(*admin_time) except (ValueError, TypeError, OverflowError): messages.error(request, _("Invalid date. Admin-time was not set.")) return safe_redirect(request, next_page) if admin_time.year >= 1900: request.session['admin_time'] = \ timezone.localtime(timezone.now()). \ tzinfo.localize(admin_time).astimezone(pytz.utc) else: messages.error(request, _("Date has to be after 1900." " Admin-time was not set.")) return safe_redirect(request, next_page) raise Http404
def admin_time(request, next_page=None): if 'next' in request.REQUEST: next_page = request.REQUEST['next'] if request.method == 'POST': if 'reset-button' in request.POST: if 'admin_time' in request.session: del request.session['admin_time'] return safe_redirect(request, next_page) elif is_real_superuser(request): current_admin_time = re.findall(r'\d+', request.POST['admin-time']) current_admin_time = map(int, current_admin_time) try: current_admin_time = datetime(*current_admin_time) except (ValueError, TypeError, OverflowError): messages.error(request, _("Invalid date. Admin-time was not set.")) return safe_redirect(request, next_page) if current_admin_time.year >= 1900: request.session['admin_time'] = \ timezone.localtime(timezone.now()). \ tzinfo.localize(current_admin_time). \ astimezone(pytz.utc).isoformat() else: messages.error( request, _("Date has to be after 1900." " Admin-time was not set.")) return safe_redirect(request, next_page) raise Http404
def view(self, request, contest, existing_problem=None): if not contest: messages.error(request, _("Option not available")) path = request.path if existing_problem: path += '?' + urlencode({'problem': existing_problem.id}) return safe_redirect(request, path) is_reupload = existing_problem is not None if existing_problem: url_key = existing_problem.problemsite.url_key else: # take url_key form form url_key = request.POST.get('url_key', None) if url_key is None: # take url_key from Problemset url_key = request.GET.get('url_key', None) form = ProblemsetSourceForm(url_key) post_data = {'form': form, 'is_reupload': is_reupload} if request.POST: if not Problem.objects.filter(problemsite__url_key=url_key) \ .exists(): messages.error(request, _('Given url key is invalid')) return TemplateResponse(request, "problems/problemset_source.html", post_data) problem = Problem.objects.get(problemsite__url_key=url_key) if existing_problem: assert problem == existing_problem assert 'instance_id' in request.GET pi = problem.probleminstance_set.get(contest=request.contest, id=request.GET['instance_id']) update_tests_from_main_pi(pi) # limits could be changed pi.needs_rejudge = True pi.save() messages.success(request, _("Problem successfully updated")) else: pi = get_new_problem_instance(problem) pi.contest = request.contest pi.short_name = None pi.save() messages.success(request, _("Problem successfully uploaded")) return safe_redirect(request, reverse( 'oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, "problems/problemset_source.html", post_data)
def view(self, request, contest, existing_problem=None): if not contest: messages.error(request, _("Option not available")) path = request.path if existing_problem: path += '?' + urlencode({'problem': existing_problem.id}) return safe_redirect(request, path) is_reupload = existing_problem is not None if existing_problem: url_key = existing_problem.problemsite.url_key else: # take url_key form form url_key = request.POST.get('url_key', None) if url_key is None: # take url_key from Problemset url_key = request.GET.get('url_key', None) form = ProblemsetSourceForm(url_key) post_data = {'form': form, 'is_reupload': is_reupload} if request.POST: if not Problem.objects.filter(problemsite__url_key=url_key) \ .exists(): messages.error(request, _('Given url key is invalid')) return TemplateResponse(request, "problems/problemset_source.html", post_data) problem = Problem.objects.get(problemsite__url_key=url_key) if existing_problem: assert problem == existing_problem assert 'instance_id' in request.GET pi = problem.probleminstance_set.get( contest=contest, id=request.GET['instance_id']) update_tests_from_main_pi(pi) # limits could be changed pi.needs_rejudge = True pi.save() messages.success(request, _("Problem successfully updated")) else: pi = get_new_problem_instance(problem, contest) messages.success(request, _("Problem successfully uploaded")) return safe_redirect( request, reverse('oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, "problems/problemset_source.html", post_data)
def su_reset_view(request, next_page=None, redirect_field_name=REDIRECT_FIELD_NAME): reset_to_real_user(request) if redirect_field_name in request.REQUEST: next_page = request.REQUEST[redirect_field_name] return safe_redirect(request, next_page)
def reattach_problem_confirm_view(request, problem_instance_id, contest_id): contest = get_object_or_404(Contest, id=contest_id) if not can_admin_contest(request.user, contest): raise PermissionDenied problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) if request.method == 'POST': if request.POST.get('copy-limits', '') == 'on': pi = copy_problem_instance(problem_instance, contest) else: pi = get_new_problem_instance(problem_instance.problem, contest) messages.success(request, _(u"Problem {} added successfully.".format(pi))) return safe_redirect( request, reverse( 'oioioiadmin:contests_probleminstance_changelist', kwargs={'contest_id': contest.id}, ), ) return TemplateResponse( request, 'contests/reattach_problem_confirm.html', {'problem_instance': problem_instance, 'destination_contest': contest}, )
def su_view(request, next_page=None, redirect_field_name=REDIRECT_FIELD_NAME): form = SuForm(request.POST) if not form.is_valid(): return TemplateResponse( request, 'simple-centered-form.html', { 'form': form, 'action': reverse('su'), 'title': _("Login as another user"), }, ) user = form.cleaned_data['user'] if is_under_su(request): raise SuspiciousOperation su_to_user(request, user, form.cleaned_data['backend']) if redirect_field_name in request.GET: next_page = request.GET[redirect_field_name] elif redirect_field_name in request.POST: next_page = request.POST[redirect_field_name] request.session[REDIRECTION_AFTER_SU_KEY] = 'PRE_SU' return safe_redirect(request, next_page)
def registration_view(self, request): participant = self._get_participant_for_form(request) form = self.get_form(request, participant) assert form is not None, "can_register or can_edit_registration " \ "returned True, but controller returns no registration form" if request.method == 'POST': if form.is_valid(): participant, created = Participant.objects.get_or_create( contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) can_unregister = False if participant: can_unregister = self.can_unregister(request, participant) context = { 'form': form, 'participant': participant, 'can_unregister': can_unregister, } return TemplateResponse(request, self.registration_template, context)
def registration_view(self, request): participant = self._get_participant_for_form(request) if "oi_oiregistrationformdata" in request.session: # pylint: disable=not-callable form = self.form_class(request.session["oi_oiregistrationformdata"]) del request.session["oi_oiregistrationformdata"] else: form = self.get_form(request, participant) if request.method == "POST": if "_add_school" in request.POST: data = request.POST.copy() data.pop("_add_school", None) data.pop("csrfmiddlewaretoken", None) request.session["oi_oiregistrationformdata"] = data return redirect("add_school") elif form.is_valid(): # pylint: disable=maybe-no-member participant, created = Participant.objects.get_or_create(contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if "next" in request.GET: return safe_redirect(request, request.GET["next"]) else: return redirect("default_contest_view", contest_id=self.contest.id) context = {"form": form, "participant": participant} return TemplateResponse(request, self.registration_template, context)
def registration_view(self, request): participant = self._get_participant_for_form(request) if 'pa_paregistrationformdata' in request.session: # pylint: disable=not-callable form = self.form_class(request.session[ 'pa_paregistrationformdata']) del request.session['pa_paregistrationformdata'] else: form = self.get_form(request, participant) form.set_terms_accepted_text(self.get_terms_accepted_phrase()) if request.method == 'POST': # pylint: disable=maybe-no-member if form.is_valid(): participant, created = Participant.objects \ .get_or_create(contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) context = {'form': form, 'participant': participant} return TemplateResponse(request, self.registration_template, context)
def registration_view(self, request): participant = self._get_participant_for_form(request) if 'oi_oiregistrationformdata' in request.session: form = self.form_class( request.session['oi_oiregistrationformdata']) del request.session['oi_oiregistrationformdata'] else: form = self.get_form(request, participant) if request.method == 'POST': if '_add_school' in request.POST: data = request.POST.copy() data.pop('_add_school', None) data.pop('csrfmiddlewaretoken', None) request.session['oi_oiregistrationformdata'] = data return redirect('add_school') elif form.is_valid(): participant, created = Participant.objects \ .get_or_create(contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) context = {'form': form, 'participant': participant} return TemplateResponse(request, self.registration_template, context)
def registration_view(self, request): participant = self._get_participant_for_form(request) if 'pa_paregistrationformdata' in request.session: # pylint: disable=not-callable form = self.form_class( request.session['pa_paregistrationformdata']) del request.session['pa_paregistrationformdata'] else: form = self.get_form(request, participant) form.set_terms_accepted_text(self.get_terms_accepted_phrase()) if request.method == 'POST': # pylint: disable=maybe-no-member if form.is_valid(): participant, created = Participant.objects \ .get_or_create(contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) auditLogger.info( "User %d (%s) registered in %s from IP %s UA: %s", request.user.id, request.user.username, self.contest.id, request.META.get('REMOTE_ADDR', '?'), request.META.get('HTTP_USER_AGENT', '?')) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) context = {'form': form, 'participant': participant} return TemplateResponse(request, self.registration_template, context)
def registration_view(self, request): try: participant = Participant.objects.get(contest=self.contest, user=request.user) if not self.can_edit_registration(request, participant): raise PermissionDenied except Participant.DoesNotExist: participant = None if participant is None and not self.can_register(request): raise PermissionDenied form = self.get_form(request, participant) if request.method == 'POST': if form.is_valid(): participant, created = Participant.objects.get_or_create( contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) context = {'form': form, 'participant': participant} return TemplateResponse(request, self.registration_template, context)
def registration_view(self, request): participant = self._get_participant_for_form(request) if 'oi_oiregistrationformdata' in request.session: # pylint: disable=not-callable form = self.form_class(request.session[ 'oi_oiregistrationformdata']) del request.session['oi_oiregistrationformdata'] else: form = self.get_form(request, participant) form.set_terms_accepted_text(self.get_terms_accepted_phrase()) if request.method == 'POST': if '_add_school' in request.POST: data = request.POST.copy() data.pop('_add_school', None) data.pop('csrfmiddlewaretoken', None) request.session['oi_oiregistrationformdata'] = data return redirect('add_school') elif form.is_valid(): # pylint: disable=maybe-no-member participant, created = Participant.objects \ .get_or_create(contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) context = {'form': form, 'participant': participant} return TemplateResponse(request, self.registration_template, context)
def rejudge_all_submissions_for_problem_view(request, problem_instance_id): problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) count = problem_instance.submission_set.count() if request.POST: for submission in problem_instance.submission_set.all(): problem_instance.controller.judge(submission, request.GET.dict(), is_rejudge=True) messages.info( request, ungettext_lazy( "%(count)d rejudge request received.", "%(count)d rejudge requests received.", count, ) % {'count': count}, ) problem_instance.needs_rejudge = False problem_instance.save(update_fields=["needs_rejudge"]) return safe_redirect( request, reverse('oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, 'contests/confirm_rejudge.html', {'count': count})
def user_info_redirect_view(request): form = GetUserInfoForm(request, request.POST) if not form.is_valid(): return TemplateResponse( request, 'simple-centered-form.html', { 'form': form, 'action': reverse('user_info_redirect', kwargs={'contest_id': request.contest.id}), 'title': _("See user info page"), }, ) user = form.cleaned_data['user'] return safe_redirect( request, reverse('user_info', kwargs={ 'contest_id': request.contest.id, 'user_id': user.id }), )
def response_change(self, request, obj): if '_continue' in request.POST and '_popup' not in request.REQUEST: return HttpResponseRedirect(request.get_full_path()) if 'came_from' in request.GET and '_continue' not in request.POST \ and '_saveasnew' not in request.POST \ and '_addanother' not in request.POST: return safe_redirect(request, request.GET['came_from']) return super(ModelAdmin, self).response_change(request, obj)
def problemset_add_or_update_problem_view(request): if not can_add_to_problemset(request): if request.contest: url = reverse("add_or_update_problem") + "?" + urllib.urlencode(request.GET.dict()) return safe_redirect(request, url) raise PermissionDenied return add_or_update_problem(request, None, "problems/problemset/add_or_update.html")
def response_change(self, request, obj): not_popup = '_popup' not in request.GET and '_popup' not in request.POST if '_continue' in request.POST and not_popup: return HttpResponseRedirect(request.get_full_path()) if ('came_from' in request.GET and '_continue' not in request.POST and '_saveasnew' not in request.POST and '_addanother' not in request.POST): return safe_redirect(request, request.GET.get('came_from')) return super(ModelAdmin, self).response_change(request, obj)
def response_delete(self, request): opts = self.model._meta if 'came_from' in request.GET: return safe_redirect(request, request.GET['came_from']) if not self.has_change_permission(request): return HttpResponseRedirect(reverse('admin:index', current_app=self.admin_site.name)) return HttpResponseRedirect(reverse('admin:%s_%s_changelist' % (opts.app_label, opts.module_name), current_app=self.admin_site.name))
def problemset_add_or_update_problem_view(request): if not can_add_to_problemset(request): if request.contest: url = reverse('add_or_update_problem') + '?' + \ six.moves.urllib.parse.urlencode(request.GET.dict()) return safe_redirect(request, url) raise PermissionDenied return add_or_update_problem(request, None, 'problems/problemset/add-or-update.html')
def reset_tests_limits_for_probleminstance_view(request, problem_instance_id): problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) if request.POST: update_tests_from_main_pi(problem_instance) messages.success(request, _("Tests limits reset successfully")) return safe_redirect(request, reverse( 'oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, 'contests/confirm_resetting_limits.html', {'probleminstance': problem_instance})
def reset_tests_limits_for_probleminstance_view(request, problem_instance_id): problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) if request.POST: update_tests_from_main_pi(problem_instance) messages.success(request, _("Tests limits resetted successfully")) return safe_redirect(request, reverse( 'oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, 'contests/confirm_resetting_limits.html', {'probleminstance': problem_instance})
def view(self, request, contest, existing_problem=None): is_reupload = existing_problem is not None if is_reupload: # reuploading doesn't make much sense here return _("Reuploading quizzes is not supported") if request.method == "POST": form = EmptyQuizSourceForm(request.POST) else: form = EmptyQuizSourceForm() post_data = {'form': form, 'is_reupload': is_reupload} if request.method == "POST" and form.is_valid(): with transaction.atomic(): controller = self.problem_controller_class quiz = Quiz.objects.create( name=form.cleaned_data['name'], short_name=form.cleaned_data['short_name'], controller_name=controller, author=request.user, ) algorithm_tag, created = AlgorithmTag.objects.get_or_create( name='Quiz') if not created: for language_code, _ in LANGUAGES: AlgorithmTagLocalization.objects.create( algorithm_tag=algorithm_tag, language=language_code, name='Quiz', ) AlgorithmTagThrough.objects.create(problem=quiz, tag=algorithm_tag) ProblemSite.objects.create(problem=quiz, url_key=generate_key()) pi = ProblemInstance.objects.create(problem=quiz, short_name=quiz.short_name) quiz.main_problem_instance = pi quiz.save() if contest: quiz.contest = contest get_new_problem_instance(quiz, contest) messages.success(request, _("Quiz successfully added")) return safe_redirect( request, reverse('oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, "quizzes/emptyquiz-source.html", post_data)
def user_info_redirect_view(request): form = GetUserInfoForm(request, request.POST) if not form.is_valid(): return TemplateResponse(request, 'simple-centered-form.html', { 'form': form, 'action': reverse('user_info_redirect', kwargs={'contest_id': request.contest.id}), 'title': _("See user info page")}) user = form.cleaned_data['user'] return safe_redirect(request, reverse('user_info', kwargs={ 'contest_id': request.contest.id, 'user_id': user.id}))
def rejudge_not_needed_view(request, problem_instance_id): problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) if request.POST: problem_instance.needs_rejudge = False problem_instance.save(update_fields=["needs_rejudge"]) messages.success(request, _("Needs rejudge flag turned off.")) return safe_redirect( request, reverse('oioioiadmin:contests_probleminstance_changelist'), ) return TemplateResponse(request, 'contests/confirm_rejudge_not_needed.html')
def su_view(request, next_page=None, redirect_field_name=REDIRECT_FIELD_NAME): form = SuForm(request.POST) if not form.is_valid(): return TemplateResponse(request, 'simple-centered-form.html', {'form': form, 'action': reverse('su'), 'title': _("Login as another user")}) user = form.cleaned_data['user'] if user.is_superuser or is_under_su(request): raise SuspiciousOperation su_to_user(request, user, form.cleaned_data['backend']) if redirect_field_name in request.REQUEST: next_page = request.REQUEST[redirect_field_name] return safe_redirect(request, next_page)
def reattach_problem_confirm_view(request, problem_instance_id, contest_id): contest = get_object_or_404(Contest, id=contest_id) if not can_admin_contest(request.user, contest): raise PermissionDenied problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) if request.POST: pi = get_new_problem_instance(problem_instance.problem, contest) messages.success(request, _(u"Problem {} added successfully." .format(pi))) return safe_redirect(request, reverse( 'oioioiadmin:contests_probleminstance_changelist', kwargs={'contest_id': contest.id})) return TemplateResponse(request, 'contests/reattach_problem_confirm.html', {'problem_instance': problem_instance, 'destination_contest': contest})
def view(self, request, contest, existing_problem=None): is_reupload = existing_problem is not None if is_reupload: # reuploading doesn't make much sense here return _("Reuploading quizzes is not supported") if request.method == "POST": form = EmptyQuizSourceForm(request.POST) else: form = EmptyQuizSourceForm() post_data = {'form': form, 'is_reupload': is_reupload} if request.method == "POST" and form.is_valid(): with transaction.atomic(): controller = 'oioioi.quizzes.controllers.QuizProblemController' quiz = Quiz.objects.create( name=form.cleaned_data['name'], short_name=form.cleaned_data['short_name'], controller_name=controller, author=request.user ) tag = Tag.objects.get_or_create(name='quiz')[0] TagThrough.objects.create( problem=quiz, tag=tag ) ProblemSite.objects.create( problem=quiz, url_key=generate_key() ) pi = ProblemInstance.objects.create( problem=quiz, short_name=quiz.short_name ) quiz.main_problem_instance = pi quiz.save() if contest: quiz.contest = contest get_new_problem_instance(quiz, contest) messages.success(request, _("Quiz successfully added")) return safe_redirect(request, reverse( 'oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, "quizzes/emptyquiz-source.html", post_data)
def rejudge_all_submissions_for_problem_view(request, problem_instance_id): problem_instance = get_object_or_404(ProblemInstance, id=problem_instance_id) count = problem_instance.submission_set.count() if request.POST: for submission in problem_instance.submission_set.all(): problem_instance.controller.judge(submission, request.GET.dict(), is_rejudge=True) messages.info(request, ungettext_lazy("%(count)d rejudge request received.", "%(count)d rejudge requests reveived.", count) % {'count': count}) problem_instance.needs_rejudge = False problem_instance.save() return safe_redirect(request, reverse( 'oioioiadmin:contests_probleminstance_changelist')) return TemplateResponse(request, "contests/confirm_rejudge.html", {'count': count})
def registration_view(self, request): participant = self._get_participant_for_form(request) if 'pa_paregistrationformdata' in request.session: # pylint: disable=not-callable form = self.form_class(request.session[ 'pa_paregistrationformdata']) del request.session['pa_paregistrationformdata'] else: form = self.get_form(request, participant) if request.method == 'POST': # pylint: disable=maybe-no-member if form.is_valid(): participant, created = Participant.objects \ .get_or_create(contest=self.contest, user=request.user) self.handle_validated_form(request, form, participant) if 'next' in request.GET: return safe_redirect(request, request.GET['next']) else: return redirect('default_contest_view', contest_id=self.contest.id) context = {'form': form, 'participant': participant} return TemplateResponse(request, self.registration_template, context)
def login_view(request, redirect_field_name=REDIRECT_FIELD_NAME, **kwargs): if request.user.is_authenticated(): redirect_to = request.GET.get(redirect_field_name, None) return safe_redirect(request, redirect_to) else: return auth_login(request, extra_context=site_name(request), **kwargs)
def login_view(request, redirect_field_name=REDIRECT_FIELD_NAME, **kwargs): if request.user.is_authenticated: redirect_to = request.GET.get(redirect_field_name, None) return safe_redirect(request, redirect_to) else: return Login2FAView.as_view(**kwargs)(request)
def login_view(request, redirect_field_name=REDIRECT_FIELD_NAME, **kwargs): if request.user.is_authenticated(): redirect_to = request.REQUEST.get(redirect_field_name, None) return safe_redirect(request, redirect_to) else: return auth_login(request, **kwargs)