Example #1
0
def status(request):
    status_filter = StatusFilter(request.GET)
    submissions = get_visible_submission(request.user).order_by('-id')
    render_data = {}
    render_data['status_filter'] = status_filter
    render_data['running_contests'] = get_running_contests().order_by('id')

    if status_filter.is_valid():
        username = status_filter.cleaned_data['username']
        cid = status_filter.cleaned_data['cid']
        pid = status_filter.cleaned_data['pid']
        status = status_filter.cleaned_data['status']

        if username:
            user = User.objects.get(username=username)
            submissions = submissions.filter(user=user)

        if pid:
            problem = Problem.objects.get(id=pid)
            submissions = submissions.filter(problem=problem)

        if cid:
            contest = Contest.objects.get(id=cid)
            submissions = get_contest_submissions(contest, submissions)

        if status:
            submissions = submissions.filter(status=status)

        submissions = get_current_page(request, submissions)

        # Regroup submission details
        submissions.object_list = regroup_submission(submissions.object_list)

        # Serialize to json
        if 'type' in request.GET and request.GET['type'] == 'json':
            submissions = [submission['grouper']
                           for submission in submissions.object_list]
            # Remove unnecessary fields
            submissions = serialize("python", submissions)
            submissions = [s['fields'] for s in submissions]

            return HttpResponse(json.dumps(submissions, default=lambda obj: obj.isoformat() if hasattr(obj, 'isoformat') else obj))
    else:
        messages.warning(request, 'Please check filter constraints again!')
        return render_index(request, 'status/status.html', render_data)

    if not submissions:
        messages.warning(request, 'No submissions found for the given query!')

    render_data['submissions'] = submissions

    return render_index(request, 'status/status.html', render_data)
def show_detail(submission, user):
    """Test if the user can see that submission's
    details (code, error message, etc)

    Args:
        submission: the submission to show
        user: an User object
    Returns:
        a boolean of the judgement
    """
    user = validate_user(user)

    # admin can see everyone's detail
    if user.has_admin_auth():
        return True
    # no one can see admin's detail
    if submission.user.has_admin_auth():
        return False
    # during the contest, only owner/coowner can view contestants' detail
    contests = get_running_contests()
    if contests:
        contests = contests.filter(
            problem=submission.problem,
            creation_time__lte=submission.submit_time
        )
        return show_contest_submission(submission, user, contests)
    # a user can view his own detail
    if submission.user == user:
        return True
    # a problem owner can view his problem's detail in normal mode
    if submission.problem.owner_id == user.username:
        return True
    # contest owner/coowner can still view code after the contest in normal
    # mode
    contests = Contest.objects.filter(
        problem=submission.problem,
        end_time__gte=submission.submit_time,
        creation_time__lte=submission.submit_time)
    if show_contest_submission(submission, user, contests):
        return True
    # a user can view his team member's detail
    if submission.team:
        team_member = TeamMember.objects.filter(
            team=submission.team, member=user)
        if team_member or submission.team.leader == user:
            return True
    # no condition is satisfied
    return False
Example #3
0
def show_detail(submission, user):
    """Test if the user can see that submission's
    details (code, error message, etc)

    Args:
        submission: the submission to show
        user: an User object
    Returns:
        a boolean of the judgement
    """
    user = validate_user(user)

    # admin can see everyone's detail
    if user.has_admin_auth():
        return True
    # no one can see admin's detail
    if submission.user.has_admin_auth():
        return False
    # during the contest, only owner/coowner can view contestants' detail
    contests = get_running_contests()
    if contests:
        contests = contests.filter(problem=submission.problem,
                                   creation_time__lte=submission.submit_time)
        return show_contest_submission(submission, user, contests)
    # a user can view his own detail
    if submission.user == user:
        return True
    # a problem owner can view his problem's detail in normal mode
    if submission.problem.owner_id == user.username:
        return True
    # contest owner/coowner can still view code after the contest in normal
    # mode
    contests = Contest.objects.filter(
        problem=submission.problem,
        end_time__gte=submission.submit_time,
        creation_time__lte=submission.submit_time)
    if show_contest_submission(submission, user, contests):
        return True
    # a user can view his team member's detail
    if submission.team:
        team_member = TeamMember.objects.filter(team=submission.team,
                                                member=user)
        if team_member or submission.team.leader == user:
            return True
    # no condition is satisfied
    return False
Example #4
0
def status(request):
    start_time = time.time()
    status_filter = StatusFilter(request.GET)
    submissions = get_visible_submission(request.user).order_by('-id')
    render_data = {}
    render_data['status_filter'] = status_filter
    render_data['running_contests'] = get_running_contests().order_by('id')

    if status_filter.is_valid():
        username = status_filter.cleaned_data['username']
        cid = status_filter.cleaned_data['cid']
        pid = status_filter.cleaned_data['pid']
        status = status_filter.cleaned_data['status']

        if username:
            user = User.objects.get(username=username)
            submissions = submissions.filter(user=user)

        if pid:
            problem = Problem.objects.get(id=pid)
            submissions = submissions.filter(problem=problem)

        if cid:
            contest = Contest.objects.get(id=cid)
            submissions = get_contest_submissions(contest, submissions)

        if status:
            submissions = submissions.filter(status=status)

        huge_table_count = None
        # No constriant provided, the search result would be huge
        # Cache the count for faster paging
        if not (username or pid or cid or status):
            if cache.get('huge_table_count'):
                huge_table_count = cache.get('huge_table_count')
            else:
                huge_table_count = submissions.count()
                cache.set('huge_table_count', huge_table_count, 60)

        submissions = get_current_page(request,
                                       submissions,
                                       count=huge_table_count)

        # Regroup submission details
        submissions.object_list = regroup_submission(submissions.object_list)

        # Serialize to json
        if 'type' in request.GET and request.GET['type'] == 'json':
            submissions = [
                submission['grouper'] for submission in submissions.object_list
            ]
            # Remove unnecessary fields
            submissions = serialize("python", submissions)
            submissions = [s['fields'] for s in submissions]

            return HttpResponse(
                json.dumps(submissions,
                           default=lambda obj: obj.isoformat()
                           if hasattr(obj, 'isoformat') else obj))
    else:
        messages.warning(request, 'Please check filter constraints again!')
        return render_index(request, 'status/status.html', render_data)

    if not submissions:
        messages.warning(request, 'No submissions found for the given query!')

    render_data['submissions'] = submissions
    render_data['searching_time'] = time.time() - start_time
    return render_index(request, 'status/status.html', render_data)