async def post(self, *, tid: int, letter: str, lang: str, code: str): tdoc = await contest.get(self.domain_id, tid) pid = contest.convert_to_pid(tdoc['pids'], letter) pdoc = await problem.get(self.domain_id, pid) tsdoc = await contest.get_status(self.domain_id, tdoc['_id'], self.user['_id']) if not tsdoc or tsdoc.get('attend') != 1: raise error.ContestNotAttendedError(tdoc['_id']) if not self.is_live(tdoc): raise error.ContestNotLiveError(tdoc['_id']) if not pdoc: raise error.ProblemNotFoundError(self.domain_id, pid, tdoc['_id']) rid = await record.add(self.domain_id, pdoc['_id'], constant.record.TYPE_SUBMISSION, self.user['_id'], lang, code, tid=tdoc['_id'], hidden=True) # here is a update status. # await contest.update_status(self.domain_id, tdoc['_id'], self.user['_id'], # rid, pdoc['_id'], False, 0) if (not contest.RULES[tdoc['rule']].show_func(tdoc, self.now) and not self.has_perm(builtin.PERM_VIEW_CONTEST_HIDDEN_STATUS)): self.json_or_redirect( self.reverse_url('contest_detail', tid=tdoc['_id'])) else: self.json_or_redirect(self.reverse_url('record_detail', rid=rid))
async def get(self, *, tid: int, letter: str): uid = self.user['_id'] if self.has_priv( builtin.PRIV_USER_PROFILE) else None tdoc = await contest.get(self.domain_id, tid) pid = contest.convert_to_pid(tdoc['pids'], letter) pdoc = await problem.get(self.domain_id, pid, uid) if not pdoc: raise error.ProblemNotFoundError(self.domain_id, pid, tdoc['_id']) pdoc['letter'] = letter if not self.is_done(tdoc): tsdoc = await contest.get_status(self.domain_id, tdoc['_id'], self.user['_id']) if not tsdoc or tsdoc.get('attend') != 1: raise error.ContestNotAttendedError(tdoc['_id']) if not self.is_live(tdoc): raise error.ContestNotLiveError(tdoc['_id']) tsdoc, udoc = await asyncio.gather( contest.get_status(self.domain_id, tdoc['_id'], self.user['_id']), user.get_by_uid(tdoc['owner_uid'])) attended = tsdoc and tsdoc.get('attend') == 1 path_components = self.build_path( (self.translate('contest_main'), self.reverse_url('contest_main')), (tdoc['title'], self.reverse_url('contest_detail', tid=tid)), (pdoc['title'], None)) self.render('problem_detail.html', tdoc=tdoc, pdoc=pdoc, tsdoc=tsdoc, udoc=udoc, attended=attended, page_title=pdoc['title'], path_components=path_components)
async def get(domain_id: str, pid: int, uid: int=None): coll = db.Collection('problem') pdoc = await coll.find_one({'domain_id': domain_id, '_id': pid}) if not pdoc: raise error.ProblemNotFoundError(domain_id, pid) if uid is not None: pdoc['psdoc'] = await get_status(domain_id, pid=pid, uid=uid) else: pdoc['psdoc'] = None return pdoc
async def edit(domain_id: str, pid: int, **kwargs): if 'title' in kwargs: validator.check_title(kwargs['title']) if 'content' in kwargs: validator.check_content(kwargs['content']) coll = db.Collection('problem') pdoc = await coll.find_one_and_update(filter={'domain_id': domain_id, '_id': pid}, update={'$set': kwargs}, return_document=True) if not pdoc: raise error.ProblemNotFoundError(domain_id, pid) return pdoc
async def post(self, *, tid: int, title: str, content: str, rule: int, private: bool = False, begin_at_date: str, begin_at_time: str, duration: float, pids: str): tdoc = await contest.get(self.domain_id, tid) if not self.own(tdoc, builtin.PERM_EDIT_CONTEST_SELF): self.check_perm(builtin.PERM_EDIT_CONTEST) try: begin_at = datetime.datetime.strptime( begin_at_date + ' ' + begin_at_time, '%Y-%m-%d %H:%M') begin_at = self.timezone.localize(begin_at).astimezone( pytz.utc).replace(tzinfo=None) end_at = begin_at + datetime.timedelta(hours=duration) except ValueError: raise error.ValidationError('begin_at_date', 'begin_at_time') if begin_at >= end_at: raise error.ValidationError('duration') pids = list(OrderedSet(map(int, pids.split(',')))) pdocs = await problem.get_multi(domain_id=self.domain_id, _id={ '$in': pids }, projection={ '_id': 1 }).sort('_id', 1).to_list(None) exist_pids = [pdoc['_id'] for pdoc in pdocs] if len(pids) != len(exist_pids): for pid in pids: if pid not in exist_pids: raise error.ProblemNotFoundError(self.domain_id, pid) await contest.edit(domain_id=self.domain_id, tid=tid, title=title, content=content, rule=rule, private=private, begin_at=begin_at, end_at=end_at, pids=pids) for pid in pids: await problem.set_hidden(self.domain_id, pid, True) self.json_or_redirect(self.reverse_url('contest_detail', tid=tid))
async def send_or_cancel(self, *, tid: int, uid: int, pid: int, balloon: bool = True): tsdoc = await contest.get_status(self.domain_id, tid, uid) pdetail = None for pstatus in tsdoc['detail']: if pstatus['pid'] == pid: pdetail = pstatus break if not pdetail: raise error.ProblemNotFoundError(self.domain_id, pid) if balloon and (not pdetail['accept'] or pdetail['balloon']): raise error.ContestIllegalBalloonError() await contest.set_status_balloon(self.domain_id, tid, uid, pid, balloon) self.json_or_redirect(self.reverse_url('contest_balloon', tid=tid))
async def get(self, *, tid: int, letter: str): uid = self.user['_id'] if self.has_priv( builtin.PRIV_USER_PROFILE) else None tdoc = await contest.get(self.domain_id, tid) pid = contest.convert_to_pid(tdoc['pids'], letter) pdoc = await problem.get(self.domain_id, pid, uid) if not pdoc: raise error.ProblemNotFoundError(self.domain_id, pid, tdoc['_id']) pdoc['letter'] = letter tsdoc, udoc = await asyncio.gather( contest.get_status(self.domain_id, tdoc['_id'], self.user['_id']), user.get_by_uid(tdoc['owner_uid'])) attended = tsdoc and tsdoc.get('attend') == 1 if (contest.RULES[tdoc['rule']].show_func(tdoc, self.now) or self.has_perm(builtin.PERM_VIEW_CONTEST_HIDDEN_STATUS)): rdocs = await record.get_user_in_problem_multi( uid, self.domain_id, pdoc['_id']).sort([('_id', -1)]).to_list(10) else: rdocs = [] if not self.prefer_json: path_components = self.build_path( (self.translate('contest_main'), self.reverse_url('contest_main')), (tdoc['title'], self.reverse_url('contest_detail', tid=tid)), (pdoc['title'], self.reverse_url('contest_detail_problem', tid=tid, letter=pdoc['letter'])), (self.translate('contest_detail_problem_submit'), None)) self.render('problem_submit.html', tdoc=tdoc, pdoc=pdoc, rdocs=rdocs, tsdoc=tsdoc, udoc=udoc, attended=attended, page_title=pdoc['title'], path_components=path_components) else: self.json({'rdocs': rdocs})
async def set_hidden(domain_id: str, pid: int, hidden: bool): pdoc = await edit(domain_id, pid, hidden=hidden) if not pdoc: raise error.ProblemNotFoundError(domain_id, pid) return pdoc
async def set_data(domain_id: str, pid: int, data: objectid.ObjectId): pdoc = await edit(domain_id, pid, data=data) if not pdoc: raise error.ProblemNotFoundError(domain_id, pid) return pdoc