def editOneProblem(): form = EditProblemForm().validateForApi() userId = g.userInfo.userId Problem.editOneProblem(ownerId=userId, problemId=form.problemId.data, form=form) return Success(information="编辑问题成功")
def post(self): args = create_problem_parser.parse_args() if Problem.search(remote_oj=args['remote_oj'], remote_prob=args['remote_prob'])['data']: raise ParameterException('problem already exist') data = get_problem_info(args['remote_oj'], args['remote_prob']) problem = Problem.create(**args, title=data['title'], description=data['description'], status=0) return {'message': 'create success', 'problem_id': problem.id}, 201
def deleteQuestionnaire(): userId = g.userInfo.userId form = DeleteQuestionnaireForm().validateForApi() Problem.deleteProblems(ownerId=userId, questionnaireId=form.questionnaireId.data) Questionnaire.deleteQuestionnaire( ownerId=userId, questionnaireId=form.questionnaireId.data) return Success(information="删除问卷成功")
def add_pro(ques): with db.auto_commit(): pro = Problem() pro.ques = ques db.session.add(pro) return Success()
def post(self): args = create_solution_parser.parse_args() problem = Problem.get_by_id(args['problem_id']) if args['contest_id']: contest = Contest.get_by_id(args['contest_id']) if contest.status != 2: raise ParameterException('contest not available') if problem.id not in [i.id for i in contest.problem_list]: raise ParameterException('problem not in contest') real_language = Language.search(oj=problem.remote_oj, key=args['language'])['data'] if not real_language: raise ParameterException('language does not exist') real_language = real_language[0].value old_language = args['language'] args['language'] = real_language solution = Solution.create(**args, user_id=g.user.id, status='Local info: Create solution') async_submit_code(solution.id, solution.problem_id, old_language, solution.code) return { 'message': 'create solution success', 'solution_id': solution.id }, 201
def post(self): args = search_problem_parser.parse_args() user = get_current_user() if args['status']: if not user or user.permission != -1: raise Forbidden() return Problem.search(**args)
def modify(self, **kwargs): from app.models.oj import OJ from app.models.problem import Problem from app.models.problem_set.problem_relationship import \ ProblemRelationship from app.models.problem_set.user_relationship import UserRelationship from app.models.user import User if kwargs['problem_list'] is not None: for i in ProblemRelationship.search(problem_set_id=self.id, page_size=-1)['data']: i.delete() for i in kwargs['problem_list']: oj_name, problem_pid = i['problem'].split('-', 1) oj = OJ.get_by_name(oj_name) problem = Problem.get_by_oj_id_and_problem_pid( oj.id, problem_pid) ProblemRelationship.create(problem_id=problem.id, problem_set_id=self.id, difficulty=i['difficulty']) kwargs.pop('problem_list') if kwargs['user_list'] is not None: for i in UserRelationship.search(problem_set_id=self.id, page_size=-1)['data']: i.delete() for i in kwargs['user_list']: user = User.get_by_id(i) if user is None: continue UserRelationship.create(username=user.username, problem_set_id=self.id) kwargs.pop('user_list') super().modify(**kwargs)
def pushWJWDataToDB(): from app.models.problem import Problem from app.models.user import User from app.models.questionnaire import Questionnaire from app.models.basicInfo import BasicInfo with db.app.app_context(): # 计数器 count = 0 failedNum = 0 successNum = 0 templateUserId = User.getTemplateUserId() info = BasicInfo.objects.first() myList = WJWSpider.getLinks(5) # 获得当前网站下已经爬到的最新链接 newestLink = info.getNewestLink(0) for currentLink in myList: count += 1 # 发现当前链接和最新链接相等 if currentLink == newestLink: sendSuccessEmail(successNum, failedNum) return # 防止由于网站原因拿不到部分数据 如果异常则加载下一份问卷 try: qid = getUniqueId() p = WJWSpider(url=currentLink).runSpider() Questionnaire.createByTemplates(templateUserId, qid, p.getTitle(), p.getSubTitle()) problems = p.getProblems() for p in problems: res = WJWProblem(p) Problem.createByTemplates(res.getProblemTitle(), res.checkProblemType(), res.getProblemOptions(), getUniqueId(), templateUserId, qid) successNum += 1 except Exception as e: current_app.logger.info(e) failedNum += 1 continue # 最新数据 并且生效写入数据库了 if successNum is 1: info.renewNewestLink(0, currentLink) sendSuccessEmail(successNum, failedNum)
def copyTemplates(qid, uid): from app.models.user import User from app.models.problem import Problem templateUserId = User.getTemplateUserId() # 获得目标模板 newqid = getUniqueId() q = Questionnaire.objects.filter(questionnaireId=qid, ownerId=templateUserId).first() if not q: raise NoQuestionnaire Questionnaire.createByTemplates(uid, newqid, q.title, q.subTitle) ps = Problem.objects.filter(targetQuestionnaireId=qid) if not ps: raise NoProblem for p in ps: Problem.createByTemplates(p.title, p.type, p.options, getUniqueId(), uid, newqid)
def post(self): args = contest_parser.parse_args() problem_list = args['problem_list'] for problem_id in problem_list: problem = Problem.get_by_id(problem_id) if not problem: raise NotFound('problem {} not found'.format(problem_id)) Contest.modify(**contest_parser.parse_args()) return {'message': 'create success'}, 201
def getQuestionnaire(ownerId, questionnaireId, isAdmin): from app.models.problem import Problem q = Questionnaire.objects.filter( ownerId=ownerId, questionnaireId=questionnaireId).first() if not q: raise NoQuestionnaire return { "basicInfo": q.getConditionJson(isAdmin=isAdmin), "problems": Problem.getProblems(questionnaireId) }
def crawl_problem_rating(problem_id): problem = Problem.get_by_id(problem_id) oj = OJ.get_by_id(problem.oj_id) try: oj_spider: BaseSpider = globals()[oj.name.title() + 'Spider']() rating = oj_spider.get_problem_info(problem.problem_pid)['rating'] except: rating = DEFAULT_PROBLEM_RATING problem.modify(rating=rating)
def patch(self, id_): contest = Contest.get_by_id(id_) if contest is None: raise NotFound() args = contest_parser.parse_args() problem_list = args['problem_list'] for problem_id in problem_list: problem = Problem.get_by_id(problem_id) if not problem: raise NotFound('problem {} not found'.format(problem_id)) contest.modify(**contest_parser.parse_args()) return {'message': 'modify success'}
def checkSecretKey(questionnaireId, key): from app.models.problem import Problem q = Questionnaire.objects.filter( questionnaireId=questionnaireId).first() if not q: raise NoQuestionnaire if q.secretKey != key and q.isSecret: raise WrongProblemSecretKey return { "basicInfo": q.getConditionJson(isAdmin=False), "problems": Problem.getProblems(questionnaireId) }
def post(self, id_): solution = Solution.get_by_id(id_) if solution is None: raise NotFound() if solution.status_canonical != 'OTHER' and g.user.permission != -1: raise Forbidden() problem = Problem.get_by_id(solution.problem_id) old_language = Language.search(oj=problem.remote_oj, value=solution.language)['data'][0].key solution.modify(status='Local info: Start rejudge') async_submit_code(solution.id, solution.problem_id, old_language, solution.code) return {'message': 'create rejudge success'}, 201
def problem_list(self): from app.models.problem import Problem from app.models.problem_set.problem_relationship import \ ProblemRelationship r = list() for i in ProblemRelationship.search(problem_set_id=self.id, page_size=-1)['data']: p = Problem.get_by_id(i.problem_id) p.difficulty = i.difficulty fields = Problem.fields.copy() fields.append('difficulty') p.fields = fields r.append(p) return r
def crawl_accept_problem(username, oj_id): user = User.get_by_id(username) if not user: return oj = OJ.get_by_id(oj_id) if not oj: return oj_username = OJUsername.search(username=username, oj_id=oj_id)['data'] if not oj_username: return oj_username = oj_username[0] oj_spider: BaseSpider = globals()[oj.name.title() + 'Spider']() accept_problems = dict() for i in AcceptProblem.search(username=username, page_size=-1)['data']: accept_problems["{}-{}".format(i.problem.oj.name, i.problem.problem_pid)] = \ datetime.datetime.strftime(i.create_time, '%Y-%m-%d %H:%M:%S') res = oj_spider.get_user_info(oj_username, accept_problems.copy()) if res['success']: oj_username.modify(last_success_time=datetime.datetime.now()) crawl_accept_problems = res['data'] print(crawl_accept_problems) deduplication_accept_problem = list() for i in crawl_accept_problems: pid = "{}-{}".format(i['oj'], i['problem_pid']) if i['accept_time'] is not None: if accept_problems.get(pid): if i['accept_time'] < accept_problems.get(pid): deduplication_accept_problem.append(i) else: deduplication_accept_problem.append(i) else: if accept_problems.get(pid) is None: deduplication_accept_problem.append(i) print(len(deduplication_accept_problem)) for i in deduplication_accept_problem: oj = OJ.get_by_name(i['oj']) problem = Problem.get_by_oj_id_and_problem_pid(oj.id, i['problem_pid']) submit_crawl_problem_rating_task(problem.id) accept_problem = AcceptProblem.get_by_username_and_problem_id( username, problem.id) accept_problem.modify(create_time=str_to_datetime(i['accept_time']), referer_oj_id=oj_id)
def submit_code(solution_id, problem_id, language, code): solution = Solution.get_by_id(solution_id) g.solution = solution problem = Problem.get_by_id(problem_id) remote_user = get_remote_user(problem.remote_oj) solution.modify(status='Local info: Assign remote user: {}'.format( remote_user.username), remote_user_id=remote_user.id) spider = globals()[remote_user.oj.title() + 'Spider'](remote_user) solution.modify(status='Local info: Submitting') res = spider.submit(problem.remote_oj, problem.remote_prob, language, code) remote_user.modify(status=1) if not res.get('success'): solution.modify(status='Remote info: {}'.format(res.get('error')), processing=0) return remote_id = res['remote_id'] solution.modify(status='Local info: Get remote id: {}'.format(remote_id), remote_id=remote_id) check_status(spider, solution)
def create(cls, **kwargs): from app.models.oj import OJ from app.models.problem import Problem from app.models.problem_set.problem_relationship import \ ProblemRelationship from app.models.problem_set.user_relationship import UserRelationship from app.models.user import User problem_set = super().create(**kwargs) for i in kwargs['problem_list']: oj_name, problem_pid = i['problem'].split('-', 1) oj = OJ.get_by_name(oj_name) problem = Problem.get_by_oj_id_and_problem_pid(oj.id, problem_pid) ProblemRelationship.create(problem_id=problem.id, problem_set_id=problem_set.id, difficulty=i['difficulty']) for i in kwargs['user_list']: user = User.get_by_id(i) if user is None: continue UserRelationship.create(username=user.username, problem_set_id=problem_set.id)
def appendOneProblem(): userId = g.userInfo.userId form = ProblemForm().validateForApi() pid = Problem().appendOneProblem(ownerId=userId, form=form) return Success(information="新建问题成功", payload={"problemId": pid})
def patch(self, id_): problem = Problem.get_by_id(id_) if not problem: raise NotFound() problem.modify(**modify_problem_parser.parse_args()) return {'message': 'modify success'}
def delete(self, id_): problem = Problem.get_by_id(id_) if not problem: raise NotFound() problem.delete() return {'message': 'delete success'}, 204
def get(self, id_): problem = Problem.get_by_id(id_) if problem is None: raise NotFound() return problem
def getProblemByPid(problemId): userId = g.userInfo.userId res = Problem.getOneProblemByPid(pid=problemId, oid=userId) return Success(information='更新成功', payload=res)
def deleteOneProblem(): form = DeleteProblemForm().validateForApi() userId = g.userInfo.userId Problem.deleteOneProblem(ownerId=userId, problemId=form.problemId.data) return Success(information="删除问题成功")
def problem(self): return Problem.get_by_id(self.problem_id)