Ejemplo n.º 1
0
def upload_main_block(request, problem):
    if request.method == 'POST' and 'upload_main' in request.POST:
        form = ProblemUploadFilesForm(request.POST, request.FILES)
        modified = False
        if form.is_valid():
            if form.cleaned_data['select_checker']:
                with ChangeDir(problem.path):
                    path = standard_checkers_utils.add_standard_checker_to_solution(
                        form.cleaned_data['select_checker'])
                    problem.checker_path = norm(path)
                    modified = True
            elif 'checker' in request.FILES.keys():
                checker_file = request.FILES['checker']
                problem.checker_path = norm(checker_file.name)
                file_save(checker_file, problem.path)
                modified = True
            if 'validator' in request.FILES.keys():
                validator_file = request.FILES['validator']
                problem.validator_path = norm(validator_file.name)
                file_save(validator_file, problem.path)
                modified = True
            problem.save()
            if modified:
                export_from_database(problem)
    form = ProblemUploadFilesForm()
    return {'form': form, 'problem': problem}
Ejemplo n.º 2
0
def upload_solution(request, id):
    problem = Problem.objects.get(id=id)
    if request.method == 'POST':
        form = SolutionAddForm(request.POST, request.FILES)
        if 'submit_file' in form.data:
            if form.is_valid() and request.FILES:
                solution = Solution(problem=problem)
                directory = norm(os.path.join(str(problem.path), 'solutions'))
                solution.path = norm(os.path.relpath(
                        file_save(request.FILES['solution_file'], directory),
                        start=str(problem.path)))
                solution.input = form.cleaned_data['input_file_name']
                solution.output = form.cleaned_data['output_file_name']
                solution.save()
                for choice in Verdict.objects.filter(name__in=form.cleaned_data['possible_verdicts']):
                    solution.possible_verdicts.add(choice)
                for choice in Verdict.objects.filter(name__in=form.cleaned_data['expected_verdicts']):
                    solution.expected_verdicts.add(choice)
                solution.save()
                form = SolutionAddForm()
                export_from_database(problem)
        else:
            form = SolutionAddForm()
    else:
        form = SolutionAddForm()
    return {'form': form}
Ejemplo n.º 3
0
        def wrapped_function(request, id, *args, **kwargs):
            problem_was_deleted = False

            if read:
                try:
                    problem = Problem.objects.get(id=id)
                except Problem.DoesNotExist:
                    problem = None
                problem_path = problem.path if problem else None
                if problem:
                    package_path = os.path.join(problem.path,
                                                'default.package')
                    if not (os.path.exists(package_path)
                            and os.path.getmtime(package_path)
                            == problem.last_modified):
                        # print('OLOLO', os.path.getmtime(package_path), '!=',
                        # problem.last_modified, 'diff is',
                        # os.path.getmtime(package_path) - problem.last_modified)
                        problem = synchronization.import_to_database(problem)
                        if problem:
                            problem.last_modified = os.path.getmtime(
                                package_path)
                            problem.save()
                        else:
                            problem_was_deleted = True
            if not problem_was_deleted:
                response = function(request, id, *args, **kwargs)
                if write:
                    synchronization.export_from_database(
                        Problem.objects.get(id=id))
                return response
            else:
                return render_to_response('problem/deleted_problem.html',
                                          {"path": problem_path})
Ejemplo n.º 4
0
def upload_solution(request, id):
    problem = Problem.objects.get(id=id)
    if request.method == 'POST':
        form = SolutionAddForm(request.POST, request.FILES)
        if 'submit_file' in form.data:
            if form.is_valid() and request.FILES:
                solution = Solution(problem=problem)
                directory = norm(os.path.join(str(problem.path), 'solutions'))
                solution.path = norm(
                    os.path.relpath(file_save(request.FILES['solution_file'],
                                              directory),
                                    start=str(problem.path)))
                solution.input = form.cleaned_data['input_file_name']
                solution.output = form.cleaned_data['output_file_name']
                solution.save()
                for choice in Verdict.objects.filter(
                        name__in=form.cleaned_data['possible_verdicts']):
                    solution.possible_verdicts.add(choice)
                for choice in Verdict.objects.filter(
                        name__in=form.cleaned_data['expected_verdicts']):
                    solution.expected_verdicts.add(choice)
                solution.save()
                form = SolutionAddForm()
                export_from_database(problem)
        else:
            form = SolutionAddForm()
    else:
        form = SolutionAddForm()
    return {'form': form}
