def test_get_problem_by_objective(self): coder = Coder(user=self.user) coder.save() problem = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_6_B", objective=Problem.ProblemObjective.UM) problem.save() response = self.client.get("/api/problem/objective/") self.assertEqual(response.status_code, 200) response = self.client.post("/api/problem/objective/", {}) self.assertEqual(response.status_code, 405)
def get(self, request, remote_oj, remote_id, **kwargs): if remote_oj not in list({item.oj_name for item in Support.objects.filter(oj_enable=True)}): return Response( res_format('remote_oj not valid', status=Message.ERROR), status=status.HTTP_200_OK) if not remote_id.isalnum(): return Response( res_format('remote_id not valid', status=Message.ERROR), status=status.HTTP_200_OK) if request.GET.get('fresh') and request.GET.get('html'): return Response(res_format('\"fresh\" and \"html\" cannot exist together', Message.ERROR), status=status.HTTP_200_OK) if request.GET.get('fresh'): last_submit_time = request.session.get('last_fresh_time', None) if last_submit_time and (datetime.now() - datetime.fromtimestamp(last_submit_time)).seconds < 5: return Response(res_format("Cannot fresh within five seconds", status=Message.ERROR), status=status.HTTP_200_OK) request.session['last_fresh_time'] = datetime.now().timestamp() try: problem = Problem.objects.get(remote_oj=remote_oj, remote_id=remote_id) problem.request_status = config.Problem.Status.STATUS_PENDING.value problem.save() get_problem_task.delay(problem.id) return Response(res_format(ProblemSerializer(problem).data), status=status.HTTP_200_OK) except ObjectDoesNotExist: return Response(res_format('System error', Message.ERROR), status=status.HTTP_200_OK) if request.GET.get('html'): try: problem = Problem.objects.get(remote_oj=remote_oj, remote_id=remote_id) return HttpResponse(problem.html) except: return Response(res_format('', status=Message.ERROR)) try: problem = Problem.objects.get(remote_oj=remote_oj, remote_id=remote_id) if problem.request_status == config.Problem.Status.STATUS_RETRYABLE.value: get_problem_task.delay(problem.id) except ObjectDoesNotExist: try: problem = Problem(remote_oj=remote_oj, remote_id=remote_id, request_status=config.Problem.Status.STATUS_PENDING.value) problem.save() get_problem_task.delay(problem.id) except IntegrityError: return Response(res_format('System error', Message.ERROR), status=status.HTTP_200_OK) return Response(res_format(ProblemSerializer(problem).data), status=status.HTTP_200_OK)
def update_uva_problems(): url = "https://uhunt.onlinejudge.org/api/p" res = requests.get(url) data = res.json() for p in data: if len(Problem.objects.filter(prob_id=p[0])) > 0: continue new_problem = Problem() new_problem.prob_id = p[0] new_problem.index = p[1] new_problem.name = p[2] dacu = p[3] wa = p[16] if dacu != 0: rating = min(3600, max(800, (20 - min(log2(dacu), 20)) * 200)) new_problem.rating = str(int(rating)) new_problem.difficulty = rating_to_difficulty(rating) new_problem.platform = 'U' new_problem.url = "https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=" + str( p[0]) new_problem.save() url = "https://uhunt.onlinejudge.org/api/cpbook/3" res = requests.get(url) data = res.json() for p in data: title1 = p['title'] for q in p['arr']: title2 = q['title'] for r in q['arr']: title3 = r[0] for i in range(1, len(r)): prob_num = abs(r[i]) tags = [title1, title2, title3] Problem.objects.filter(index=prob_num).update(tags=tags)
def mutate(self, info: ResolveInfo, **kwargs): form = CreateProblemForm(kwargs) if form.is_valid(): values = form.cleaned_data samples = loads(values.get('samples')) prob = Problem() limitation = Limitation() assign(prob, **values) assign(limitation, **values) limitation.save() prob.limitation = limitation prob.save() for each in samples: ProblemSample( input_content=each.get('inputContent'), output_content=each.get('outputContent'), problem=prob ).save() return CreateProblem(slug=prob.slug) else: raise RuntimeError(form.errors.as_json())
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 test_get_problem_input(self): problem = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_6_B", objective=1) problem.save() problem_input_1 = ProblemInput(problem=problem, content="input str 1") problem_input_1.save() problem_input_2 = ProblemInput(problem=problem, content="input str 2") problem_input_2.save() problem_input_3 = ProblemInput(problem=problem, content="input str 3") problem_input_3.save() problem_output_1 = ProblemOutput(problem_input=problem_input_1, content="output str a") problem_output_1.save() problem_output_2 = ProblemOutput(problem_input=problem_input_2, content="output str b") problem_output_2.save() problem_output_3 = ProblemOutput(problem_input=problem_input_3, content="output str c") problem_output_3.save() problem_id = problem.to_dict()['id'] response = self.client.get(f"/api/problem/{problem_id}/input/") self.assertEqual(response.status_code, 200) expected_response = json.dumps( get_dicts_with_filter(ProblemInput.objects, problem__id=problem_id)) self.assertEqual(response.content.decode(), expected_response) response = self.client.get("/api/problem/1000/input/") self.assertEqual(response.status_code, 400) response = self.client.post("/api/problem/1/input/", {}) self.assertEqual(response.status_code, 405)
def test_get_problem_output(self): problem = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_6_B", objective=1) problem.save() problem_input = ProblemInput(problem=problem, content=["1", "2", "3"]) problem_input.save() problem_output = ProblemOutput(problem_input=problem_input, content=["1", "2", "3"]) problem_output.save() problem_input_id = problem.to_dict()['id'] response = self.client.get(f"/api/problem/{problem_input_id}/output/") self.assertEqual(response.status_code, 200) response = self.client.post("/api/problem/1/output/", {}) self.assertEqual(response.status_code, 405)
def test_solution_create_exception(self): problem = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_6_B", objective=1) problem.save() problem_id = problem.to_dict()['id'] response = self.client.post( f"/api/problem/{problem_id}/solution/", json.dumps({}), content_type="application/json", ) self.assertEqual(response.status_code, 400) response = self.client.post("/api/problem/2/solution/", {}) self.assertEqual(response.status_code, 400) response = self.client.delete("/api/problem/2/solution/") self.assertEqual(response.status_code, 405)
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, }
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 codeforces_update_problems(): # check whether we have updated the problems of a particular contest , # if no , update the problems , else not .. url = "https://codeforces.com/api/contest.list" res = requests.get(url) data = res.json() if (data["status"] != 'OK'): return for codeforces_contest in data['result']: url = "https://codeforces.com/api/contest.standings?contestId=" + str( codeforces_contest['id']) + "&from=1&count=1" res = requests.get(url) data = res.json() if (data["status"] != 'OK'): continue new_contest = contest() if 'startTimeSeconds' in codeforces_contest: new_contest.startTime = codeforces_contest['startTimeSeconds'] new_contest.Type = 'R' new_contest.contestId = codeforces_contest['id'] new_contest.name = codeforces_contest['name'] new_contest.duration = codeforces_contest['durationSeconds'] if len(contest.objects.filter( contestId=codeforces_contest['id'])) == 0: new_contest.save() for contest_problem in data['result']['problems']: new_problem = Problem() new_problem.name = contest_problem['name'] new_problem.contest_id = contest_problem['contestId'] new_problem.prob_id = str( contest_problem['contestId']) + contest_problem['index'] new_problem.url = "https://codeforces.com/contest/" + str( contest_problem['contestId'] ) + "/problem/" + contest_problem['index'] new_problem.platform = 'F' new_problem.index = contest_problem['index'] new_problem.tags = contest_problem['tags'] if 'rating' in contest_problem: new_problem.rating = contest_problem['rating'] new_problem.difficulty = rating_to_difficulty( int(contest_problem['rating'])) if len( Problem.objects.filter( prob_id=str(contest_problem['contestId']) + contest_problem['index'])) == 0: new_problem.save() url = "https://codeforces.com/api/contest.list?gym=true" res = requests.get(url) data = res.json() if (data["status"] != 'OK'): return for codeforces_contest in data['result']: url = "https://codeforces.com/api/contest.standings?contestId=" + str( codeforces_contest['id']) + "&from=1&count=1" res = requests.get(url) data = res.json() if (data["status"] != 'OK'): continue new_contest = contest() if 'startTimeSeconds' in codeforces_contest: new_contest.startTime = codeforces_contest['startTimeSeconds'] new_contest.Type = 'R' new_contest.contestId = codeforces_contest['id'] new_contest.name = codeforces_contest['name'] new_contest.duration = codeforces_contest['durationSeconds'] if len(contest.objects.filter( contestId=codeforces_contest['id'])) == 0: new_contest.save() for contest_problem in data['result']['problems']: new_problem = Problem() new_problem.name = contest_problem['name'] new_problem.contest_id = contest_problem['contestId'] new_problem.prob_id = str( contest_problem['contestId']) + contest_problem['index'] new_problem.url = "https://codeforces.com/gym/" + str( contest_problem['contestId'] ) + "/problem/" + contest_problem['index'] new_problem.platform = 'F' new_problem.index = contest_problem['index'] new_problem.tags = contest_problem['tags'] if 'rating' in contest_problem: new_problem.rating = contest_problem['rating'] new_problem.difficulty = rating_to_difficulty( int(contest_problem['rating'])) if len( Problem.objects.filter( prob_id=str(contest_problem['contestId']) + contest_problem['index'])) == 0: new_problem.save() return
def test_other_solutions(self): client = Client() user = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) user_id = User.objects.filter(email="*****@*****.**").first().id client.login(username="******", password="******") problem1 = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_6_B", objective=1) problem1.save() problem1_id = problem1.to_dict()['id'] problem2 = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ALDS1_4_B", objective=3) problem2.save() problem2_id = problem2.to_dict()['id'] problem3 = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_7_B", objective=1) problem3.save() problem3_id = problem3.to_dict()['id'] solution1_body = { "erase_cnt": 12, "elapsed_time": 30, "evaluation": 66.0, "code": "n=int(input())\na=[[0 for i in range(13)]for j in range(4)]\nfor k in range(n):\ncard=input().split()\np=5\nif (card[0]=='S'):\np=0\nelif (card[0]=='H':\np=1\nelif (card[0]=='C'):\np=2\nelse:\np=3\nq=int(card[1])-1\na[p][q]=1\nfor i in range(4):\nfor j in range(13):\nif a[i][j]==0:\ntype=''\nif i==0 :\ntype='S'\nelif i==1:\ntype='H'\nelif i==2:\ntype='C'\nelse:\ntype='D'\nprint('{0} {1}'.format(type,j+1))", } response = client.post( f"/api/problem/{problem1_id}/solution/", json.dumps(solution1_body), content_type="application/json", ) self.assertEqual(response.status_code, 201) solution2_body = { "erase_cnt": 12, "elapsed_time": 30, "evaluation": 66.0, "code": "n=int(input())\na=[[0 for i in range(13)]for j in range(4)]\nfor k in range(n):\ncard=input().split()\np=5\nif (card[0]=='S'):\np=0\nelif (card[0]=='H':\np=1\nelif (card[0]=='C'):\np=2\nelse:\np=3\nq=int(card[1])-1\na[p][q]=1\nfor i in range(4):\nfor j in range(13):\nif a[i][j]==0:\ntype=''\nif i==0 :\ntype='S'\nelif i==1:\ntype='H'\nelif i==2:\ntype='C'\nelse:\ntype='D'\nprint('{0} {1}'.format(type,j+1))", } response = client.post( f"/api/problem/{problem2_id}/solution/", json.dumps(solution2_body), content_type="application/json", ) self.assertEqual(response.status_code, 201) solution3_body = { "erase_cnt": 12, "elapsed_time": 30, "evaluation": 66.0, "code": "n=int(input())\na=[[0 for i in range(13)]for j in range(4)]\nfor k in range(n):\ncard=input().split()\np=5\nif (card[0]=='S'):\np=0\nelif (card[0]=='H':\np=1\nelif (card[0]=='C'):\np=2\nelse:\np=3\nq=int(card[1])-1\na[p][q]=1\nfor i in range(4):\nfor j in range(13):\nif a[i][j]==0:\ntype=''\nif i==0 :\ntype='S'\nelif i==1:\ntype='H'\nelif i==2:\ntype='C'\nelse:\ntype='D'\nprint('{0} {1}'.format(type,j+1))", } response = client.post( f"/api/problem/{problem3_id}/solution/", json.dumps(solution3_body), content_type="application/json", ) self.assertEqual(response.status_code, 201) response = client.post("/api/analysis/my/report/") self.assertEqual(response.status_code, 204) User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) client = Client() client.login(username="******", password="******") response = client.get(f"/api/analysis/other/{user_id}/solutions/") self.assertEqual(response.status_code, 200)