def _parse_dag_json(dag): try: dag = json.decode(dag) except decoder.JSONDecodeError: raise error.ValidationError('dag') from None if not isinstance(dag, list): raise error.ValidationError('dag') new_dag = [] try: for node in dag: if any(k not in node for k in ['_id', 'require_nids', 'pids']): raise error.ValidationError('dag') new_node = {'_id': int(node['_id']), 'title': str(node.get('title', '')), 'require_nids': misc.dedupe(map(int, node['require_nids'])), 'pids': misc.dedupe(map(document.convert_doc_id, node['pids']))} new_dag.append(new_node) except ValueError: raise error.ValidationError('dag') from None return new_dag
async def post(self, *, src_domain_id: str, src_pids: str, numeric_pid: bool = False, hidden: bool = False): src_ddoc, src_dudoc = await asyncio.gather( domain.get(src_domain_id), domain.get_user(src_domain_id, self.user['_id'])) if not src_dudoc: src_dudoc = {} if not self.dudoc_has_perm(ddoc=src_ddoc, dudoc=src_dudoc, udoc=self.user, perm=builtin.PERM_VIEW_PROBLEM): # TODO: This is the source domain's PermissionError. raise error.PermissionError(builtin.PERM_VIEW_PROBLEM) src_pids = misc.dedupe( map(document.convert_doc_id, src_pids.replace('\r\n', '\n').split('\n'))) if len(src_pids) > self.MAX_PROBLEMS_PER_REQUEST: raise error.BatchCopyLimitExceededError( self.MAX_PROBLEMS_PER_REQUEST, len(src_pids)) pdocs = await problem.get_multi(domain_id=src_domain_id, doc_id={'$in': src_pids}) \ .sort('doc_id', 1) \ .to_list() exist_pids = [pdoc['doc_id'] for pdoc in pdocs] if len(src_pids) != len(exist_pids): for pid in src_pids: if pid not in exist_pids: raise error.ProblemNotFoundError(src_domain_id, pid) for pdoc in pdocs: if pdoc.get('hidden', False): if not self.dudoc_has_perm( ddoc=src_ddoc, dudoc=src_dudoc, udoc=self.user, perm=builtin.PERM_VIEW_PROBLEM_HIDDEN): # TODO: This is the source domain's PermissionError. raise error.PermissionError( builtin.PERM_VIEW_PROBLEM_HIDDEN) for pdoc in pdocs: pid = None if numeric_pid: pid = await domain.inc_pid_counter(self.domain_id) await problem.copy(pdoc, self.domain_id, self.user['_id'], pid, hidden) self.redirect(self.reverse_url('problem_main'))
def _parse_pids(pids_str): pids = misc.dedupe(map(document.convert_doc_id, pids_str.split(','))) return pids
def test_dedupe(self): self.assertListEqual(misc.dedupe([2,1,1,3,2,3]),[2,1,3]) self.assertListEqual(misc.dedupe([]),[]) self.assertListEqual(misc.dedupe(map(int,['2','1','1','3','2','3'])),[2,1,3]) self.assertListEqual(misc.dedupe(['b','a','b','c','b']),['b','a','c']) self.assertListEqual(misc.dedupe([0]),[0])