def list(self, request: Request, *args, **kwargs): user = self.user key = self.ensure_key() nodes_query_utils = UserGrantedNodesQueryUtils(user) assets_query_utils = UserGrantedAssetsQueryUtils(user) nodes = PermNode.objects.none() assets = Asset.objects.none() all_tree_nodes = [] if not key: nodes = nodes_query_utils.get_top_level_nodes() elif key == PermNode.UNGROUPED_NODE_KEY: assets = assets_query_utils.get_ungroup_assets() elif key == PermNode.FAVORITE_NODE_KEY: assets = assets_query_utils.get_favorite_assets() else: nodes = nodes_query_utils.get_node_children(key) assets = assets_query_utils.get_node_assets(key) assets = assets.prefetch_related('platform') tree_nodes = self.serialize_nodes(nodes, with_asset_amount=True) tree_assets = self.serialize_assets(assets, key) all_tree_nodes.extend(tree_nodes) all_tree_nodes.extend(tree_assets) return Response(data=all_tree_nodes)
def get_queryset(self): if getattr(self, 'swagger_fake_view', False): return Asset.objects.none() user = self.user utils = UserGrantedAssetsQueryUtils(user) assets = utils.get_favorite_assets() assets = assets.prefetch_related('platform').only(*self.only_fields) return assets
def get_queryset(self): if getattr(self, 'swagger_fake_view', False): return Asset.objects.none() queryset = UserGrantedAssetsQueryUtils(self.user) \ .get_all_granted_assets() queryset = queryset.prefetch_related('platform').only( *self.only_fields) return queryset
def add_assets(self, data: list, assets_query_utils: UserGrantedAssetsQueryUtils): if settings.PERM_SINGLE_ASSET_TO_UNGROUP_NODE: all_assets = assets_query_utils.get_direct_granted_nodes_assets() else: all_assets = assets_query_utils.get_all_granted_assets() all_assets = all_assets.annotate( parent_key=F('nodes__key')).prefetch_related('platform') data.extend(self.serialize_assets(all_assets))
def get_queryset(self): if getattr(self, 'swagger_fake_view', False): return Asset.objects.none() node_id = self.kwargs.get("node_id") node, assets = UserGrantedAssetsQueryUtils( self.user).get_node_all_assets(node_id) assets = assets.prefetch_related('platform').only(*self.only_fields) self.pagination_node = node return assets
def list(self, request: Request, *args, **kwargs): """ 此算法依赖 UserGrantedMappingNode 获取所有授权的节点和资产 Node = UserGrantedMappingNode + 授权节点的子节点 Asset = 授权节点的资产 + 直接授权的资产 """ user = request.user data = [] asset_perm_ids = get_user_all_asset_perm_ids(user) system_user_id = request.query_params.get('system_user') if system_user_id: asset_perm_ids = list(AssetPermission.objects.valid().filter( id__in=asset_perm_ids, system_users__id=system_user_id, actions__gt=0).values_list('id', flat=True).distinct()) nodes_query_utils = UserGrantedNodesQueryUtils(user, asset_perm_ids) assets_query_utils = UserGrantedAssetsQueryUtils(user, asset_perm_ids) self.add_ungrouped_resource(data, nodes_query_utils, assets_query_utils) self.add_favorite_resource(data, nodes_query_utils, assets_query_utils) if system_user_id: # 有系统用户筛选的需要重新计算树结构 self.add_node_filtered_by_system_user(data, user, asset_perm_ids) else: all_nodes = nodes_query_utils.get_whole_tree_nodes( with_special=False) data.extend(self.serialize_nodes(all_nodes, with_asset_amount=True)) self.add_assets(data, assets_query_utils) return Response(data=data)
def get_queryset(self): pk = self.kwargs.get("pk") query_utils = UserGrantedAssetsQueryUtils(None, asset_perm_ids=[pk]) assets = query_utils.get_all_granted_assets() return assets