コード例 #1
0
    def create_submission(self,
                          request,
                          problem_instance,
                          form_data,
                          judge_after_create=True,
                          **kwargs):
        submission = ProgramSubmission(
            user=form_data.get('user', request.user),
            problem_instance=problem_instance,
            kind=form_data.get(
                'kind',
                problem_instance.controller.get_default_submission_kind(
                    request, problem_instance=problem_instance)),
            date=request.timestamp)

        file = form_data['file']
        if file is None:
            lang_exts = get_allowed_languages_dict(problem_instance)
            langs_field_name = form_field_id_for_langs(problem_instance)
            extension = lang_exts[form_data[langs_field_name]][0]
            file = ContentFile(form_data['code'], '__pasted_code.' + extension)

        submission.source_file.save(file.name, file)
        submission.save()
        if judge_after_create:
            problem_instance.controller.judge(submission)
        return submission
コード例 #2
0
    def _add_langs_to_form(self, request, form, problem_instance):
        controller = problem_instance.controller

        choices = [('', '')]
        for lang in get_allowed_languages_dict(problem_instance).keys():
            compiler_name = None
            compiler = controller.get_compiler_for_language(problem_instance, lang)
            if compiler is not None:
                available_compilers = getattr(settings, 'AVAILABLE_COMPILERS', {})
                compilers_for_language = available_compilers.get(lang)
                if compilers_for_language is not None:
                    compiler_info = compilers_for_language.get(compiler)
                    if compiler_info is not None:
                        compiler_name = compiler_info.get('display_name')
            langs = getattr(settings, 'SUBMITTABLE_LANGUAGES', {})
            lang_display = langs[lang]['display_name']
            if compiler_name is not None:
                choices.append((lang, "%s (%s)" % (lang_display, compiler_name)))
            else:
                choices.append((lang, lang_display))

        field_name = form_field_id_for_langs(problem_instance)
        form.fields[field_name] = forms.ChoiceField(
            required=False,
            label=_("Programming language"),
            choices=choices,
            widget=forms.Select(attrs={'disabled': 'disabled'}),
        )
        narrow_input_field(form.fields[field_name])
        form.set_custom_field_attributes(field_name, problem_instance)
コード例 #3
0
    def validate_submission_form(self, request, problem_instance, form,
                                 cleaned_data):
        if any(field in form.errors.as_data() for field in ('file', 'code')):
            return  # already have a ValidationError

        is_file_chosen = 'file' in cleaned_data and \
                cleaned_data['file'] is not None
        is_code_pasted = 'code' in cleaned_data and cleaned_data['code']

        if (not is_file_chosen and not is_code_pasted) or \
                (is_file_chosen and is_code_pasted):
            raise ValidationError(
                _("You have to either choose file or paste "
                  "code."))

        langs_field_name = form_field_id_for_langs(problem_instance)
        if langs_field_name not in cleaned_data:
            cleaned_data[langs_field_name] = None

        if not cleaned_data[langs_field_name] and is_file_chosen:
            ext = os.path.splitext(cleaned_data['file'].name)[1].strip('.')
            cleaned_data[langs_field_name] = \
                get_language_by_extension(problem_instance, ext)

        if not cleaned_data[langs_field_name]:
            if is_code_pasted:
                raise ValidationError(
                    _("You have to choose programming language."))
            else:
                raise ValidationError(_("Unrecognized file extension."))

        langs = get_allowed_languages_dict(problem_instance)
        if cleaned_data[langs_field_name] not in langs.keys():
            raise ValidationError(
                _("This language is not allowed for selected"
                  " problem."))

        if is_file_chosen:
            code = cleaned_data['file'].read()
        else:
            code = cleaned_data['code'].encode('utf-8')

        if problem_instance.controller \
                .check_repeated_submission(request, problem_instance, form):
            lines = iter(code.splitlines())
            md5 = hashlib.md5()
            for line in lines:
                md5.update(line)
            md5 = md5.hexdigest()
            session_md5_key = 'programs_%d_md5' % \
                    cleaned_data['problem_instance'].id

            if session_md5_key in request.session and \
                    md5 == request.session[session_md5_key]:
                del request.session[session_md5_key]
                raise ValidationError(
                    _("You have submitted the same file for this problem "
                      "again. Please resubmit if you really want "
                      "to submit the same file"))
            else:
                request.session[session_md5_key] = md5
                request.session.save()

        return cleaned_data