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})
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})
def add_problem_block(request): is_success, is_error = False, False if request.method == 'POST': form = AddProblemForm(request.POST) if form.is_valid(): path = norm(form.cleaned_data['path']) if is_problem_path(path): problem = Problem(path=path) problem.save() import_to_database(problem) problem.save() is_success = True else: is_error = True else: form = AddProblemForm() return { 'form': form, 'is_success': is_success, 'is_error': is_error, }
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, }
def import_from_polygon_block(request): is_success = False if request.method == 'POST': form = ProblemImportFromPolygonForm(request.POST) if form.is_valid(): with ChangeDir(form.cleaned_data['target_path']): archive_name = download_zip.get_problem( form.cleaned_data['contest_id'], form.cleaned_data['problem_letter'].upper()) problem_name = create_problem(archive_name + ".zip") problem_path = norm(os.path.join(form.cleaned_data['target_path'], problem_name)) problem = Problem(path=problem_path, short_name=problem_name) problem.save() import_to_database(model=problem) problem.save() form = ProblemImportFromPolygonForm() is_success = True else: form = ProblemImportFromPolygonForm() return { 'form': form, 'is_success': is_success, }
def copy_problem_block(request): is_success, is_error = False, False if request.method == 'POST': form = CopyProblemForm(request.POST) if form.is_valid(): new_path = norm(form.cleaned_data['copy_to']) old_path = Problem.objects.get(id=form.cleaned_data['problem']).path copytree(old_path, new_path) problem = Problem(path=new_path) problem.save() problem = import_to_database(path=new_path) problem.save() is_success = True print(111, problem.id, problem.name) id = problem.id prob = Problem.objects.get(id=id) print(222, prob.id, prob.name) else: form = CopyProblemForm() return { 'form': form, 'is_success': is_success, 'is_error': is_error, }