def copy_contest_block(request): is_success, is_error = False, False if request.method == 'POST': form = CopyContestForm(request.POST) if form.is_valid(): new_path = norm(form.cleaned_data['new_contest_file']) old_path = Contest.objects.get(id=form.cleaned_data['contest']).path copyfile(old_path, new_path) contest = Contest(path=new_path) contest.save() import_to_database(path=new_path) contest.save() is_success = True else: form = CopyContestForm() return { 'form': form, 'is_success': is_success, 'is_error': is_error, }
def copy_contest_block(request): is_success, is_error = False, False if request.method == 'POST': form = CopyContestForm(request.POST) if form.is_valid(): new_path = norm(form.cleaned_data['new_contest_file']) old_path = Contest.objects.get( id=form.cleaned_data['contest']).path copyfile(old_path, new_path) contest = Contest(path=new_path) contest.save() import_to_database(path=new_path) contest.save() is_success = True else: form = CopyContestForm() return { 'form': form, 'is_success': is_success, 'is_error': is_error, }
def import_to_database(model=None, path=None): assert (model is None) != (path is None) if path is not None: model = get_contest_by_path(path) contest_path = path or str(model.path) if not os.path.exists(contest_path): model.delete() return None conf = PleaseContest(contest_path).config model.name = conf.get("name", "") method = conf.get("id_method", "") if method: for id in Contest.ID_METHODS: if id[1] == method: model.id_method = id[0] break model.statement_name = conf["statement"].get("name", "") model.statement_location = conf["statement"].get("location", "") model.statement_date = conf["statement"].get("date", "") model.statement_template = conf["statement"].get("template", "contest.tex") model.save() order = 0 ContestProblem.objects.filter(contest=model).delete() for problem in conf["problem"]: ContestProblem( problem=Problem.objects.get(path=norm(os.path.join(os.path.dirname(model.path), problem["path"]))), contest=model, id_in_contest=problem["id"], order=order, ).save() order += 1 a = Contest.objects.get(id=1) return model
def import_to_database(model=None, path=None): assert ((model is None) != (path is None)) if path is not None: model = get_contest_by_path(path) contest_path = path or str(model.path) if not os.path.exists(contest_path): model.delete() return None conf = PleaseContest(contest_path).config model.name = conf.get("name", "") method = conf.get("id_method", "") if method: for id in Contest.ID_METHODS: if id[1] == method: model.id_method = id[0] break model.statement_name = conf['statement'].get("name", "") model.statement_location = conf['statement'].get("location", "") model.statement_date = conf['statement'].get("date", "") model.statement_template = conf['statement'].get("template", "contest.tex") model.save() order = 0 ContestProblem.objects.filter(contest=model).delete() for problem in conf['problem']: ContestProblem(problem=Problem.objects.get(path=norm( os.path.join(os.path.dirname(model.path), problem['path']))), contest=model, id_in_contest=problem['id'], order=order).save() order += 1 a = Contest.objects.get(id=1) return model