Ejemplo n.º 5
0
def upload_main_block(request, problem):
    if request.method == 'POST' and 'upload_main' in request.POST:
        form = ProblemUploadFilesForm(request.POST, request.FILES)
        modified = False
        if form.is_valid():
            if form.cleaned_data['select_checker']:
                with ChangeDir(problem.path):
                    path = standard_checkers_utils.add_standard_checker_to_solution(form.cleaned_data['select_checker'])
                    problem.checker_path = norm(path)
                    modified = True
            elif 'checker' in request.FILES.keys():
                checker_file = request.FILES['checker']
                problem.checker_path = norm(checker_file.name)
                file_save(checker_file, problem.path)
                modified = True
            if 'validator' in request.FILES.keys():
                validator_file = request.FILES['validator']
                problem.validator_path = norm(validator_file.name)
                file_save(validator_file, problem.path)
                modified = True
            problem.save()
            if modified:
                export_from_database(problem)
    form = ProblemUploadFilesForm()
    return {'form': form, 'problem': problem}
Ejemplo n.º 6
0
        def wrapped_function(request, id, *args, **kwargs):
            problem_was_deleted = False

            if read:
                try:
                    problem = Problem.objects.get(id=id)
                except Problem.DoesNotExist:
                    problem = None
                problem_path = problem.path if problem else None
                if problem:
                    package_path = os.path.join(problem.path, 'default.package')
                    if not (os.path.exists(package_path) and
                            os.path.getmtime(package_path) == problem.last_modified):
                        # print('OLOLO', os.path.getmtime(package_path), '!=',
                              # problem.last_modified, 'diff is',
                              # os.path.getmtime(package_path) - problem.last_modified)
                        problem = synchronization.import_to_database(problem)
                        if problem:
                            problem.last_modified = os.path.getmtime(package_path)
                            problem.save()
                        else:
                            problem_was_deleted = True
            if not problem_was_deleted:
                response = function(request, id, *args, **kwargs)
                if write:
                    synchronization.export_from_database(Problem.objects.get(id=id))
                return response
            else:
                return render_to_response('problem/deleted_problem.html',
                                          {"path": problem_path})
Ejemplo n.º 7
0
def edit_or_create_problem_block(request, problem=None):
    is_success = False
    if request.method == 'POST':
        form = ProblemEditForm(request.POST)
        if form.is_valid():
            if problem is None:
                if not os.path.exists(form.cleaned_data["path"]):
                    raise NoDirectoryException("There is no such directory!")
                if os.path.exists(os.path.join(form.cleaned_data["path"], form.cleaned_data["short_name"])):
                    raise ProblemExistsException("This problem already exists")
                problem = import_to_database(problem, "../templates/Template/")
                problem.path = norm(os.path.join(form.cleaned_data["path"], form.cleaned_data["short_name"]))
                with ChangeDir(form.cleaned_data["path"]):
                    generate_problem(form.cleaned_data["short_name"])
                if not hasattr(ProblemEditForm, 'available_tags'):
                    other_tags = form.cleaned_data['available_tags']
                    for tag in other_tags:
                        if tag:
                            problem.tags.add(ProblemTag.objects.get(name=tag))
                if form.cleaned_data['new_tags']:
                    for tags in form.cleaned_data['new_tags'].split(';'):
                        for tag in map(str.strip, tags.split(',')):
                            if tag:
                                problem.tags.add(ProblemTag.objects.get_or_create(name=tag)[0])
            problem.name = form.cleaned_data["name"]
            problem.short_name = form.cleaned_data["short_name"]
            problem.input = form.cleaned_data["input"]
            problem.output = form.cleaned_data["output"]
            problem.time_limit = float(form.cleaned_data["time_limit"])
            problem.memory_limit = int(form.cleaned_data["memory_limit"])
            problem.save()
            export_from_database(problem)
            is_success = True
    else:
        if problem is None:
            form = ProblemEditForm()
        else:
            form = ProblemEditForm(initial={
                'name': problem.name,
                'short_name': problem.short_name,
                'input': problem.input,
                'output': problem.output,
                'time_limit': problem.time_limit,
                'memory_limit': problem.memory_limit,
            })
    return {
        'form': form,
        'is_success': is_success,
    }
