Esempio n. 1
0
    def __init__(self, *args, **kwargs):
        self.competition = kwargs.pop('competition')
        self.evaluator = get_evaluator(self.competition.evaluator_version)
        self.fixed_score = self.competition.fixed_task_score
        user = kwargs.pop('user')
        super(CompetitionTaskForm, self).__init__(*args, **kwargs)

        self.t_comment_extra_class = "ctask-comment"
        if self.instance.pk:
            self.fields['text'].initial = self.instance.task.content.text
            self.fields['comment'].initial = self.instance.comment.text
            self.t_comment_extra_class += \
                    " " + ctask_comment_class(self.instance, user)

        descriptor = self.initial.get('descriptor')
        if descriptor:
            variables = safe_parse_descriptor(self.evaluator, descriptor)
            self.fields['descriptor'].help_text = get_solution_help_text(
                    variables, error_message=_("Invalid!"), show_types=True)
        self.fields['descriptor'].label = mark_safe(
                xss.escape(_("Solution")) + \
                ' <a href="' + comp_url(self.competition, 'rules') +
                '" target="_blank"><i class="icon-question-sign" title="' +
                xss.escape(_("Help")) + '"></i></a>')
        if self.fixed_score:
            del self.fields['score']

        self.fields['text'].widget.attrs.update(
                {'class': 'comp-mathcontent-text', 'rows': 5})
        self.fields['comment'].widget.attrs.update(
                {'class': 'comp-mathcontent-text ctask-comment', 'rows': 3})
Esempio n. 2
0
def task_detail(request, competition, data, ctask_id):
    is_admin = data['is_admin']
    extra = ['task__author'] if is_admin else []
    ctask = get_object_or_404(
            CompetitionTask.objects.select_related('chain', 'task',
                'task__content', *extra),
            competition=competition, id=ctask_id)
    ctask_id = int(ctask_id)
    team = data['team']
    if not is_admin:
        if (not team and not data['has_finished']) or not data['has_started'] \
                or ctask.chain.unlock_minutes > data['minutes_passed']:
            raise Http404

    if ctask.score > 1:
        ctask.t_score_text = ungettext(
                "This task is worth %d point.",
                "This task is worth %d points.",
                ctask.score) % ctask.score

    evaluator = get_evaluator(competition.evaluator_version)
    variables = safe_parse_descriptor(evaluator, ctask.descriptor)
    if team:
        ctasks, chain_submissions = preprocess_chain(
                competition, ctask.chain, team, preloaded_ctask=ctask)
        submissions = [x for x in chain_submissions if x.ctask_id == ctask_id]
        submissions.sort(key=lambda x: x.date)

        if data['has_finished']:
            ctask.t_is_locked = False

        if ctask.t_is_locked and not is_admin:
            raise Http404

        if request.method == 'POST' and (not data['has_finished'] or is_admin):
            solution_form = CompetitionSolutionForm(request.POST,
                    descriptor=ctask.descriptor, evaluator=evaluator)
            submission = None
            delete = False
            if is_admin and 'delete-submission' in request.POST:
                try:
                    submission = Submission.objects.get(
                            id=request.POST['delete-submission'])
                    chain_submissions = \
                            [x for x in chain_submissions if x != submission]
                    submissions = [x for x in submissions if x != submission]
                    submission.delete()
                    delete = True
                except Submission.DoesNotExist:
                    pass
            elif solution_form.is_valid():
                # TODO: Ignore submission if already correctly solved.
                if len(submissions) < ctask.max_submissions:
                    result = solution_form.cleaned_data['result']
                    is_correct = evaluator.check_result(
                            ctask.descriptor, result)
                    submission = Submission(ctask=ctask, team=team,
                            result=result, cache_is_correct=is_correct)
                    submission.save()
                    chain_submissions.append(submission)
                    submissions.append(submission)

            if delete or submission:
                if is_admin and team.is_admin_private():
                    update_ctask_cache_admin_solved_count(
                            competition, ctask, ctask.chain)
                update_score_on_ctask_action(competition, team, ctask.chain,
                        ctask, submission, delete,
                        chain_ctask_ids=[x.id for x in ctasks],
                        chain_submissions=chain_submissions)

                # Prevent form resubmission.
                return (ctask.get_absolute_url(), )

        else:
            solution_form = CompetitionSolutionForm(
                    descriptor=ctask.descriptor, evaluator=evaluator)

        data['is_solved'] = any(x.cache_is_correct for x in submissions)
        data['solution_form'] = solution_form
        data['submissions'] = submissions
        data['submissions_left'] = ctask.max_submissions - len(submissions)

    if is_admin:
        data['all_ctask_submissions'] = list(Submission.objects \
                .filter(ctask_id=ctask_id) \
                .select_related('team') \
                .order_by('id'))
        for submission in data['all_ctask_submissions']:
            submission.team.competition = competition

    data['help_text'] = get_solution_help_text(variables)
    data['chain'] = ctask.chain
    data['ctask'] = ctask

    if competition.show_solutions and data['has_finished'] \
            and not data.get('is_solved', False):
        data['sample_solution'] = get_sample_solution(variables)

    return data