Exemplo n.º 1
0
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},
    )
Exemplo n.º 2
0
 def check_permissions(request, contest=None, existing_problem=None):
     if not request.user.has_perm('problems.problems_db_admin'):
         if contest and (not can_admin_contest(request.user, contest)):
             return False
     if existing_problem:
         if not can_admin_problem(request, existing_problem):
             return False
     return True
Exemplo n.º 3
0
def can_admin_problem(request, problem):
    if request.user.has_perm('problems.problems_db_admin'):
        return True
    if request.user.has_perm('problems.problem_admin', problem):
        return True
    if request.user == problem.author:
        return True
    if problem.contest:
        return can_admin_contest(request.user, problem.contest)
    return False
Exemplo n.º 4
0
def can_admin_problem(request, problem):
    if request.user.has_perm('problems.problems_db_admin'):
        return True
    if request.user.has_perm('problems.problem_admin', problem):
        return True
    if request.user == problem.author:
        return True
    # If a user is admining a contest where the task was initialy added,
    # he is considered to be a co-author of the task, giving him rights to admin it
    if problem.contest:
        return can_admin_contest(request.user, problem.contest)
    return False
Exemplo n.º 5
0
def ban_user_view(request, user_id):
    # Banning user blocks any user interaction with forum, while still
    # allowing viewing forum contents.

    forum = request.contest.forum

    if 'next' in request.GET:
        redirect_url = request.GET['next']
    else:
        redirect_url = reverse('forum',
                               kwargs={'contest_id': request.contest.id})

    user = get_object_or_404(User, id=user_id)
    if can_admin_contest(user, request.contest):
        messages.error(request, _("You can't ban an admin."))
        return redirect(redirect_url)

    if request.method == 'POST':
        form = BanForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user = user
            instance.forum = request.contest.forum
            instance.admin = request.user
            instance.save()
            messages.success(request, _("Banned user: "******"Removed %d reports") % removed_reports_count)
            return redirect(redirect_url)
    else:
        form = BanForm()

    return TemplateResponse(
        request,
        'forum/ban_user.html',
        {
            'banned_user': user,
            'form': form,
            'next': redirect_url,
            'msgs': get_msgs(request),
        },
    )
Exemplo n.º 6
0
def reattach_problem_contest_list_view(request, problem_instance_id,
                                       full_list=False):
    problem_instance = get_object_or_404(ProblemInstance,
                                         id=problem_instance_id)

    if full_list:
        contests = Contest.objects.all()
    else:
        contests = recent_contests(request) or Contest.objects.all()

    contests = [c for c in contests if can_admin_contest(request.user, c)]
    return TemplateResponse(request,
                            'contests/reattach_problem_contest_list.html',
                            {'problem_instance': problem_instance,
                             'contest_list': contests,
                             'full_list': full_list})
Exemplo n.º 7
0
def reattach_problem_contest_list_view(request, problem_instance_id,
                                       full_list=False):
    problem_instance = get_object_or_404(ProblemInstance,
                                         id=problem_instance_id)

    if full_list:
        contests = Contest.objects.all()
    else:
        contests = recent_contests(request) or Contest.objects.all()

    contests = [c for c in contests if can_admin_contest(request.user, c)]
    return TemplateResponse(request,
                            'contests/reattach_problem_contest_list.html',
                            {'problem_instance': problem_instance,
                             'contest_list': contests,
                             'full_list': full_list})
Exemplo n.º 8
0
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})
Exemplo n.º 9
0
def ban_user_view(request, user_id):
    # Banning user blocks any user interaction with forum, while still
    # allowing viewing forum contents.

    forum = request.contest.forum

    if 'next' in request.GET:
        redirect_url = request.GET['next']
    else:
        redirect_url = reverse('forum', kwargs={'contest_id': request.contest.id})

    user = get_object_or_404(User, id=user_id)
    if can_admin_contest(user, request.contest):
        messages.error(request, _("You can't ban an admin."))
        return redirect(redirect_url)

    if request.method == 'POST':
        form = BanForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user = user
            instance.forum = request.contest.forum
            instance.admin = request.user
            instance.save()
            messages.success(request, _("Banned user: "******"Removed %d reports") %
                                 removed_reports_count)
            return redirect(redirect_url)
    else:
        form = BanForm()

    return TemplateResponse(request, 'forum/ban_user.html',
                            {'banned_user': user,
                             'form': form,
                             'next': redirect_url,
                             'msgs': get_msgs(request)})
Exemplo n.º 10
0
def can_admin_problem_instance(request, pi):
    if pi.contest:
        return can_admin_contest(request.user, pi.contest)
    else:
        return can_admin_problem(request, pi.problem)
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
 def has_change_permission(self, request, obj=None):
     if not obj:
         return request.user.is_superuser
     return can_admin_contest(request.user, obj)
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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)