From 150793542d6a0ef35821ebbb977cec6171183196 Mon Sep 17 00:00:00 2001 From: yasminddias Date: Sun, 15 Jun 2025 11:23:23 +0100 Subject: [PATCH] Deleta perspectiva --- backend/projects/perspective/views.py | 36 +++- .../components/perspective/QuestionList.vue | 12 ++ frontend/i18n/en/projects/perspectives.js | 9 +- .../pages/projects/_id/perspectives/index.vue | 167 +++++++++++++++++- .../perspective/apiPerspectiveRepository.ts | 13 +- .../perspectiveApplicationService.ts | 4 + test_delete_all.html | 127 +++++++++++++ 7 files changed, 350 insertions(+), 18 deletions(-) create mode 100644 test_delete_all.html diff --git a/backend/projects/perspective/views.py b/backend/projects/perspective/views.py index bb52060d..52ce17ab 100644 --- a/backend/projects/perspective/views.py +++ b/backend/projects/perspective/views.py @@ -24,7 +24,7 @@ class QuestionViewSet(viewsets.ModelViewSet): ordering = ['order', 'created_at'] def get_permissions(self): - if self.action in ['create', 'update', 'partial_update', 'destroy', 'bulk_create', 'bulk_delete']: + if self.action in ['create', 'update', 'partial_update', 'destroy', 'bulk_create', 'bulk_delete', 'delete_all']: permission_classes = [IsAuthenticated, CanCreatePerspective] else: permission_classes = [IsAuthenticated, CanViewPerspective] @@ -90,6 +90,15 @@ class QuestionViewSet(viewsets.ModelViewSet): return super().list(request, *args, **kwargs) + def destroy(self, request, *args, **kwargs): + """Override destroy to ensure consistent response format""" + instance = self.get_object() + question_text = instance.text[:50] + "..." if len(instance.text) > 50 else instance.text + self.perform_destroy(instance) + return Response({ + 'message': f'Question "{question_text}" deleted successfully' + }, status=status.HTTP_200_OK) + def perform_create(self, serializer): import logging logger = logging.getLogger(__name__) @@ -122,23 +131,36 @@ class QuestionViewSet(viewsets.ModelViewSet): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - @action(detail=False, methods=['delete']) + @action(detail=False, methods=['post']) def bulk_delete(self, request, project_id=None): question_ids = request.data.get('ids', []) if not question_ids: return Response({'error': 'No question IDs provided'}, status=status.HTTP_400_BAD_REQUEST) - + questions = Question.objects.filter( id__in=question_ids, project_id=project_id ) - + deleted_count = questions.count() questions.delete() - + + return Response({ + 'message': f'{deleted_count} questions deleted successfully', + 'deleted_count': deleted_count + }, status=status.HTTP_200_OK) + + @action(detail=False, methods=['post']) + def delete_all(self, request, project_id=None): + """Delete all questions in the project""" + questions = Question.objects.filter(project_id=project_id) + deleted_count = questions.count() + questions.delete() + return Response({ - 'message': f'{deleted_count} questions deleted successfully' - }, status=status.HTTP_204_NO_CONTENT) + 'message': f'{deleted_count} questions deleted successfully', + 'deleted_count': deleted_count + }, status=status.HTTP_200_OK) class AnswerViewSet(viewsets.ModelViewSet): diff --git a/frontend/components/perspective/QuestionList.vue b/frontend/components/perspective/QuestionList.vue index 78539f61..080d03f7 100644 --- a/frontend/components/perspective/QuestionList.vue +++ b/frontend/components/perspective/QuestionList.vue @@ -1,5 +1,17 @@