|
1 | 1 | from django.db import transaction
|
2 |
| -from rest_framework import permissions, status |
| 2 | +from rest_framework import permissions, status, viewsets |
3 | 3 | from rest_framework.decorators import action
|
4 | 4 | from rest_framework.response import Response
|
5 | 5 | from rest_framework.viewsets import GenericViewSet, mixins
|
@@ -145,3 +145,65 @@ def assign(self, request):
|
145 | 145 | @action(detail=False, methods=['post'], url_path='unassign')
|
146 | 146 | def unassign(self, request):
|
147 | 147 | return self._unassign(request)
|
| 148 | + |
| 149 | + |
| 150 | +class ServiceObjectDeleteViewSet(viewsets.ViewSet): |
| 151 | + """ |
| 152 | + Bulk deletion of role assignments for deleted objects. |
| 153 | + Uses standard create() method to bypass service token authentication restrictions. |
| 154 | + Handles both user and team assignments in a single API call. |
| 155 | + """ |
| 156 | + |
| 157 | + permission_classes = [HasResourceRegistryPermissions] |
| 158 | + |
| 159 | + def create(self, request): |
| 160 | + """ |
| 161 | + Delete all role assignments (user and team) for a specific resource. |
| 162 | +
|
| 163 | + Expected request data: |
| 164 | + { |
| 165 | + "resource_type": "main.inventory", |
| 166 | + "resource_pk": "4" |
| 167 | + } |
| 168 | + """ |
| 169 | + from ..models import DABContentType |
| 170 | + |
| 171 | + # Validate request data |
| 172 | + serializer_data = { |
| 173 | + 'resource_type': request.data.get('resource_type'), |
| 174 | + 'resource_pk': request.data.get('resource_pk'), |
| 175 | + } |
| 176 | + |
| 177 | + if not serializer_data['resource_type'] or not serializer_data['resource_pk']: |
| 178 | + return Response({'error': 'Both resource_type and resource_pk are required'}, status=status.HTTP_400_BAD_REQUEST) |
| 179 | + |
| 180 | + try: |
| 181 | + # Parse resource_type (e.g., "main.inventory" -> app_label="main", model="inventory") |
| 182 | + app_label, model_name = serializer_data['resource_type'].split('.', 1) |
| 183 | + except ValueError: |
| 184 | + return Response({'error': 'Invalid resource_type format. Expected: app_label.model_name'}, status=status.HTTP_400_BAD_REQUEST) |
| 185 | + |
| 186 | + try: |
| 187 | + # Get the content type |
| 188 | + content_type = DABContentType.objects.get(app_label=app_label, model=model_name) |
| 189 | + except DABContentType.DoesNotExist: |
| 190 | + return Response({'error': f'Content type not found: {serializer_data["resource_type"]}'}, status=status.HTTP_400_BAD_REQUEST) |
| 191 | + |
| 192 | + # Perform bulk deletion in a transaction |
| 193 | + with transaction.atomic(): |
| 194 | + # Delete user role assignments |
| 195 | + user_deleted_count = RoleUserAssignment.objects.filter(content_type=content_type, object_id=serializer_data['resource_pk']).delete()[0] |
| 196 | + |
| 197 | + # Delete team role assignments |
| 198 | + team_deleted_count = RoleTeamAssignment.objects.filter(content_type=content_type, object_id=serializer_data['resource_pk']).delete()[0] |
| 199 | + |
| 200 | + total_deleted = user_deleted_count + team_deleted_count |
| 201 | + |
| 202 | + return Response( |
| 203 | + { |
| 204 | + 'message': f'Deleted {total_deleted} role assignments for {serializer_data["resource_type"]} {serializer_data["resource_pk"]}', |
| 205 | + 'deleted_count': total_deleted, |
| 206 | + 'breakdown': {'user_assignments_deleted': user_deleted_count, 'team_assignments_deleted': team_deleted_count}, |
| 207 | + }, |
| 208 | + status=status.HTTP_200_OK, |
| 209 | + ) |
0 commit comments