def showContestProblemStatistics(request, p_index=None, cid=None): logger.info(str(request).replace("\n", "\t")) """ view used to show statistics of a problem in a contest """ try: u = User.getSessionUser(request.session) if not u: raise Err(request, err='not login') cid = int(cid) c = Contest.getById(cid) p_index = p_index if p_index else '' try: contest_problem = ContestProblem.getBy(c, p_index) except: raise Err( request, err='no contestproblem', log_format=('{0}'.format(p_index), ''), user_format=(u'{0}'.format(p_index), u'搞错了什么吧!'), ) if not contest_problem.contest.course_class.canBeManaged(u): raise Err(request, err='no priv') all_submissions = Submission.submissionList(cp=contest_problem) submissions = all_submissions.filter( status='Accepted').order_by('run_time')[:20] status_list = [] for i in Const.STATUS_CN.iterkeys(): status_list.append({ 'name': Const.STATUS_CN[i], 'number': all_submissions.filter(status=i).count() }) for sub_s in submissions: sub_s.status_color = Const.STATUS_COLOR[ sub_s.status] if sub_s.status in Const.STATUS_COLOR else '' sub_s.status_cn = Const.STATUS_CN[sub_s.status] return render( request, 'newtpl/statistic/contest_problem.html', { 'submissions': submissions, 'contest_problem': contest_problem, 'status_list': status_list, 'tpl': { 'sp': True } }) except Exception as e: logger.error(str(e).replace("\n", "\t")) return render(request, Err.ERROR_PAGE, {'errmsg': unicode(e)})
def showContestProblem(request, c_id, idx): # modified logger.info(str(request).replace("\n", "\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') c_id = int(c_id) c = Contest.getById(c_id) cn = c.getContestNotice() cp = ContestProblem.getBy(c=c, idx=idx) try: c.canEnterContest(u) except: raise Err(request, 'no priv') can_manage = True try: c.canBeManaged(u) except: can_manage = False if (not c.canEnterWithTime(u)) and (not can_manage): raise Err(request, 'contest not started') c.course_class_name = unicode(c.course_class.getFullName()) p = cp.problem p.idx = cp.problem_index p.title = unicode(cp.problem_title) p.desc = json.loads(p.prob_desc) cp_list = c.getContestProblem() for cp, cp_res, cp_user_res in zip(cp_list, getContestResult(c=c), getContestUserResult(c=c, u=u)): cp.ac = cp_res['ac_cnt'] cp.sub = cp_res['sub_cnt'] cp.user_res = cp_user_res return render( request, "newtpl/problem/showContestProblem.html", { 'c': c, 'cp_list': cp_list, 'contest_notice_list': cn, 'p': p, 'status_query': queryString(problem_index=p.idx), 'tpl': { 'sp': True, 'can_manage': can_manage } }) except Exception as e: logger.error(str(e).replace("\n", "\t")) return render(request, Err.ERROR_PAGE)
def showContestProblem(request,c_id,idx): # modified logger.info(str(request).replace("\n","\t")) try: u = User.getSessionUser(request.session) if not u: raise Err(request, 'not login') c_id = int(c_id) c = Contest.getById(c_id) cn = c.getContestNotice() cp = ContestProblem.getBy(c=c, idx=idx) try: c.canEnterContest(u) except: raise Err(request, 'no priv') can_manage = True try: c.canBeManaged(u) except: can_manage = False if (not c.canEnterWithTime(u)) and (not can_manage): raise Err(request, 'contest not started') c.course_class_name = unicode(c.course_class.getFullName()) p = cp.problem p.idx = cp.problem_index p.title = unicode(cp.problem_title) p.desc=json.loads(p.prob_desc) cp_list = c.getContestProblem() for cp, cp_res, cp_user_res in zip(cp_list, getContestResult(c=c), getContestUserResult(c=c, u=u)): cp.ac = cp_res['ac_cnt'] cp.sub = cp_res['sub_cnt'] cp.user_res = cp_user_res return render(request, "newtpl/problem/showContestProblem.html", {'c':c, 'cp_list':cp_list,'contest_notice_list':cn, 'p':p, 'status_query':queryString(problem_index=p.idx), 'tpl':{'sp':True, 'can_manage':can_manage}}) except Exception as e: logger.error(str(e).replace("\n","\t")) return render(request, Err.ERROR_PAGE)
def showContestProblemStatistics( request, p_index=None, cid=None): logger.info(str(request).replace("\n","\t")) """ view used to show statistics of a problem in a contest """ try: u = User.getSessionUser(request.session) if not u: raise Err( request, err='not login') cid = int(cid) c = Contest.getById(cid) p_index = p_index if p_index else '' try: contest_problem = ContestProblem.getBy( c, p_index) except: raise Err( request, err='no contestproblem', log_format=( '{0}'.format(p_index), ''), user_format=( u'{0}'.format( p_index), u'搞错了什么吧!'), ) if not contest_problem.contest.course_class.canBeManaged( u): raise Err( request, err = 'no priv') all_submissions = Submission.submissionList( cp=contest_problem ) submissions = all_submissions.filter( status='Accepted').order_by('run_time')[:20] status_list = [] for i in Const.STATUS_CN.iterkeys(): status_list.append( { 'name': Const.STATUS_CN[i], 'number': all_submissions.filter( status=i).count()} ) for sub_s in submissions: sub_s.status_color = Const.STATUS_COLOR[sub_s.status] if sub_s.status in Const.STATUS_COLOR else '' sub_s.status_cn = Const.STATUS_CN[ sub_s.status] return render( request, 'newtpl/statistic/contest_problem.html', { 'submissions': submissions, 'contest_problem': contest_problem, 'status_list': status_list, 'tpl': { 'sp': True } }) except Exception as e: logger.error(str(e).replace("\n","\t")) return render(request, Err.ERROR_PAGE, { 'errmsg': unicode(e) } )
def submissionList(request, contest_id=None, page_id='1'): logger.info(str(request).replace("\n\r", "\t")) """ @view: list submission of some contest """ try: page_id = int(page_id) u = User.getSessionUser(request.session) if not u: raise Err(request, err='not login') cid = int(contest_id) try: c = Contest.getById(cid) except: raise Err( request, err='no contest', log_format=('{0}'.format(cid), ''), user_format=(u'{0}'.format(cid), u'别做坏事!'), ) c.course_class_name = unicode(c.course_class.getFullName()) c.description = unicode(c.contest_description) c.title = unicode(c.contest_title) now = datetime.now() if c.start_time + timedelta(minutes=c.length) < now: c.status = 'ended' elif c.start_time > now: c.status = 'scheduled' else: c.status = 'running' c.time_passed = min( max(int((now - c.start_time).total_seconds()) / 60, 0), c.length) c.time_passed_percent = 100 * c.time_passed / c.length c.time_left = c.length - c.time_passed isManager = c.course_class.canBeManaged(u) user = u if not isManager else None # privilege for showing all submissions idxList = [(cp.problem_index, cp.problem_index) for cp in c.getContestProblem()] langList = [('gcc', 'GNU C'), ('g++', 'GNU C++'), ('java', 'java')] form = submissionListForm(idxList, langList, request.GET) if form.is_valid(): if form.cleaned_data['problem_index']: try: contestProb = ContestProblem.getBy( c, form.cleaned_data['problem_index']) except: contestProb = None # raise Exception(u'contest problem not found') else: contestProb = None else: raise Err(request, err='example err', log_format=('form invalid', ''), user_format=(u'输入的内容不合法', '')) #sub_all_c = getSUB() #submissions = bigfilter( u=user, c=c, cp=contestProb, uname=form.cleaned_data['username'], lang=form.cleaned_data['language'], sta=form.cleaned_data['status']) submissions = Submission.submissionList( u=user, c=c, cp=contestProb, uname=form.cleaned_data['username'], lang=form.cleaned_data['language'], sta=form.cleaned_data['status']) submissions = Submission.submissionList( u=user, c=c, cp=contestProb, uname=form.cleaned_data['username'], lang=form.cleaned_data['language'], sta=form.cleaned_data['status']) if isManager and 'rejudge' in request.GET and c.board_type != 2: map(lambda x: Submission.rejudgeSubmission(x), submissions) paginator = Paginator(submissions, Const.STATUS_PER_PAGE) page_id = min(max(int(page_id), 1), paginator.num_pages) s = paginator.page(page_id) for sub_s in s: sub_s.status_color = Const.STATUS_COLOR[ sub_s.status] if sub_s.status in Const.STATUS_COLOR else '' sub_s.status_cn = Const.STATUS_CN[sub_s.status] return render( request, 'newtpl/submission/status.html', { 'sList': s, 'form': form, 'c': c, 'tpl': { 'can_manage': True if isManager else False } }) #return render(request, 'newtpl/submission/status.html', {'sList':s, 'form':form, 'c':c}) except Exception as e: logger.error(str(e).replace("\n\r", "\t")) return render(request, Err.ERROR_PAGE, {'errmsg': unicode(e)})
def addSubmission(request, contest_id=None, problem_index=None): logger.info(str(request).replace("\n\r", "\t")) """ view used to add submission """ try: contest_id = int(contest_id) u = User.getSessionUser(request.session) if not u: raise Err(request, err='not login') if not contest_id: raise Err( request, err='request err', log_format=('contest id', 'UNDEFINED'), user_format=(u'考试编号', u'你吃了么!!!'), ) elif not problem_index: raise Err( request, err='request err', log_format=('problem index', 'UNDEFINED'), user_format=(u'题目序号', u'哪里去了!!'), ) else: try: c = Contest.getById(contest_id) except: raise Err( request, err='no contest', log_format=('{0}'.format(cid), ''), user_format=(u'{0}'.format(cid), u'别做坏事!'), ) try: p_index = ContestProblem.getBy(c=c, idx=problem_index) except: raise Err( request, err='no contest problem', log_format=('{0}'.format(problem_index), ''), user_format=(u'{0}'.format(problem_index), u'别乱写好么!!'), ) p = p_index.problem if not Submission.canSubmitCode(u, p_index): raise Err( request, err='submit err', log_format=( 'no priv to submit', 'contest ended or no priv to attend the contest'), user_format=(u'您没有提交该题的权限', u'考试已结束或者您没有参加本场考试的权限')) lang_list = [] if c.lang_limit & 1 == 1: lang_list.append(('gcc', 'GNU C')) if (c.lang_limit >> 1) & 1 == 1: lang_list.append(('g++', 'GNU C++')) if (c.lang_limit >> 2) & 1 == 1: lang_list.append(('java', 'JAVA')) if request.method == 'POST': form = addSubmissionForm(lang_list, request.POST) if form.is_valid(): sub_name = str(datetime.now()) time_str = sub_name.split('.') time_str.pop() for i in ['-', ':', '.', ' ']: sub_name = sub_name.replace(i, '_') import os code_file_path = os.path.join( Const.SUBMISSION_TMP_PATH, "{0:010d}_{1}".format(u.uid, sub_name)) code_length = 0 # head_details: details of the submission added at the head of the code file head_details = '' head_details += '/*\n' head_details += 'USER_ID: ' + str(u) + '\n' head_details += 'PROBLEM: ' + str(p.pid) + '\n' head_details += 'SUBMISSION_TIME: ' + time_str[0] + '\n' head_details += '*/\n' if 'code_file' in request.FILES: default_storage.save(code_file_path, request.FILES['code_file']) else: if form.cleaned_data['code']: default_storage.save( code_file_path, ContentFile(head_details + form.cleaned_data['code'])) else: raise Err( request, err='request err', log_format=('code', 'no input'), user_format=(u'代码呢!', u'不写代码交什么交!!'), ) code_length = default_storage.size(code_file_path) sub_lang = form.cleaned_data['language'] if sub_lang not in map(lambda x: x[0], lang_list): raise Err( request, err='illegal language', log_format=('{0}'.format(sub_lang), 'blabla'), user_format=(u'{0}'.format(sub_lang), u'别瞎搞成不!!'), ) x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') additional = {'submit_ip': ip} Submission.addSubmission(u, p_index, code_file_path, sub_lang, code_length, additional, c.board_type == 2) return redirect('Submission:contest_status', contest_id=contest_id) # could it be okay? else: raise Exception(u'form invalid') # the usage of url.name in urls.py # not POST method else: form = addSubmissionForm(lang_list) return render( request, 'newtpl/submission/submit.html', { 'form': form, 'tpl': { 'sp': True }, 'contest': c, 'cp': p_index }, ) # 'test_info': form.cleaned_data['language']}) except Exception as e: logger.error(str(e).replace("\n\r", "\t")) return render( request, Err.ERROR_PAGE, { 'errmsg': unicode(e), }, )
def showResult(request, cid, page='1'): try: u = User.getSessionUser(request.session) if not u: raise Err(request, err='not login') c = Contest.getById(int(cid)) try: c.canBeManaged(u) except: raise Err(request, err='no priv') if ('threshold' in request.GET) and request.GET['threshold']: threshold = float(request.GET['threshold']) else: threshold = Const.CHEAT_DEFAULT_THRESHOLD #------------form------------- idxList = [(cp.problem_index, cp.problem_index) for cp in c.getContestProblem()] langList = [('gcc','GNU C'), ('g++','GNU C++'), ('java','java')] form = submissionListForm(idxList, langList, request.GET) if form.is_valid(): if form.cleaned_data['problem_index']: try: contestProb = ContestProblem.getBy( c, form.cleaned_data['problem_index']) except: contestProb=None # raise Exception(u'contest problem not found') else: contestProb=None else: raise Err( request, err='example err', log_format=( 'form invalid', ''), user_format=( u'输入的内容不合法', '') ) #------------form-------------- cheatList = Cheat.getCheatList(contest=c, threshold=0) #------------filter------------ if contestProb: cheatList = cheatList.filter(contest_problem=contestProb) if form.cleaned_data['username']: cheatList1 = cheatList.filter(sub1__user__username__icontains=form.cleaned_data['username']) cheatList2 = cheatList.filter(sub2__user__username__icontains=form.cleaned_data['username']) cheatList = cheatList1 | cheatList2 #------------filter------------ paginator = Paginator(cheatList, Const.CHEAT_PER_PAGE) page = min(max(int(page), 1), paginator.num_pages) cl = paginator.page(page) ipa = [] ipb = [] for idx, element in enumerate(cl): info_a = eval(element.sub1.other_info) info_b = eval(element.sub2.other_info) #cl[idx] = {'c': element, 'ip_a': info_a['submit_ip'], 'ip_b': info_b['submit_ip']} ipa.append(info_a['submit_ip']) ipb.append(info_b['submit_ip']) return render(request, 'newtpl/cheat/showResult.html', {'tpl':{'sp':True,}, 'contest':c, 'cheat_list':cl, 'ipa':ipa,'ipb':ipb, 'form':form }) except Exception as e: return render(request, Err.ERROR_PAGE) # def codeDiff(request, ctid): # try: # u = User.getSessionUser(request.session) # if not u: # raise Err(request, err='not login') # ct = Cheat.objects.select_related('sub1__user', 'sub2__user').get(ctid=ctid) # # try: # ct.contest.canBeManaged(u) # except: # raise Err(request, err='no priv') # # return render(request, 'newtpl/cheat/codeDiff.html', {'tpl':{'sp':True,}, 'sub1':ct.sub1, 'sub2':ct.sub2}) # # # except Exception as e: # return render(request, Err.ERROR_PAGE) """
def submissionList(request, contest_id=None, page_id='1'): logger.info(str(request).replace("\n\r","\t")) """ @view: list submission of some contest """ try: page_id = int(page_id) u = User.getSessionUser(request.session) if not u: raise Err( request, err='not login') cid = int(contest_id) try: c = Contest.getById(cid) except: raise Err( request, err='no contest', log_format=( '{0}'.format(cid), ''), user_format=( u'{0}'.format( cid), u'别做坏事!'), ) c.course_class_name = unicode(c.course_class.getFullName()) c.description = unicode(c.contest_description) c.title = unicode(c.contest_title) now = datetime.now() if c.start_time+timedelta(minutes=c.length) < now: c.status = 'ended' elif c.start_time > now: c.status = 'scheduled' else: c.status = 'running' c.time_passed = min(max(int((now-c.start_time).total_seconds())/60, 0), c.length) c.time_passed_percent = 100*c.time_passed/c.length c.time_left = c.length-c.time_passed isManager = c.course_class.canBeManaged(u) user = u if not isManager else None # privilege for showing all submissions idxList = [(cp.problem_index, cp.problem_index) for cp in c.getContestProblem()] langList = [('gcc','GNU C'), ('g++','GNU C++'), ('java','java')] form = submissionListForm(idxList, langList, request.GET) if form.is_valid(): if form.cleaned_data['problem_index']: try: contestProb = ContestProblem.getBy( c, form.cleaned_data['problem_index']) except: contestProb=None # raise Exception(u'contest problem not found') else: contestProb=None else: raise Err( request, err='example err', log_format=( 'form invalid', ''), user_format=( u'输入的内容不合法', '') ) submissions = Submission.submissionList( u=user, c=c, cp=contestProb, uname=form.cleaned_data['username'], lang=form.cleaned_data['language'], sta=form.cleaned_data['status']) if isManager and 'rejudge' in request.GET: map( lambda x: Submission.rejudgeSubmission( x), submissions) paginator = Paginator(submissions, Const.STATUS_PER_PAGE) page_id = min(max(int(page_id), 1), paginator.num_pages) s = paginator.page(page_id) for sub_s in s: sub_s.status_color = Const.STATUS_COLOR[sub_s.status] if sub_s.status in Const.STATUS_COLOR else '' sub_s.status_cn = Const.STATUS_CN[ sub_s.status] return render(request, 'newtpl/submission/status.html', {'sList':s, 'form':form, 'c':c, 'tpl':{'can_manage': True if isManager else False}}) except Exception as e: logger.error(str(e).replace("\n\r","\t")) return render(request, Err.ERROR_PAGE, { 'errmsg': unicode(e) } )
def addSubmission(request, contest_id=None, problem_index=None): logger.info(str(request).replace("\n\r","\t")) """ view used to add submission """ try: contest_id = int(contest_id) u = User.getSessionUser( request.session) if not u: raise Err( request, err='not login') if not contest_id: raise Err( request, err='request err', log_format=( 'contest id', 'UNDEFINED'), user_format=( u'考试编号', u'你吃了么!!!'), ) elif not problem_index: raise Err( request, err='request err', log_format=( 'problem index', 'UNDEFINED'), user_format=( u'题目序号', u'哪里去了!!'), ) else: try: c = Contest.getById( contest_id ) except: raise Err( request, err='no contest', log_format=( '{0}'.format(cid), ''), user_format=( u'{0}'.format( cid), u'别做坏事!'), ) try: p_index = ContestProblem.getBy(c=c, idx=problem_index) except: raise Err( request, err='no contestproblem', log_format=( '{0}'.format( problem_index), ''), user_format=( u'{0}'.format( problem_index), u'别乱写好么!!'), ) p = p_index.problem if not Submission.canSubmitCode( u, p_index ): raise Err( request, err = 'submit err', log_format = ( 'no priv to submit', 'contest ended or no priv to attend the contest'), user_format = ( u'您没有提交该题的权限', u'考试已结束或者您没有参加本场考试的权限') ) lang_list = [] if c.lang_limit & 1 == 1: lang_list.append( ('gcc','GNU C')) if (c.lang_limit >> 1) & 1 == 1: lang_list.append( ('g++','GNU C++')) if (c.lang_limit >> 2) & 1 == 1: lang_list.append( ('java', 'JAVA')) if request.method == 'POST': form = addSubmissionForm(lang_list, request.POST) if form.is_valid(): sub_name = str( datetime.now()) time_str = sub_name.split('.') time_str.pop() for i in ['-',':','.',' ']: sub_name = sub_name.replace(i,'_') import os code_file_path = os.path.join( Const.SUBMISSION_TMP_PATH, "{0:010d}_{1}".format( u.uid, sub_name)) code_length = 0 # head_details: details of the submission added at the head of the code file head_details = '' head_details += '/*\n' head_details += 'USER_ID: ' + str(u) + '\n' head_details += 'PROBLEM: ' + str(p.pid) + '\n' head_details += 'SUBMISSION_TIME: ' + time_str[0] + '\n' head_details += '*/\n' if 'code_file' in request.FILES: default_storage.save( code_file_path, request.FILES['code_file']) else: if form.cleaned_data['code']: default_storage.save( code_file_path, ContentFile( head_details + form.cleaned_data['code'])) else: raise Err( request, err='request err', log_format=( 'code', 'no input'), user_format=( u'代码呢!', u'不写代码交什么交!!'), ) code_length = default_storage.size( code_file_path) sub_lang = form.cleaned_data['language'] if sub_lang not in map( lambda x: x[0], lang_list): raise Err( request, err='illegal language', log_format=( '{0}'.format( sub_lang), 'blabla'), user_format=( u'{0}'.format( sub_lang), u'别瞎搞成不!!'), ) x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') additional = { 'submit_ip': ip} Submission.addSubmission( u, p_index, code_file_path, sub_lang, code_length, additional) return redirect('Submission:contest_status',contest_id=contest_id) # could it be okay? else: raise Exception(u'form invalid') # the usage of url.name in urls.py # not POST method else: form = addSubmissionForm(lang_list) return render( request, 'newtpl/submission/submit.html', { 'form': form, 'tpl': {'sp':True}, 'contest': c, 'cp': p_index}, )# 'test_info': form.cleaned_data['language']}) except Exception as e: logger.error(str(e).replace("\n\r","\t")) return render( request, Err.ERROR_PAGE, { 'errmsg': unicode(e), }, )