def receive_judge_result(self, sid): """ 接收评测结果(基础:转移result文件、核对信息等) :param sid: JudgeStatus ID :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._license_check(): return result = self._request.POST.get("result") root = json.loads(result) status = ProblemMdl.JudgeStatus.objects.filter(id=sid) if status.exists(): status = status[0] status.result = result status.flag = root.get('exitcode', 8) status.exe_mem = root.get('memused', 0) status.exe_time = root.get('timeused', 0) status.save() self.status_callback_proc(status) self._exchange_progrem_run_outdata(status, root.get('session_id'), root.get("outdatas")) self._arrange_judge_result(status) self._result = kernel.RESTStruct(True, data=True) else: self._result = kernel.RESTStruct(False, msg="Error: No status found.")
def tdmaker_receive_judge_result(self, id): """ [TDMaker]接收评测结果(写入全新的测试数据) :param sid: JudgeStatus ID :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._license_check(): return result = self._request.POST.get("result") root = json.loads(result) tdq = ProblemMdl.TdmakerQueue.objects.filter(id=id) if tdq.exists(): tdq = tdq[0] tdq.flag = root.get('exitcode', 8) tdq.memused = root.get('memused', 0) tdq.timeused = root.get('timeused', 0) tdq.save() session_id = root.get('session_id', '0') out_tmp_dir = kernel.LocalStorage( kernel.const.PROGRAM_RUN_OUTDATAS_TEMP, session_id) out_target_dir = kernel.LocalStorage( kernel.const.PROBLEM_TESTDATA_DIR, str(tdq.problem.id)) testdata = tdq.problem.test_data.all() for td in testdata: handle = td.handle out_target_dir.clone_file( "%s.out" % handle, out_tmp_dir.get_file_path("%s.outdata" % handle)) out_tmp_dir.delete('') # 删除当前目录 self._result = kernel.RESTStruct(True, data=True) else: self._result = kernel.RESTStruct( False, msg="Error: No tdmaker status found.")
def save_problem_demo_code(self, pid): # 保存题目的示例代码接口 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return query = PBInc.ProblemBodyQuery() problem = query.get_problem_detail(pid, True) if problem is None: self._result = kernel.RESTStruct(False, '题目信息未找到') return if not self.__check_permission(problem, True, owner_only=True): return code_content = self._request.POST.get('content') code_lang = self._request.POST.get('lang') if not kernel.const.LANGUAGE_DESCRIPTION.has_key(code_lang): self._result = kernel.RESTStruct(False, msg='不支持的评测语言') return problem.demo_code = json.dumps({ "lang": code_lang, "content": code_content }) problem.save() self._result = kernel.RESTStruct(True)
def repository_new_folder(self, repo_handle): """ 新建文件夹 :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return if self._user_session.user_role < 2: self._result = kernel.RESTStruct(False, '当前账户没有操作权限') return repo = self._get_repository(repo_handle) if repo is None: self._result = kernel.RESTStruct(False, '找不到仓库') return if not self.__general_permission_check(repo, True): return stor = kernel.LocalStorage(kernel.const.EDU_REPOSITORY_ROOT_DIR, repo_handle) path = self._request.POST.get("path", "").replace("../", "").replace("..", "") stor = stor.get_child_dir_storage(path) print stor.get_current_path() folder_name = self._request.POST.get("folder_name", "") if folder_name.strip() == "": self._result = kernel.RESTStruct(False, '请输入新文件夹名') return stor.new_folder(folder_name) self._result = kernel.RESTStruct(True)
def save_impression(self, asgn_id): """保存评测详情""" self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return asgn = self._get_asgn_detail(asgn_id) if asgn is None: return # 不返回结果,没什么好说的,非法调用接口了都 flag, dec = self._check_asgn_permission(asgn) if flag != 0: if flag == -1: self._result = kernel.RESTStruct(False, msg='对不起,作业还没开放') elif flag == 1: self._result = kernel.RESTStruct(False, msg='对不起,作业提交已经结束') return report = AsgnModel.StuReport.objects.filter( asgn=asgn, student=self._user_session.entity) if not report.exists(): self._result = kernel.RESTStruct(False, msg='你还没有生成实验报告') return report = report[0] impression = self._request.POST.get('impression', '') report.impression = impression report.modify_time = int(time.time()) report.save() self._result = kernel.RESTStruct(True)
def batch_change_visiable(self, flag): # 改变题目的可见性接口 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return if int(flag) == 0: vflag = False else: vflag = True problem_ids = self._request.POST.getlist('problem_ids') ps = ProblemMdl.Problem.objects.filter(id__in=problem_ids) msg = [] for p in ps: if self.__check_permission_only(p, owner_only=True): p.is_show = vflag p.save() else: msg.append("%s.%s" % (p.id, p.title)) if len(msg) > 0: self._result = kernel.RESTStruct(True, msg="操作成功,但是以下项目没有改动:<br />%s" % "<br />".join(msg)) else: self._result = kernel.RESTStruct(True, msg="操作成功")
def delete_testdata(self, pid, handle): # 删除测试数据接口 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(ajax=True): return query = PBInc.ProblemBodyQuery() problem = query.get_problem_detail(pid, True) if problem is None: self._result = kernel.RESTStruct(False, '题目信息未找到') return if not self.__check_permission(problem, True, owner_only=True): return testdata = problem.test_data.filter(handle=handle) if not testdata.exists(): self._result = kernel.RESTStruct(False, '未找到测试数据信息') return td = testdata[0] stor = kernel.LocalStorage(kernel.const.PROBLEM_TESTDATA_DIR, str(problem.id)) stor.delete("%s.in" % str(td.handle)) stor.delete("%s.out" % str(td.handle)) td.delete() if problem.test_data.count() == 0: problem.pause_judge = True problem.save() self._result = kernel.RESTStruct(True)
def new_testdata(self, pid): # 新建测试数据接口 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(ajax=True): return query = PBInc.ProblemBodyQuery() problem = query.get_problem_detail(pid, True) if problem is None: self._result = kernel.RESTStruct(False, '题目信息未找到') return if not self.__check_permission(problem, True, owner_only=True): return order = problem.test_data.count() + 1 td = ProblemMdl.TestData() td.handle = kernel.GeneralTools.get_my_handle_id() td.name = "Problem %s Testdata %d" % (str(problem.id), order) td.order = order td.available = True td.visible = False td.update_time = 0 td.save() stor = kernel.LocalStorage(kernel.const.PROBLEM_TESTDATA_DIR, str(problem.id)) stor.new_file("%s.in" % str(td.handle), '') stor.new_file("%s.out" % str(td.handle), '') problem.test_data.add(td) self._result = kernel.RESTStruct(True)
def run_tdmaker(self, pid): # 运行测试数据生成器 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return query = PBInc.ProblemBodyQuery() problem = query.get_problem_detail(pid, True) if problem is None: self._result = kernel.RESTStruct(False, '题目信息未找到') return if not self.__check_permission(problem, True, owner_only=True): return demo_code = {} if problem.demo_code is not None and problem.demo_code.strip() != "": demo_code = json.loads(problem.demo_code) lang = demo_code.get('lang', 'gcc') que = ProblemMdl.TdmakerQueue() que.author = self._user_session.entity que.problem = problem que.lang = lang que.flag = -1 que.save() self._result = kernel.RESTStruct(True)
def asgn_public_statistics_analyzer(self, asgn_id): """ 作业统计模块 :param asgn_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON """ if not self._check_login(True): return """ asgn = self._get_asgn_detail(asgn_id) if asgn is None: self._action = kernel.const.VIEW_ACTION_JSON self._result = kernel.RESTStruct(False, '未找到作业信息') return """ if not self.__check_permission_only(asgn): self._action = kernel.const.VIEW_ACTION_JSON self._result = kernel.RESTStruct(False, '当前账户没有操作权限') return """ result = self._asgn_analyzer(asgn) self._result = kernel.RESTStruct(True, data=result)
def change_problem_judge_pause(self, pid, pause): # 设置题目的评测启动或停止 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return query = PBInc.ProblemBodyQuery() problem = query.get_problem_detail(pid, True) if problem is None: self._result = kernel.RESTStruct(False, '题目信息未找到') return if not self.__check_permission(problem, True, owner_only=True): return if pause == '1': pause = True else: pause = False if pause is False and problem.test_data.count() == 0: self._result = kernel.RESTStruct(False, '当前题目没有测试数据,无法启动评测') return problem.pause_judge = pause problem.save() self._result = kernel.RESTStruct(True)
def change_course_assistants(self, course_id): """ 助教管理:更改助教信息 :param course_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON course = self.__general_permission_check(course_id, True) if course is False: return action_ids = self._request.POST.getlist("action_ids") # 获取批量操作的列表 if len(action_ids) == 0: self._result = kernel.RESTStruct(False, '请选择要操作的项目') return msg = [] for ids in action_ids: student = AccountModel.User.objects.filter( id=ids.strip()) # 检查用户是否存在并且是否为学生用户 if student.exists(): student = student[0] course.assistants.remove(student) # Remove else: # 不存在则取消本次操作 msg.append(u"学生 ID=%s 不存在" % ids.strip()) continue if len(msg) == 0: self._result = kernel.RESTStruct(True, msg=u"批量操作成功") else: self._result = kernel.RESTStruct(True, msg=u"批量操作成功,但某些项目可能存在问题:<br />" + u"<br />".join(msg))
def delete_account(self): """ 删除账户 :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return if not self._check_permission(True): return msg = [] account_ids = self._request.POST.getlist("account_ids") if len(account_ids) == 0: self._result = kernel.RESTStruct(False, msg="无操作项目") return for id in account_ids: account = AccountModel.User.objects.filter(id=id) if not account.exists(): msg.append("[%s]不存在" % id) continue account = account[0] account.delete() if len(msg) > 0: self._result = kernel.RESTStruct(True, msg="操作成功,但是以下项目没有改动:<br />%s" % "<br />".join(msg)) else: self._result = kernel.RESTStruct(True, msg="操作成功")
def delete_classify(self, classify_id=0): """ 删除分类 :param classify_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(): return if int(classify_id) == 0: self._result = kernel.RESTStruct(False, "根分类不能被删除") return node = ProblemMdl.ProblemClassify.objects.filter(id=classify_id) if not node.exists(): self._result = kernel.RESTStruct(False, "未找到节点") return node = node[0] if not self.__check_permission(node, True): return cids = list(set(self.get_children_nodes(node.id))) plist = ProblemMdl.Problem.objects.filter(classify__in=cids) for p in plist: p.classify = None p.save() node.delete() self._result = kernel.RESTStruct(True)
def change_course_students(self, course_id): """ 学生管理:更改排课信息 :param course_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON course = self.__general_permission_check(course_id, True) if course is False: return action_ids = self._request.POST.getlist("action_ids") # 获取批量操作的列表 if len(action_ids) == 0: self._result = kernel.RESTStruct(False, '请选择要操作的项目') return action = self._request.POST.get("action", '') # 获取执行动作 msg = [] if str(action) == 'delete': # 如果执行动作不是【删除】,则值为排课ID target_arr = None else: target_arr = course.arrangements.filter(id=action) # 根据排课ID扫描排课信息 if not target_arr.exists(): # 值非法 self._result = kernel.RESTStruct(False, '没有找到排课信息') return target_arr = target_arr[0] for ids in action_ids: id_temp = ids.split("|") if len(id_temp) != 2: continue arrid = id_temp[0] stuid = id_temp[1] student = AccountModel.User.objects.filter( id=stuid.strip(), role=1) # 检查用户是否存在并且是否为学生用户 if student.exists(): student = student[0] else: # 不存在则取消本次操作 msg.append(u"学生 ID=%s 不存在" % stuid.strip()) continue arr = course.arrangements.filter(id=arrid.strip()) if arr.exists(): arr = arr[0] arr.students.remove(student) # 从排课列表中删除 else: msg.append(u"排课信息 ID%s 不存在" % arrid.strip()) continue if target_arr is not None: # 向要移动到排课记录中添加学生信息,如果是删除操作,则不会执行到这里 target_arr.students.add(student) if len(msg) == 0: self._result = kernel.RESTStruct(True, msg=u"批量操作成功") else: self._result = kernel.RESTStruct(True, msg=u"批量操作成功,但某些项目可能存在问题:<br />" + u"<br />".join(msg))
def save_asgn_submission(self, asgn_id, problem_id): """提交代码评测""" self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return asgn = self._get_asgn_detail(asgn_id) if asgn is None: return # 不返回结果,没什么好说的,非法调用接口了都 asgn_problem = asgn.problemset.filter(problem__id=problem_id) if not asgn_problem.exists(): self._result = kernel.RESTStruct(False, msg='这个作业没有设置这道题目') return asgn_problem = asgn_problem[0] flag, dec = self._check_asgn_permission(asgn) if flag != 0: if flag == -1: self._result = kernel.RESTStruct(False, msg='对不起,作业还没开放') return elif flag == 1: self._result = kernel.RESTStruct(False, msg='对不起,作业提交已经结束') return proc_result = self._save_submission(problem_id) # 调用父类设置好的方法 if proc_result.flag is True: # 如果父类方法处理成功 status = proc_result.data status.callback = json.dumps({'provider': 'asgn', 'id': asgn.id}) status.save() asgn.judge_status.add(status) asgn.save() sol = AsgnModel.Solution.objects.filter( asgn=asgn, problems=asgn_problem.problem, author=self._user_session.entity) if not sol.exists(): sol = AsgnModel.Solution() sol.asgn = asgn sol.problems = asgn_problem.problem sol.author = self._user_session.entity sol.start_time = int(time.time()) sol.save() else: sol = sol[0] sol.judge_status.add(status) sol.submission += 1 sol.save() asgn_problem.submission += 1 asgn_problem.save() proc_result.data = status.id else: proc_result.data = None self._result = proc_result
def read_xls_to_change_team_user_info(self, contest_id): """ 比赛专用账号信息导入 :param contest_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return contest = self._get_contest(contest_id) if contest is None: self._send_error_contest_doesnt_exists(True) return if not self._check_permission(contest): self._send_error_contest_permission_denied(True) return if self._user_session.user_id != 'acm': self._result = kernel.RESTStruct(False, "只有ACM协会专用账号才能够执行该操作") return files = self._request.FILES.get('upload_xls') if files is None: self._result = kernel.RESTStruct(False, "没有找到上传的文件") return path = "contest_xls/%s%s" % (uuid.uuid4(), '.xls') file_name = os.path.join(kernel.const.IMPORT_PROCESS_TEMP_DIR, path) destination = open(file_name, 'wb+') for chunk in files.chunks(): destination.write(chunk) destination.close() try: xls_sheet = xlrd.open_workbook(file_name) xls_table = xls_sheet.sheet_by_index(0) for i in range(2, xls_table.nrows): user_row = xls_table.row_values(i) team_id = user_row[0] if team_id[0:4] != 'team': continue team = AccountModel.User.objects.filter(id=team_id) if not team.exists(): continue team = team[0] team.nickname = user_row[1] team.realname = user_row[2] team.sex = 0 if str(user_row[3]) == 'Y' else 1 team.password = self._user_session.gen_passwd(user_row[4]) team.locked = False team.save() self._result = kernel.RESTStruct(True) except: self._result = kernel.RESTStruct(False, "XLS文件处理过程出现错误,请检查XLS文件是否填写正确") return
def mgr_save_problem_setting(self, contest_id, problem_id): """ 保存题目设置 :param contest_id: :param problem_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return contest = self._get_contest(contest_id) if contest is None: self._send_error_contest_doesnt_exists(True) return if not self._check_permission(contest): self._send_error_contest_permission_denied(True) return if self._user_session.user_id != contest.author.id: self._result = kernel.RESTStruct(False, "只有比赛发起者才能够执行该操作") return cproblem = contest.problemset.filter(problem__id=problem_id) if not cproblem.exists(): self._send_error_contest_problem_not_found(True) return cproblem = cproblem[0] judge_style = self._request.POST.get("judge_style", "0") if not str.isdigit(str(judge_style)): judge_style = 0 judge_style = int(judge_style) lang = self._request.POST.getlist("lang") if len(lang) == 0: self._result = kernel.RESTStruct(False, "请至少分配一种评测语言") return cnt = 0 clang = contest.lang.split(",") if contest.lang != 'all' else kernel.const.LANGUAGE_PROVIDE for i in lang: if i not in clang: cnt += 1 if cnt == 0 and (len(lang) == len(clang)): lang = 'inherit' else: lang = ','.join(lang) cproblem.judge_style = judge_style cproblem.lang = lang cproblem.save() self._result = kernel.RESTStruct(True)
def save_register_student(self): """ 验证教务密码 :return: """ self._action = kernel.const.VIEW_ACTION_JSON username = self._request.POST.get('user', "") jw_pwd = self._request.POST.get('password', "") if username.strip() == '': self._result = kernel.RESTStruct(False, "学号不能为空!") return username_vaild = re.compile("^(\d{10})$") if username_vaild.match(username) is None: self._result = kernel.RESTStruct(False, '学号不符合规范!') return nns = AccountModel.User.objects.filter(id=username) if nns.exists(): self._result = kernel.RESTStruct(False, '当前用户已存在!') return esc = kernel.ESConnector() if not esc.login_validate(username, jw_pwd): self._result = kernel.RESTStruct(False, "教务系统认证失败!接口返回信息:" + esc.login_msg) return else: info = esc.get_student_info() user = AccountModel.User() user.id = username user.password = kernel.LoginSession.gen_passwd(jw_pwd) user.realname = info.get('name') user.nickname = info.get('name') user.role = 1 user.create_time = int(time.time()) if str(info.get('gender')) == '男': user.sex = 1 else: user.sex = 0 user.save() stu = AccountModel.Student() stu.account = user stu.studentId = username stu.studentPwd = jw_pwd stu.studentName = info.get('name') stu.grade = info.get('grade') stu.sex = info.get('gender') stu.className = info.get('className') stu.department = info.get('depName') stu.major = info.get('enrollSpecialityName') stu.rawData = json.dumps(info) stu.save() self._result = kernel.RESTStruct(True)
def mgr_save_status_change(self, contest_id, status_id): """ 保存题目设置 :param contest_id: :param problem_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return contest = self._get_contest(contest_id) if contest is None: self._send_error_contest_doesnt_exists(True) return if not self._check_permission(contest): self._send_error_contest_permission_denied(True) return cstatus = contest.judge_status.filter(id=status_id) if not cstatus.exists(): self._result = kernel.RESTStruct(False, '没有找到相关评测记录') return cstatus = cstatus[0] cproblem = contest.problemset.filter(problem=cstatus.problem) if not cproblem.exists(): self._send_error_contest_problem_not_found(True) return cproblem = cproblem[0] if cproblem.judge_style not in [1, 2]: self._result = kernel.RESTStruct(False, '由于当前题目设置为【自动评测】,不能使用此功能') return flag = self._request.POST.get('flag', -1) try: flag = int(flag) except: flag = -1 if flag < 0 or flag > 8: self._result = kernel.RESTStruct(False, '参数错误') return cstatus.flag = flag cstatus.save() jsapi = ProblemKernel.JudgeServiceAPI(self._request) jsapi.status_callback_proc(cstatus) self._result = kernel.RESTStruct(True)
def save_asgn_problem_setting(self, asgn_id, asgn_problem_id): """ 保存当前作业中某道题目的设置 """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return asgn = self._get_asgn_detail(asgn_id) if asgn is None: return # 不返回结果,没什么好说的,非法调用接口了都 if not self.__check_permission(asgn, True): return ap = asgn.problemset.filter(id=asgn_problem_id) if not ap.exists(): self._result = kernel.RESTStruct(False, "找不到该题目设置") return ap = ap[0] score = self._request.POST.get("score", "") if not str.isdigit(str(score)): self._result = kernel.RESTStruct(False, "分数值非法,请不要输入负数或者非数字符") return score = int(score) lang = self._request.POST.getlist("lang") if len(lang) == 0: self._result = kernel.RESTStruct(False, "请至少分配一种评测语言") return cnt = 0 asgn_lang = asgn.lang.split( ",") if asgn.lang != 'all' else kernel.const.LANGUAGE_PROVIDE for i in lang: if i not in asgn_lang: cnt += 1 if cnt == 0 and (len(lang) == len(asgn_lang)): lang = 'inherit' else: lang = ','.join(lang) require = True if self._request.POST.get('require', '0') == '1' else False ap.score = score ap.lang = lang ap.require = require ap.save() self._result = kernel.RESTStruct(True)
def save_new_problem(self): # 保存新建题目接口 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return if not self.__check_permission(None, True): return rel, msg = self.__save_problem_setting() if rel: self._result = kernel.RESTStruct(rel, data=msg) else: self._result = kernel.RESTStruct(rel, msg)
def faq_save_reply(self, contest_id, faq_id): """ 保存回复问答 :param contest_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return contest = self._get_contest(contest_id) if contest is None: self._send_error_contest_doesnt_exists(True) return if not self._check_permission(contest): self._send_error_contest_permission_denied(True) return if not self._check_admin_permission(contest): self._send_error_contest_permission_denied(True) return faq = ContestModel.FAQ.objects.filter(id=faq_id) if not faq.exists(): self._send_error_contest_faq_not_found(True) return faq = faq[0] content = self._request.POST.get("content", "") is_public = self._request.POST.get("is_public", "") if content.strip() == '': self._result = kernel.RESTStruct(False, "请输入回复的内容") return if is_public == '1': is_public = True else: is_public = False faq.answer_content = content faq.answer_referee = self._user_session.entity faq.answer_time = int(time.time()) faq.is_private = not is_public faq.save() self._result = kernel.RESTStruct(True)
def save_new_asgn(self, course_id): """ 保存当前作业的设置 """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return course = self._get_course_by_id(course_id) if course is None: self._result = kernel.RESTStruct(False, "课程不存在") return if self._set_asgn_setting(course): self._result = kernel.RESTStruct(True)
def __check_permission(self, problem=None, ajax=False, owner_only=False, no_redirect=False): """ 检查权限(包含渲染数据处理,也就是说调用完的结果为False时只要return即可,不必再写渲染代码) :param problem: 问题记录 :ajax problem: 是否为ajax请求 :param owner_only: 仅允许拥有者操作(管理员无效) :param no_redirect: 禁用错误页面跳转 :return: """ flag = self.__check_permission_only(problem, owner_only) if flag is False: if no_redirect: self._action = kernel.const.VIEW_ACTION_DEFAULT self._context = "[ERROR]当前账户没有操作权限" return flag if ajax: self._action = kernel.const.VIEW_ACTION_JSON self._result = kernel.RESTStruct(False, u'当前账户没有操作权限') return flag else: self._action = kernel.const.VIEW_ACTION_ERROR_PAGE self._context = kernel.error_const.ERROR_ADMIN_PERMISSION_DENIED return flag return flag
def faq_del(self, contest_id): """ 删除提问、公告 :param contest_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return contest = self._get_contest(contest_id) if contest is None: self._send_error_contest_doesnt_exists(True) return if not self._check_permission(contest): self._send_error_contest_permission_denied(True) return is_referee = self._check_admin_permission(contest) faq_id = self._request.GET.get('id', '') try: faq_id = int(faq_id) except: return faq = ContestModel.FAQ.objects.filter(id=faq_id) if not faq.exists(): self._result = kernel.RESTStruct(False, "问答内容不存在") return faq = faq[0] if is_referee: faq.delete() else: if self._user_session.user_id == faq.author.id: if faq.answer_time != 0: self._result = kernel.RESTStruct(False, "您的问题已经被回复,不能删除!") return faq.delete() else: self._result = kernel.RESTStruct(False, "非法操作") return self._result = kernel.RESTStruct(True)
def _set_asgn_setting(self, course, asgn=None): """ 作业设置内部处理程序 :param course: 归属课程 :param asgn: 作业类,None则新建 :return: """ name = self._request.POST.get("name", "") if name.strip() == '': self._result = kernel.RESTStruct(False, "请输入作业名称") return False score = self._request.POST.get("full_score", "") if not str.isdigit(str(score)): self._result = kernel.RESTStruct(False, "分数值非法,请不要输入负数或者非数字符") return False score = int(score) remark = self._request.POST.get("remark", "") lang = self._request.POST.getlist("lang", "") if len(lang) == 0: self._result = kernel.RESTStruct(False, "请至少分配一种评测语言") return False cnt = 0 asgn_lang = kernel.const.LANGUAGE_PROVIDE for i in lang: if i in asgn_lang: cnt += 1 if cnt == len(asgn_lang): lang = 'all' else: lang = ','.join(lang) if asgn is None: asgn = AsgnModel.Asgn() asgn.author = self._user_session.entity asgn.create_time = int(time.time()) asgn.course = course asgn.remark = remark asgn.lang = lang asgn.name = name asgn.full_score = score asgn.save() return True
def enable_resource_repositories(self, course_id): """ 教学资料仓库管理:启用、关闭仓库 :param course_id: :return: """ self._action = kernel.const.VIEW_ACTION_JSON course = self.__general_permission_check(course_id, True) if course is False: return rhandle = self._request.POST.getlist("rhandle") repo_list = course.repositories.all() exist_handles = [] for repo in repo_list: exist_handles.append(repo.handle) msg = [] for handle in rhandle: repo = EduModel.Repository.objects.filter(handle=handle) if not repo.exists(): msg.append(u"仓库 handle=%s 不存在" % repo.handle.strip()) continue repo = repo[0] if handle not in exist_handles: course.repositories.add(repo) else: exist_handles.remove(handle) for handle in exist_handles: repo = EduModel.Repository.objects.filter(handle=handle) if not repo.exists(): msg.append(u"仓库 handle=%s 不存在" % repo.handle.strip()) continue repo = repo[0] course.repositories.remove(repo) course.save() if len(msg) == 0: self._result = kernel.RESTStruct(True, msg=u"批量操作成功") else: self._result = kernel.RESTStruct(True, msg=u"批量操作成功,但某些项目可能存在问题:<br />" + u"<br />".join(msg))
def tdmaker_get_problem_judge_options(self, id): """ [TDMaker]获取题目评测信息(包括测试数据集合) :param sid: Status的ID :return: """ self._action = kernel.const.VIEW_ACTION_JSON if not self._license_check(): return tdq = ProblemMdl.TdmakerQueue.objects.filter(id=id) if not tdq.exists(): self._result = kernel.RESTStruct( False, msg="Error: No TDmaker status found.") return tdq = tdq[0] problem = tdq.problem demo_code = {} if problem.demo_code is not None and problem.demo_code.strip() != "": demo_code = json.loads(problem.demo_code) lang = demo_code.get('lang', 'gcc') content = demo_code.get("content", "") tds = [] testdatas = problem.test_data.order_by("order") for td in testdatas: tds.append(td.dump_for_judge()) if str(lang) == 'java': code_path = "Main.java" time_limit = problem.java_time_limit mem_limit = problem.java_memory_limit else: code_path = "m.%s" % kernel.const.SOURCE_CODE_EXTENSION.get( lang, "c") time_limit = problem.c_time_limit mem_limit = problem.c_memory_limit self._result = kernel.RESTStruct(True, data={ 'lang': lang, 'problem_id': problem.id, 'time_limit': time_limit, 'memory_limit': mem_limit, 'test_data': tds, 'code_path': code_path, 'demo_code': content })
def add_problems(self, asgn_id): # 增加题目接口 self._action = kernel.const.VIEW_ACTION_JSON if not self._check_login(True): return asgn = self._get_asgn_detail(asgn_id) if asgn is None: self._result = kernel.RESTStruct(False, msg="作业不存在") return if not self.__check_permission(asgn, True): return problem_ids = self._request.POST.getlist('problem_ids') if len(problem_ids) == 0: self._result = kernel.RESTStruct(False, msg="请选择要操作的项目") return ps = ProblemMdl.Problem.objects.filter(id__in=problem_ids) failed = [] for p in ps: if not asgn.problemset.filter(problem=p).exists(): ap = AsgnModel.AsgnProblems() ap.problem = p ap.require = True ap.save() asgn.problemset.add(ap) else: failed.append("%s. %s<br />" % (p.id, p.title)) if len(failed) > 0: self._result = kernel.RESTStruct( True, msg= u"添加成功!<br />但是我们发现您选择了一些重复的题目,因此以下题目将不会被重复添加:<br /><strong>" + u"".join(failed) + "</strong>点击确定后自动返回作业的题目列表,请刷新页面,并对新添加的题目进行设置。") else: self._result = kernel.RESTStruct( True, msg=u"添加成功!<br />点击确定后自动返回作业的题目列表,请刷新页面,并对新添加的题目进行设置。") self._request.session["problem_archive_choosing_to_asgn"] = 0