def function(*args, **kwargs): failed = 'You do not have permission to enter' url = url_for('web.exam_list') path = request.path.split('/') id = path[-2] count = int(path[-1]) now_date = datetime.now() exam_page = ExamConditionService.get_user_exam_page( id, current_user.user_id, count) exam_end_time = exam_page['end_time'] #考试的设定信息 exam_info = ExamService.get_exam_detail_info(id) #已经考的场数 exam_count = ExamConditionService.get_user_exam_count( id, current_user.user_id) #中途加入的考试必须为最新的一场即下标为exam_count-1 if count != exam_count - 1: return render_template("web/failed.html", failed=failed, url=url) #验证考试时间 if now_date < exam_info.start_time or now_date > exam_info.end_time: return render_template("web/failed.html", failed=failed, url=url) #获取当前时间 now_time = time.time() # +5s 延迟,防止js强制结束考试时无法提交 if now_time > exam_end_time + 5: return render_template("web/failed.html", failed=failed, url=url) return f(*args, **kwargs)
def function(*args, **kwargs): failed = 'You do not have permission to enter' url = url_for('web.exam_list') path = request.path.split('/') id = path[-2] count = int(path[-1]) now_date = datetime.now() now_time = int(time.time()) #考试设定信息 exam_info = ExamService.get_exam_detail_info(id) #已经考的场数 exam_count = ExamConditionService.get_user_exam_count( id, current_user.user_id) # 如果考试不是无限次那么已经考试次数>=设定次数就不能参加考试 if exam_info.count != -1 and exam_count >= exam_info.count: return render_template("web/failed.html", failed=failed, url=url) #由于是新建考试,那么当前my_answer列表长度即新一场考试在列表中的下标,数值完全匹配才能够新建考试 #(0号下标依然成立) if count != exam_count: return render_template("web/failed.html", failed=failed, url=url) if count != 0: # 获得上一场考试的情况(用于验证上一场考试是否结束) exam_page = ExamConditionService.get_user_exam_page( id, current_user.user_id, count - 1) #如果要新建的考试不是第一场次考试, 那么检查上一场的考试 if exam_page['now_time'] < exam_page['end_time']: if now_time < exam_page['end_time']: return render_template("web/failed.html", failed=failed, url=url) #验证考试时间 if now_date < exam_info.start_time or now_date > exam_info.end_time: return render_template("web/failed.html", failed=failed, url=url) return f(*args, **kwargs)
def create_page(id): examAllInfo = ExamService.get_exam_detail_info(id) choice = [] blank = [] short_answer = [] problem_list = [] if examAllInfo.is_random == 1: problem_list = examAllInfo.problem_list elif examAllInfo.is_random == 0: problem_list = examAllInfo.problem_list_example for problem in problem_list: problem_detail = ProLibService.get_problem_detailed_info(problem) if problem_detail.type == Config.PROBLEM_TYPE['choice']: # 选项乱序 list = problem_detail.desc_other.split('#$') problem_detail.desc_other = random.sample(list, len(list)) choice.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer.append(problem_detail) # 题目乱序 choice_r = random.sample(choice, examAllInfo.choice['num']) blank_r = random.sample(blank, examAllInfo.blank['num']) short_answer_r = random.sample(short_answer, examAllInfo.answer['num']) # 生成json exam_page = {} exam_page['name'] = examAllInfo.name exam_page['choice'] = [] exam_page['blank'] = [] exam_page['short_answer'] = [] exam_page['times'] = examAllInfo.during_time #记录时长 exam_page['now_time'] = int(time.time()) #当前时间(时间戳,在render前赋值保证精确) exam_page['end_time'] = exam_page['now_time'] + exam_page['times'] #结束时间(时间戳,在render前赋值保证精确) exam_page['choice_score'] = examAllInfo.choice['score'] exam_page['blank_score'] = examAllInfo.blank['score'] exam_page['short_answer_score'] = examAllInfo.answer['score'] #count为当前考试在my_answer列表中的下标值 exam_page['count'] = ExamConditionService.get_user_exam_count(id, current_user.user_id) ''' { 'choice':[ {'pid':'123', 'describe_main':'test1', 'describe_other':['a','b','c','d'], }, {'pid':'1234', 'describe_main':'test2', 'describe_other':['a','b','c','d','e'], } ], 'blank':[ {'pid':'1111', 'describe_main':'test3', 'describe_other':'', 'answer':'' }] 'short_answer':[], 'name':'Test', 'times':'120', 'now_time':'' 'end_time':'' 'choice_score':10, 'blank_score':0, 'short_answer_score':0 'count':0 } ''' for problem in choice_r: problem_dict = {'pid': problem.pid, 'desc_main': problem.desc_main, 'desc_other': problem.desc_other, 'answer': ''} exam_page['choice'].append(problem_dict) for problem in blank_r: problem_dict = {'pid': problem.pid, 'desc_main': problem.desc_main, 'desc_other': problem.desc_other, 'answer': ''} exam_page['blank'].append(problem_dict) for problem in short_answer_r: problem_dict = {'pid': problem.pid, 'desc_main': problem.desc_main, 'desc_other': problem.desc_other, 'answer': ''} exam_page['short_answer'].append(problem_dict) return exam_page