def get(self, request, prob_id): data = {'status': 200, 'msg': '成功获取题目', 'data': {}} try: # 获取问题 prob_no = str(prob_id) problem = Problem.objects.get(no=prob_no, probAuthority='公开') data["data"].update({"problem": problem.get_problem()}) print(prob_no) code = '' language = 'C' # 如果用户登录 则获取用户最新一次提交代码 is_Login, user = check_auth(request._request) try: problem_Submit = ProblemSubmit.objects.filter( problem__no=prob_no, user__id=user.id).last() language = problem_Submit.language code = problem_Submit.content except: pass data["data"].update({"language": language, "code": code}) except: data['msg'] = '题目不存在' data['status'] = 400 return JsonResponse(data=data)
def get(request, match_id): # 比赛包含的题目 data = {'status': 200, 'msg': '成功获取比赛题目列表', 'data': {}} try: match = Match.objects.get(id=match_id) except ObjectDoesNotExist: data.update({'status': 400, 'msg': '竞赛不存在'}) return JsonResponse(data) except MultipleObjectsReturned: data.update({'status': 400, 'msg': '竞赛错误'}) return JsonResponse(data) is_login, user = check_auth(request) t = match match = get_data(obj=match, serializer=MatchSerializer, dataList=[]) if match['status'] == 1: data.update({'is_start': False, 'contest': match}) elif match['status'] > 1: problems = get_data(obj=t.matchinclude_set.all(), serializer=MatchIncludeSerializer, dataList=[], many=True) exec_problems = is_accepted(t, user, problems) # 包含对于当前用户是否已经通过 data.update({ 'is_start': True, 'contest': match, 'problems': exec_problems }) return JsonResponse(data)
def post(self, request): # 创一份ProblemSubmit data = {'status': 200, 'msg': '提交成功', 'data': {}} is_Login, user = check_auth(request._request) if is_Login == False: return JsonResponse(data.update({'status': 400, 'msg': '未登录'})) if request._request.is_ajax(): prob_no = request.POST.get('prob_no', '') try: prob_no = int(prob_no) except: data['status'] = 400 data['msg'] = 'Error: Unknown problem submit' return JsonResponse(data) if 1000 > prob_no or prob_no > Problem.objects.last().no: data['status'] = 400 data['msg'] = 'Error: Unknown problem submit' return JsonResponse(data) # 对于防止恶意提交空语言, 进行判断提交语言是否符合规定 language = request.POST.get('language', 'C') if not re.search('[C|C++|Java|Python]', language): language = 'C' # 获取提交代码 content = request.POST.get('editor', '') if content != '': prob = Problem.objects.get(no=prob_no) statuses = ProblemSubmit.objects.create( user=user, problem=prob, content=content, result="0", language=language, ) prob.submit_nums += 1 flag = 0 while statuses.result == '0': time.sleep(0.7) # 如果还在判题,那就等0.7秒以后重新查询,直到结果出来后,再返回数据 if flag >= 10: time.sleep(0.6) if flag > 35: data['status'] = 400 data['msg'] = 'Error: Server busy, try again later' return JsonResponse(data) statuses = ProblemSubmit.objects.filter( problem__no=prob_no, user__id=user.id).last() flag += 1 if statuses.result == '1': prob.ac_nums += 1 prob.ratio = round(float(prob.ac_nums / prob.submit_nums), 3) * 100 prob.save() data['msg'] = 'Success: ' + str(statuses.result) return JsonResponse(data) else: # 对于空代码提交, 向ajax返回错误信息 data['status'] = 400 data['msg'] = 'Error: Your code is empty!' return JsonResponse(data)
def get(request): contest_Id = request.GET.get('contest_Id', '') contest = Match.objects.get(id=contest_Id) data = {'status': 200, 'msg': '报名成功'} is_Login, user = check_auth() if is_Login: matchRank = MatchRank(match=contest, user=user) matchRank.save() return JsonResponse(data) else: return
def get(self, request): # 按(id|名字|状态)查询第page页的竞赛列表 data = {'status': 200, 'msg': '成功获取竞赛列表', 'data': {}} match_Id_Name = request.GET.get('match_id_name', '') # 默认值为'' query_Criteria = {'attribute': '公开'} # 创建一个多条件查询字典 if match_Id_Name != '': try: query_Criteria['id'] = int(match_Id_Name) except: query_Criteria['matchName__regex'] = '.*'.join(match_Id_Name) contest = list(reversed(Match.objects.filter(**query_Criteria))) is_Logion, user = check_auth(request._request) register_status(contest, user) # 判断用户对竞赛列表的注册情况 # 暂未优化 try: match_Status = int(request.GET.get('match_status', 0)) except: match_Status = 0 if match_Status <= 3 and match_Status >= 0: temp = [] for con in contest: if con.status == match_Status or (match_Status == 1 and con.status == 0) or match_Status == 0: temp.append(con) contest = temp # 竞赛列表分页&序列化 paginator_OfContestAll = Paginator(contest, 10) page_num = request.GET.get('page', 1) paginator_OfContestAll = paginator_OfContestAll.page(page_num) paginator_OfContestAll = paginator_OfContestAll.object_list paginator_OfContestAll = MatchSerializer(paginator_OfContestAll, many=True).data data["data"].update({ 'contest': paginator_OfContestAll, 'now_page': page_num, 'match_status': match_Status, 'match_id_name': match_Id_Name }) # if request.GET.get('alr_reg', '') != '': # ret_dir['alr_reg'] = 'You have registered for the competition.' # reg_suc = request.GET.get('reg_suc', '') # if reg_suc != '': # if reg_suc == '1': # ret_dir['reg_info'] = 'registered successfully.' # elif reg_suc == '0': # ret_dir['reg_info'] = 'The registration time has passed.' return JsonResponse(data)
def get(request, match_id): # 获取题目的提交人数&通过人数&通过率&用户的通过情况 data = {'status': 200, 'msg': '成功获取比赛题目列表'} try: match = Match.objects.get(id=match_id) # 尝试获取比赛,获取不到就不做处理 except ObjectDoesNotExist: data.update({'status': 404, 'msg': '竞赛不存在'}) return JsonResponse(data) problems = get_data(obj=match.matchinclude_set.all(), serializer=MatchIncludeSerializer, dataList=[], many=True) is_login, user = check_auth(request) exec_problems = is_accepted(match, user, problems) # 包含对于当前用户是否已经通过 data.update({'problems': exec_problems}) return JsonResponse(data)
def get(self, request): #按(id|名字|类型|来源)查询第page页的问题列表 problem_Id_Name = request.GET.get('problem_Id_Name', '') problem_Source = request.GET.get('problem_Source', 'All') algorithm_Type = request.GET.get('algorithm_Type', 'All') query_Criteria = {} # 创建一个多条件查询字典 query_Criteria['probAuthority'] = '公开' if problem_Id_Name != '': try: # 尝试将参数作为数值处理,并当作id搜索,如果不行,就转化为name进行搜索 query_Criteria['no'] = int(problem_Id_Name) except: query_Criteria['title__iregex'] = '.*'.join(problem_Id_Name) if problem_Source != '' and problem_Source != 'All': query_Criteria['probSource'] = problem_Source if algorithm_Type != '' and algorithm_Type != 'All': query_Criteria['classification'] = algorithm_Type problems = Problem.objects.filter(**query_Criteria) # 分页处理 paginator_OfProblemsAll = Paginator(problems, 20) page_Num = request.GET.get('page', 1) paginator_OfProblemsAll = paginator_OfProblemsAll.page(page_Num) paginator_OfProblemsAll = paginator_OfProblemsAll.object_list paginator_OfProblemsAll = ProblemListSerializer( paginator_OfProblemsAll, many=True).data is_Login, user = check_auth(request._request) ac_Flag = is_accepted(user, paginator_OfProblemsAll) # 将这些数据返回到前端,这样便于进行多条件过滤 data = {'status': 200, 'msg': '已获取所有问题', 'data': {}} data["data"].update({ 'problems': paginator_OfProblemsAll, 'ac_flag': ac_Flag, 'now_page': page_Num, 'problem_id_name': problem_Id_Name, 'problem_source': problem_Source, 'algorithm_type': algorithm_Type }) return JsonResponse(data)
def get(request): # 按(id|名字|类型|来源)查询第page页的问题列表 problem_Id_Name = request.GET.get('problem_Id_Name', '') problem_Source = request.GET.get('problem_Source', 'All') algorithm_Type = request.GET.get('algorithm_Type', 'All') query_Criteria = {'probAuthority': '公开'} # 创建一个多条件查询字典 if problem_Id_Name != '': try: # 尝试将参数作为数值处理,并当作id搜索,如果不行,就转化为name进行搜索 query_Criteria['no'] = int(problem_Id_Name) except ValueError: query_Criteria['title__iregex'] = '.*'.join(problem_Id_Name) if problem_Source != '' and problem_Source != 'All': query_Criteria['probSource'] = problem_Source if algorithm_Type != '' and algorithm_Type != 'All': query_Criteria['classification'] = algorithm_Type problems = Problem.objects.filter(**query_Criteria).order_by('-no') # 分页处理 dataList = ["no", "title", 'ac_nums', 'submit_nums'] paginator_of_problems_all = Paginator(problems, 20) page_num = request.GET.get('page', 1) paginator_of_problems_all = paginator_of_problems_all.page(page_num) paginator_of_problems_all = paginator_of_problems_all.object_list paginator_of_problems_all = get_data(obj=paginator_of_problems_all, serializer=ProblemSerializer, dataList=dataList, many=True) is_login, user = check_auth(request) exec_problems = is_accepted(user, paginator_of_problems_all) # 将这些数据返回到前端,这样便于进行多条件过滤 data = {'status': 200, 'msg': '已获取所有问题'} data.update({ 'problems': exec_problems, 'now_page': page_num, 'problem_id_name': problem_Id_Name, 'problem_source': problem_Source, 'algorithm_type': algorithm_Type }) return JsonResponse(data)
def get(self, request): data = {'status': 200, 'msg': '获取成功', 'data': {}} is_Login, user = check_auth(request._request) if is_Login == False: data["data"].update({ 'status': 400, 'msg': '未登录', }) return JsonResponse(data) username = request.GET.get("username", "") if username != user.username: data["data"].update({ 'status': 400, 'msg': '不同用户', }) return JsonResponse(data) problem_Submit_Id = request.GET.get("problem_Submit_Id", "") print(problem_Submit_Id) problem_Submit = ProblemSubmit.objects.get(runID=problem_Submit_Id) problem_Submit = ProblemSubmitSerializer(problem_Submit).data data["data"] = {"problem_Submit": problem_Submit} return JsonResponse(data)
def get(request): # 按(id|名字|状态)查询第page页的竞赛列表 data = {'status': 200, 'msg': '成功获取竞赛列表'} contest_status = request.GET.get('contest_status', None) contest = Match.objects.filter().order_by('-id') a = datetime.now() if contest_status is not None and contest_status != '4': if contest_status == '1': contest = contest.filter(startTime__gt=a) elif contest_status == '2': contest = contest.extra(where=[ "startTime < '%s' and '%s' < datetime(startTime, '+'||howLong||' minute', 'localtime')" % (a, a) ]) elif contest_status == '3': contest = contest.extra(where=[ "datetime(startTime, '+'||howLong||' minute', 'localtime') < '%s'" % a ]) is_login, user = check_auth(request) paginator_of_contest_all = Paginator(contest, 10) page_num = request.GET.get('page', 1) paginator_of_contest_all = paginator_of_contest_all.page(page_num) paginator_of_contest_all = paginator_of_contest_all.object_list paginator_of_contest_all = get_data(obj=paginator_of_contest_all, serializer=MatchSerializer, dataList=[ 'owner', 'info', 'id', 'startTime', 'registerNum', 'howLong', 'matchName' ], context={'request': request}, many=True) data.update({ 'contest': paginator_of_contest_all, 'now_page': page_num, }) return JsonResponse(data)
def get(self, request, match_id): # 获取题目的提交人数&通过人数&通过率&用户的通过情况 data = {'status': 200, 'msg': '成功获取比赛题目列表', 'data': {}} if request._request.is_ajax(): try: contest = Match.objects.get(id=match_id) # 尝试获取比赛,获取不到就不做处理 except: data.update({'status': 400, 'msg': '竞赛不存在'}) return JsonResponse(data) problems = MatchIncludeSerializer(contest.matchinclude_set.all, many=True).data ratio_List = ratio(contest, problems) # 通过率 ac_Nums = problem_ac_nums(contest, problems) # 通过人数 submit_Nums = problem_submit_nums(contest, problems) # 提交人数 is_Login, user = check_auth(request._request) ac_List = is_accepted(contest, user, problems) # 用户通过情况 data["data"] = { 'ratio_list': ratio_List, 'ac_nums': ac_Nums, 'submit_nums': submit_Nums, 'ac_list': ac_List } return JsonResponse(data)
def post(request, match_id, problem_id): data = {'status': 200, 'msg': '提交成功'} is_login, user = check_auth(request) if not is_login: data.update({'status': 401, 'msg': '未登录'}) return JsonResponse(data) contest = Match.objects.filter(id=match_id) if contest.exists(): contest = contest[0] else: data.update({'status': 400, 'msg': '竞赛不存在'}) return JsonResponse(data) # 获取时间 进行是否还在比赛的判定 # time1 = contest.startTime.replace(tzinfo=None) # 比赛开始时间 # time2 = datetime.now() # 当前系统时间 # time3 = time1 + timedelta(minutes=int(contest.howLong)) # 比赛结束时间 # # # 如果还未开始, 就重定向到比赛开始页面 # if time1 > time2: # data.update({'status': 400, 'msg': 'Error: contest not start'}) # return JsonResponse(data) # elif time2 > time3: # # 如果比赛已经结束, 向前端ajax提交处返回错误信息 # data.update({'status': 400, 'msg': 'Error: contest is over'}) # return JsonResponse(data) # 对于非法提交, 进行判断该用户是否注册了比赛, 没有就重定向到比赛列表页面 # if not MatchRegister.objects.filter(match__id=match_id, user__username=user.username).exists(): # data.update({'status': 400, 'msg': '未注册比赛'}) # return JsonResponse(data) # 对于防止恶意提交空语言, 进行判断提交语言是否符合规定 language = request.data['language'] print(language) if not re.search('[C|C++|Java]', language): language = 'C' # 获取提交代码 content = request.data['code'] if content != '': # 创建提交记录 problem = Problem.objects.filter(no=problem_id) if problem.exists(): problem = problem[0] else: data.update({'msg': '题目错误'}) return JsonResponse(data) statuses = MatchSubmit.objects.create( match=contest, user=user, problem=problem, content=content, result="0", language=language, ) flag = 1 # 进行循环查询题目结果 while statuses.result == '0': time.sleep(1) # 如果还在判题,那就等一秒以后重新查询,直到结果出来后,再返回数据 if flag > 10: time.sleep(1) if flag > 35: data.update({ 'status': 400, 'msg': 'Error: Server busy, try again later' }) return JsonResponse(data) statuses = MatchSubmit.objects.get(runID=statuses.runID) flag += 1 if not MatchSubmit.objects.filter(match__id=match_id, user__id=user.id, problem__no=problem_id, result='1').exists(): rank = MatchRank.objects.get(match__id=match_id, user__id=user.id) problem = MatchInclude.objects.get(match__id=match_id, problem_id=problem_id) for i in rank.rank: if i['problem_no'] == problem.no: if statuses.result == '1': i['is_ac'] = True i['useTime'] = ( statuses.subTime - contest.startTime).total_seconds() * 1000 else: i['wrongTime'] += 1 data["data"].update({'result': statuses.result}) return JsonResponse(data) else: # 对于空代码提交, 向ajax返回错误信息 data.update({'status': 400, 'msg': 'Error: Your code is empty!'}) return JsonResponse(data)
def post(self, request): data = {'status': 200, 'msg': '获取成功', 'data': {}} is_Login, user = check_auth(request._request) if is_Login == False: data.update({'status': 400, 'msg': '未登录'}) return JsonResponse(data) if request._request.is_ajax(): contest_Id = request.POST.get('contest_Id', '') contest = Match.objects.filter(id=contest_Id) if contest.exists(): contest = contest[0] else: data.update({'status': 400, 'msg': '竞赛不存在'}) return JsonResponse(data) # 获取时间 进行是否还在比赛的判定 time1 = contest.startTime.replace(tzinfo=None) # 比赛开始时间 time2 = datetime.now() # 当前系统时间 time3 = time1 + timedelta(minutes=int(contest.howLong)) # 比赛结束时间 # 如果还未开始, 就重定向到比赛开始页面 if time1 > time2: data.update({'status': 400, 'msg': 'Error: contest not start'}) return JsonResponse(data) elif time2 > time3: # 如果比赛已经结束, 向前端ajax提交处返回错误信息 data.update({'status': 400, 'msg': 'Error: contest is over'}) return JsonResponse(data) # 对于非法提交, 进行判断该用户是否注册了比赛, 没有就重定向到比赛列表页面 if not MatchRegister.objects.filter( match__id=contest_Id, user__username=user.username).exists(): data.update({'status': 400, 'msg': '未注册比赛'}) return JsonResponse(data) prob_no = request.POST.get('prob_no', '') if contest_Id == '' or prob_no == '': data.update({'status': 400, 'msg': '未知错误'}) return JsonResponse(data) # 对于防止恶意提交空语言, 进行判断提交语言是否符合规定 language = request.POST.get('language', 'C') if not re.search('[C|C++|Java|Python]', language): language = 'C' # 获取提交代码 content = request.POST.get('editor', '') if content != '': # 创建提交记录 statuses = MatchSubmit.objects.create( match=contest, user=user, problem=Problem.objects.get(no=int(prob_no)), content=content, result="0", language=language, ) flag = 1 # 进行循环查询题目结果 while statuses.result == '0': time.sleep(1) # 如果还在判题,那就等一秒以后重新查询,直到结果出来后,再返回数据 if flag > 10: time.sleep(1) if flag > 35: data.update({ 'status': 400, 'msg': 'Error: Server busy, try again later' }) return JsonResponse(data) statuses = MatchSubmit.objects.filter( problem__no=prob_no, user__username=user.username).last() flag += 1 data["data"].update({'result': statuses.result}) return JsonResponse(data) else: # 对于空代码提交, 向ajax返回错误信息 data.update({ 'status': 400, 'msg': 'Error: Your code is empty!' }) return JsonResponse(data)