(self)
| 237 | |
| 238 | @transaction.atomic |
| 239 | def delete(self): |
| 240 | self.is_valid(raise_exception=True) |
| 241 | Folder = get_folder_type(self.data.get('source')) # noqa |
| 242 | Source = get_source_type(self.data.get('source')) # noqa |
| 243 | folder = Folder.objects.filter(id=self.data.get('id')).first() |
| 244 | if not folder: |
| 245 | raise serializers.ValidationError(_('Folder does not exist')) |
| 246 | if folder.id == folder.workspace_id: |
| 247 | raise serializers.ValidationError(_('Cannot delete root folder')) |
| 248 | |
| 249 | # 工作空间管理员可以删除 |
| 250 | workspace_manage = is_workspace_manage(self.data.get('user_id'), self.data.get('workspace_id')) |
| 251 | if workspace_manage: |
| 252 | nodes = Folder.objects.filter(id=self.data.get('id')).get_descendants(include_self=True) |
| 253 | for node in nodes: |
| 254 | # print(node) |
| 255 | # 删除相关的资源 |
| 256 | self.delete_source(node) |
| 257 | # 删除节点 |
| 258 | node.delete() |
| 259 | # 普通用户删除的文件夹内全部都得是自己有权限的资源 |
| 260 | else: |
| 261 | nodes = Folder.objects.filter(id=self.data.get('id')).get_descendants(include_self=True) |
| 262 | for node in nodes: |
| 263 | # 删除相关的资源 |
| 264 | source_ids = (Source.objects.filter(folder_id=node.id) |
| 265 | .annotate(id_str=Cast('id', TextField())) |
| 266 | .values_list('id_str', flat=True)) |
| 267 | # 检查文件夹是否存在未授权当前用户的资源 |
| 268 | auth_list = QuerySet(WorkspaceUserResourcePermission).filter( |
| 269 | Q(workspace_id=self.data.get('workspace_id')) & |
| 270 | Q(user_id=self.data.get('user_id')) & |
| 271 | Q(auth_target_type=self.data.get('source')) & |
| 272 | Q(target__in=source_ids) & |
| 273 | Q(permission_list__overlap=[ResourcePermission.MANAGE, ResourcePermissionRole.ROLE]) |
| 274 | ).count() |
| 275 | if auth_list != len(source_ids): |
| 276 | raise AppApiException(500, _('This folder contains resources that you dont have permission')) |
| 277 | self.delete_source(node) |
| 278 | node.delete() |
| 279 | |
| 280 | def delete_source(self, node): |
| 281 | Source = get_source_type(self.data.get('source')) # noqa |
no test coverage detected