(self, instance)
| 180 | |
| 181 | @transaction.atomic |
| 182 | def edit(self, instance): |
| 183 | self.is_valid(raise_exception=True) |
| 184 | Folder = get_folder_type(self.data.get('source')) # noqa |
| 185 | current_id = self.data.get('id') |
| 186 | current_node = Folder.objects.get(id=current_id) |
| 187 | if current_node is None: |
| 188 | raise serializers.ValidationError(_('Folder does not exist')) |
| 189 | # 模块间的移动 |
| 190 | parent_id = instance.get('parent_id') |
| 191 | if parent_id is None: |
| 192 | parent_id = current_node.parent_id |
| 193 | # 如果要修改文件夹名称,检查同级目录下是否存在同名文件夹 |
| 194 | new_name = instance.get('name') |
| 195 | if new_name is not None and new_name != current_node.name: |
| 196 | if QuerySet(Folder).filter( |
| 197 | name=new_name, |
| 198 | parent_id=parent_id, |
| 199 | workspace_id=current_node.workspace_id |
| 200 | ).exclude(id=current_id).exists(): |
| 201 | raise AppApiException(500, _('Folder name already exists')) |
| 202 | |
| 203 | edit_field_list = ['name', 'desc'] |
| 204 | edit_dict = {field: instance.get(field) for field in edit_field_list if ( |
| 205 | field in instance and instance.get(field) is not None)} |
| 206 | |
| 207 | QuerySet(Folder).filter(id=current_id).update(**edit_dict) |
| 208 | current_node.refresh_from_db() |
| 209 | |
| 210 | if parent_id is not None and current_id != current_node.workspace_id and current_node.parent_id != parent_id: |
| 211 | |
| 212 | source_type = self.data.get('source') |
| 213 | if has_target_permission(current_node.workspace_id, source_type, self.data.get('user_id'), |
| 214 | parent_id) or is_workspace_manage(self.data.get('user_id'), |
| 215 | current_node.workspace_id): |
| 216 | current_depth = get_max_depth(current_node) |
| 217 | check_depth(self.data.get('source'), parent_id, current_node.workspace_id, current_depth) |
| 218 | parent = Folder.objects.get(id=parent_id) |
| 219 | |
| 220 | if QuerySet(Folder).filter(name=current_node.name, parent_id=parent_id, |
| 221 | workspace_id=current_node.workspace_id).exists(): |
| 222 | raise AppApiException(500, _('Folder name already exists')) |
| 223 | |
| 224 | current_node.parent = parent |
| 225 | current_node.save() |
| 226 | current_node.refresh_from_db() |
| 227 | else: |
| 228 | raise AppApiException(403, _('No permission for the target folder')) |
| 229 | |
| 230 | return self.one() |
| 231 | |
| 232 | def one(self): |
| 233 | self.is_valid(raise_exception=True) |
no test coverage detected