async def get(self, *, doc_type: int=None, doc_id: str, page: int=1): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) nodes, vnode = await discussion.get_nodes_and_vnode(self.domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(self.domain_id, node_or_dtuple) if vnode['doc_type'] == document.TYPE_PROBLEM and vnode.get('hidden', False): self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN) # TODO(twd2): do more visibility check eg. contest # TODO(iceboy): continuation based pagination. ddocs, dpcount, _ = await pagination.paginate( discussion.get_multi(self.domain_id, parent_doc_type=vnode['doc_type'], parent_doc_id=vnode['doc_id']), page, self.DISCUSSIONS_PER_PAGE) uids = set(ddoc['owner_uid'] for ddoc in ddocs) if 'owner_uid' in vnode: uids.add(vnode['owner_uid']) udict = await user.get_dict(uids) vndict = {node_or_dtuple: vnode} vncontext = {} # TODO(twd2): eg. psdoc, tsdoc, ... path_components = self.build_path( (self.translate('discussion_main'), self.reverse_url('discussion_main')), (vnode['title'], None)) self.render('discussion_main_or_node.html', discussion_nodes=nodes, vnode=vnode, ddocs=ddocs, udict=udict, vndict=vndict, page=page, dpcount=dpcount, **vncontext, path_components=path_components)
async def get(self, *, pid): uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None pdoc = await problem.get(self.domain_id, document.convert_doc_id(pid), uid) path_components = self.build_path(('problem_main', self.reverse_url('problem_main')), (pdoc['title'], None)) self.render('problem_submit.html', pdoc=pdoc, page_title=pdoc['title'], path_components=path_components)
async def stream_data(self, *, pid, headers_only=False): # Judge will have PRIV_READ_PROBLEM_DATA, domain administrator will have PERM_READ_PROBLEM_DATA. if not self.has_priv(builtin.PRIV_READ_PROBLEM_DATA): self.check_perm(builtin.PERM_READ_PROBLEM_DATA) grid_out = await problem.get_data(self.domain_id, document.convert_doc_id(pid)) self.response.content_type = grid_out.content_type or 'application/octet-stream' self.response.last_modified = grid_out.upload_date self.response.headers['Etag'] = '"%s"' % grid_out.md5 # TODO(iceboy): Handle If-Modified-Since & If-None-Match here. self.response.content_length = grid_out.length if not headers_only: await self.response.prepare(self.request) # TODO(twd2): Range remaining = grid_out.length chunk = await grid_out.readchunk() while chunk and remaining >= len(chunk): self.response.write(chunk) remaining -= len(chunk) _, chunk = await asyncio.gather(self.response.drain(), grid_out.readchunk()) if chunk: self.response.write(chunk[:remaining]) await self.response.drain() await self.response.write_eof()
async def post(self, *, doc_type: int = None, doc_id: str, title: str, content: str, highlight: str = None): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) vnode = await discussion.get_vnode(self.domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(self.domain_id, node_or_dtuple) if vnode['doc_type'] == document.TYPE_PROBLEM and vnode.get( 'hidden', False): self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN) # TODO(twd2): do more visibility check eg. contest flags = {} if highlight: self.check_perm(builtin.PERM_HIGHLIGHT_DISCUSSION) flags['highlight'] = True did = await discussion.add(self.domain_id, node_or_dtuple, self.user['_id'], title, content, self.remote_ip, **flags) self.json_or_redirect(self.reverse_url('discussion_detail', did=did), did=did)
async def get(self, *, doc_type: int=None, doc_id: str, page: int=1): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) nodes, vnode = await discussion.get_nodes_and_vnode(self.domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(self.domain_id, node_or_dtuple) if vnode['doc_type'] == document.TYPE_PROBLEM and vnode.get('hidden', False): self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN) # TODO(twd2): do more visibility check eg. contest # TODO(iceboy): continuation based pagination. ddocs, dpcount, _ = await pagination.paginate( discussion.get_multi(self.domain_id, parent_doc_type=vnode['doc_type'], parent_doc_id=vnode['doc_id']), page, self.DISCUSSIONS_PER_PAGE) uids = set(ddoc['owner_uid'] for ddoc in ddocs) if 'owner_uid' in vnode: uids.add(vnode['owner_uid']) udict, dudict = await asyncio.gather(user.get_dict(uids), domain.get_dict_user_by_uid(self.domain_id, uids)) vndict = {node_or_dtuple: vnode} vncontext = {} # TODO(twd2): eg. psdoc, tsdoc, ... path_components = self.build_path( (self.translate('discussion_main'), self.reverse_url('discussion_main')), (vnode['title'], None)) self.render('discussion_main_or_node.html', discussion_nodes=nodes, vnode=vnode, ddocs=ddocs, udict=udict, dudict=dudict, vndict=vndict, page=page, dpcount=dpcount, **vncontext, path_components=path_components)
async def get_filter_query(self, uid_or_name, pid, tid): query = dict() if uid_or_name: try: query['uid'] = int(uid_or_name) except ValueError: udoc = await user.get_by_uname(uid_or_name) if not udoc: raise error.UserNotFoundError(uid_or_name) from None query['uid'] = udoc['_id'] if pid or tid: query['domain_id'] = self.domain_id if pid: query['pid'] = document.convert_doc_id(pid) if tid: query['tid'] = document.convert_doc_id(tid) return query
async def get(self, *, pid): pdoc = await problem.get(self.domain_id, document.convert_doc_id(pid)) psdocs = await problem.get_list_solution(self.domain_id, pdoc['doc_id']) path_components = self.build_path( ('problem_main', self.reverse_url('problem_main')), (pdoc['title'], self.reverse_url('problem_detail', pid=pdoc['doc_id'])), ('problem_solution', None)) self.render('problem_solution.html', psdocs=psdocs, path_components=path_components)
async def post_delete(self, *, did: document.convert_doc_id, **kwargs): did = document.convert_doc_id(did) ddoc = await discussion.get(self.domain_id, did) if not ddoc: raise error.DiscussionNotFoundError(self.domain_id, did) if not self.own(ddoc, builtin.PERM_DELETE_DISCUSSION_SELF): self.check_perm(builtin.PERM_DELETE_DISCUSSION) await discussion.delete(self.domain_id, did) self.json_or_redirect( node_url(self, 'discussion_node', discussion.node_id(ddoc)))
async def get(self, *, q: str): q = q.strip() if not q: self.json_or_redirect(self.referer_or_main) return try: pdoc = await problem.get(self.domain_id, document.convert_doc_id(q)) except error.ProblemNotFoundError: pdoc = None if pdoc: self.redirect(self.reverse_url('problem_detail', pid=pdoc['doc_id'])) return self.redirect('http://cn.bing.com/search?q={0}+site%3A{1}' \ .format(parse.quote(q), parse.quote(options.url_prefix)))
async def get(self, *, doc_type: int=None, doc_id: str): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) nodes, vnode = await discussion.get_nodes_and_vnode(self.domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(self.domain_id, node_or_dtuple) if vnode['doc_type'] == document.TYPE_PROBLEM and vnode.get('hidden', False): self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN) # TODO(twd2): do more visibility check eg. contest path_components = self.build_path( (self.translate('discussion_main'), self.reverse_url('discussion_main')), (vnode['title'], node_url(self, 'discussion_node', node_or_dtuple)), (self.translate('discussion_create'), None)) self.render('discussion_create.html', vnode=vnode, path_components=path_components)
async def post(self, *, doc_type: int = None, doc_id: str, title: str, content: str, highlight: str = None): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) flags = {} if highlight: self.check_perm(builtin.PERM_HIGHLIGHT_DISCUSSION) flags['highlight'] = True did = await discussion.add(self.domain_id, node_or_dtuple, self.user['_id'], title, content, **flags) self.json_or_redirect(self.reverse_url('discussion_detail', did=did), did=did)
async def post(self, *, doc_type: int=None, doc_id: str, title: str, content: str, highlight: str=None): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) vnode = await discussion.get_vnode(self.domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(self.domain_id, node_or_dtuple) if vnode['doc_type'] == document.TYPE_PROBLEM and vnode.get('hidden', False): self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN) # TODO(twd2): do more visibility check eg. contest flags = {} if highlight: self.check_perm(builtin.PERM_HIGHLIGHT_DISCUSSION) flags['highlight'] = True did = await discussion.add(self.domain_id, node_or_dtuple, self.user['_id'], title, content, self.remote_ip, **flags) self.json_or_redirect(self.reverse_url('discussion_detail', did=did), did=did)
async def get(self, *, doc_type: int = None, doc_id: str): if doc_type is None: node_or_dtuple = doc_id else: node_or_dtuple = (doc_type, document.convert_doc_id(doc_id)) nodes, vnode = await discussion.get_nodes_and_vnode( self.domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(self.domain_id, node_or_dtuple) path_components = self.build_path( (self.translate('discussion_main'), self.reverse_url('discussion_main')), (vnode['title'], node_url(self, 'discussion_node', node_or_dtuple)), (self.translate('discussion_create'), None)) self.render('discussion_create.html', vnode=vnode, path_components=path_components)
async def post_submit(self, *, pid, content): pdoc = await problem.get(self.domain_id, document.convert_doc_id(pid)) await problem.add_solution(self.domain_id, pdoc['doc_id'], self.user['_id'], content) self.json_or_redirect(self.reverse_url('problem_solution', pid=pdoc['doc_id']))
async def star_unstar(self, *, pid, star): pdoc = await problem.get(self.domain_id, document.convert_doc_id(pid)) pdoc = await problem.set_star(self.domain_id, pdoc['doc_id'], self.user['_id'], star) self.json_or_redirect(self.referer_or_main, star=pdoc['star'])
async def post(self, *, pid, lang, code): pdoc = await problem.get(self.domain_id, document.convert_doc_id(pid)) rid = await record.add(self.domain_id, pdoc['doc_id'], self.user['_id'], lang, code) await asyncio.gather(queue.publish('judge', rid=rid), bus.publish('record_change', rid)) self.json_or_redirect(self.reverse_url('record_main'))
def test_convert_doc_id(self): doc_id = objectid.ObjectId() self.assertEqual(document.convert_doc_id(doc_id), doc_id) self.assertEqual(document.convert_doc_id(str(doc_id)), doc_id) self.assertEqual(document.convert_doc_id(70514), 70514)
async def on_open(self): await super(ProblemPretestConnection, self).on_open() self.pid = document.convert_doc_id(self.request.match_info['pid']) bus.subscribe(self.on_record_change, ['record_change'])
async def probe_solution(self, pid, psid): pdoc = await problem.get(self.domain_id, document.convert_doc_id(pid)) psdoc = await problem.get_solution(self.domain_id, document.convert_doc_id(psid), pdoc['doc_id']) return pdoc, psdoc
async def get(self, *, uid_or_name: str = '', pid: str = '', tid: str = ''): query = {} if uid_or_name: try: query['uid'] = int(uid_or_name) except ValueError: udoc = await user.get_by_uname(uid_or_name) if not udoc: raise error.UserNotFoundError(uid_or_name) from None query['uid'] = udoc['_id'] if pid: pid = document.convert_doc_id(pid) query['domain_id'] = self.domain_id query['pid'] = pid if tid: tid = document.convert_doc_id(tid) query['domain_id'] = self.domain_id query['tid'] = tid # TODO(iceboy): projection, pagination. rdocs = await record.get_all_multi( **query, get_hidden=self.has_priv(builtin.PRIV_VIEW_HIDDEN_RECORD)).sort([ ('_id', -1) ]).limit(50).to_list(None) # TODO(iceboy): projection. udict, pdict = await asyncio.gather( user.get_dict(rdoc['uid'] for rdoc in rdocs), problem.get_dict_multi_domain( (rdoc['domain_id'], rdoc['pid']) for rdoc in rdocs)) # statistics statistics = None if self.has_priv(builtin.PRIV_VIEW_JUDGE_STATISTICS): ts = calendar.timegm(datetime.datetime.utcnow().utctimetuple()) day_count, week_count, month_count, year_count, rcount = await asyncio.gather( record.get_count( objectid.ObjectId( struct.pack('>i', ts - 24 * 3600) + struct.pack('b', -1) * 8)), record.get_count( objectid.ObjectId( struct.pack('>i', ts - 7 * 24 * 3600) + struct.pack('b', -1) * 8)), record.get_count( objectid.ObjectId( struct.pack('>i', ts - 30 * 24 * 3600) + struct.pack('b', -1) * 8)), record.get_count( objectid.ObjectId( struct.pack('>i', ts - int(365.2425 * 24 * 3600)) + struct.pack('b', -1) * 8)), record.get_count()) statistics = { 'day': day_count, 'week': week_count, 'month': month_count, 'year': year_count, 'total': rcount } self.render('record_main.html', rdocs=rdocs, udict=udict, pdict=pdict, statistics=statistics, filter_uid_or_name=uid_or_name, filter_pid=pid, filter_tid=tid)