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 = 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 add(domain_id: str, node_or_dtuple: str, owner_uid: int, title: str, content: str, **flags): validator.check_title(title) validator.check_content(content) vnode = await get_vnode(domain_id, node_or_dtuple) if not vnode: raise error.DiscussionNodeNotFoundError(domain_id, node_or_dtuple) return await document.add(domain_id, content, owner_uid, document.TYPE_DISCUSSION, title=title, num_replies=0, views=0, **flags, update_at=datetime.datetime.utcnow(), parent_doc_type=vnode['doc_type'], parent_doc_id=vnode['doc_id'])
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 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 check_node(domain_id, node_name): if not await get_exist_node(domain_id, node_name): raise error.DiscussionNodeNotFoundError(domain_id, node_name)