Ejemplo n.º 8
0
def manage_tests(request, problem):
    tp_path = os.path.join(problem.path, globalconfig.default_tests_config)
    form = ManageTestsForm()
    error = None

    if request.method == 'POST' and 'manage_tests' in request.POST:
        form = ManageTestsForm(request.POST)
        if form.is_valid():
            file_write(form.cleaned_data["tests_please_content"], tp_path)

            for field, listname in ((problem.well_done_test, 'well_done_test'),
                                    (problem.well_done_answer,
                                     'well_done_answer')):
                field.clear()
                for name in request.POST.getlist(listname):
                    field.add(WellDone.objects.get(name=name))
            problem.save()
            export_from_database(problem)

            try:
                if "generate_tests" in request.POST:
                    with ChangeDir(problem.path):
                        generate_tests()
                elif "validate" in request.POST:
                    with ChangeDir(problem.path):
                        TestsAndAnswersGenerator().validate()
            except PleaseException as e:
                error = e
    else:
        try:
            form = ManageTestsForm(
                initial={"tests_please_content": file_read(tp_path)})
        except (UnicodeDecodeError, FileNotFoundError) as e:
            error = e

    well_dones = [(well_done.name, well_done
                   in problem.well_done_test.all(), well_done
                   in problem.well_done_answer.all())
                  for well_done in WellDone.objects.all()]

    answer = {
        'form': form,
        'problem': problem,
        'error': error,
        'well_dones': well_dones
    }
    answer.update({'upload_files': upload_files(request, problem)})
    return answer
Ejemplo n.º 9
0
def manage_tests(request, problem):
    tp_path = os.path.join(problem.path, globalconfig.default_tests_config)
    form = ManageTestsForm()
    error = None

    if request.method == 'POST' and 'manage_tests' in request.POST:
        form = ManageTestsForm(request.POST)
        if form.is_valid():
            file_write(form.cleaned_data["tests_please_content"], tp_path)

            for field, listname in ((problem.well_done_test, 'well_done_test'),
                                    (problem.well_done_answer, 'well_done_answer')):
                field.clear()
                for name in request.POST.getlist(listname):
                    field.add(WellDone.objects.get(name=name))
            problem.save()
            export_from_database(problem)

            try:
                if "generate_tests" in request.POST:
                    with ChangeDir(problem.path):
                        generate_tests()
                elif "validate" in request.POST:
                    with ChangeDir(problem.path):
                        TestsAndAnswersGenerator().validate()
            except PleaseException as e:
                error = e
    else:
        try:
            form = ManageTestsForm(initial={"tests_please_content": file_read(tp_path)})
        except (UnicodeDecodeError, FileNotFoundError) as e:
            error = e

    well_dones = [(well_done.name,
                   well_done in problem.well_done_test.all(),
                   well_done in problem.well_done_answer.all())
                   for well_done in WellDone.objects.all()]

    answer = {'form': form, 'problem': problem, 'error': error, 'well_dones': well_dones}
    answer.update({'upload_files': upload_files(request, problem)})
    return answer