def test_get_node_detail_with_scope(self): node, _ = Node.retrieve_node('d_level_1') node.user_scope = ['not_existed'] node.save() res = self.client.get(reverse('siteapi:node_detail', args=('d_level_1', ))) self.assertEqual([], res.json()['user_scope'])
def core_post(users_file, node_uid): ''' creat or update users ''' cli = CLI() reader = csv.DictReader(io.StringIO(users_file.read().decode('utf-8'))) res = [] users = [] for index, row in enumerate(reader): username = row.get('username') user = User.valid_objects.filter(username=username).first() try: if user: cli.update_user(user, row) else: user = cli.create_user(row) except ValidationError as exc: raise ValidationError({index + 1: exc.detail}) users.append(user) # 重复导入的后果可以接受,故不处理 res.append(UserCSVSerializer(user).data) if node_uid: node, node_subject = TreeNode.retrieve_node(node_uid) if node_subject == 'dept': cli.add_users_to_dept(users, node) elif node_subject == 'group': cli.add_users_to_group(users, node) else: raise ValueError return res
def update_nodes_cache(node_uids): ''' 更新节点缓存 ''' print("update_nodes_cache", node_uids) nodes = Node.retrieve_nodes(node_uids) for node in nodes: node.update_cache()
def all_manage_node_uids(self): ''' 所有可管理的节点(包含直接管理的节点,及其下属节点) ''' res = set() for node_uid in self.manage_node_uids: res.update(Node.get_downstream_uids(node_uid)) return res
def get_nodes(instance): ''' 管理节点范围 ''' if instance.scope_subject == 2: for node in Node.retrieve_nodes(instance.nodes): yield { 'node_uid': node.node_uid, 'node_subject': node.node_subject, 'name': node.name, }
def retrieve_nodes(request, uids): ''' 获取节点 ''' res = [] for uid in uids: node, _ = Node.retrieve_node(uid) if not node or not node.is_visible_to_manager(request.user): raise ValidationError({'node_uids': [f'{uid} not found']}) res.append(node.detail_serializer.data) return res
def all_node_uids(self): ''' 所有直属节点以及隶属节点的uid ''' key = f'oneid:user:{self.username}:upstream_node' cache_data = cache.get(key) if cache_data is None: res = set() for parent_node in Node.retrieve_nodes(self.node_uids): res.update(parent_node.upstream_uids) cache.set(key, res) return res return cache_data
def all_manage_node_uids(self): ''' 所有可管理的节点(包含直接管理的节点,及其下属节点) ''' res = set() for node_uid in self.manage_node_uids: res.update(Node.get_downstream_uids(node_uid)) # 过滤根节点管理权限 discard_node = {'g_root', 'g_manager', 'g_extern', 'g_intra'} for node in discard_node: res.discard(node) return res
def get_object(self): ''' find node ''' node, _ = Node.retrieve_node(self.kwargs['uid']) if node is None: raise NotFound if node.__class__ == Dept: self.serializer_class = DeptTreeSerializer else: self.serializer_class = GroupTreeSerializer return node
def post(self, request, *args, **kwargs): # pylint: disable=unused-argument ''' 导入 ''' users_file = request.FILES.get('users', None) if not users_file: raise ValidationError({'users': ["this field is required"]}) node_uid = request.data.get('node_uid', '') if node_uid: node, _ = TreeNode.retrieve_node(node_uid) if not node: raise NotFound res = self.core_post(users_file, node_uid='') return Response(res)
def get_object(self): ''' find node ''' node, node_subject = Node.retrieve_node(self.kwargs['uid']) if node is None: raise NotFound try: self.check_object_permissions(self.request, node) except PermissionDenied: raise NotFound if node_subject == 'dept': self.serializer_class = DeptSerializer else: self.serializer_class = GroupSerializer return node