From 88200a93bf3403904bf74d2513dd61490a2218c3 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Tue, 2 May 2023 00:53:08 +0530 Subject: [PATCH] feat: delete imported data as well when the import is deleted (#974) --- apiserver/plane/api/views/importer.py | 35 ++++++++++++++++++++++++++- apiserver/plane/db/models/importer.py | 1 + 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/apiserver/plane/api/views/importer.py b/apiserver/plane/api/views/importer.py index 243f88e3c..b9a7fe0c5 100644 --- a/apiserver/plane/api/views/importer.py +++ b/apiserver/plane/api/views/importer.py @@ -28,6 +28,7 @@ from plane.db.models import ( Module, ModuleLink, ModuleIssue, + Label, ) from plane.api.serializers import ( ImporterSerializer, @@ -235,9 +236,20 @@ class ImportServiceEndpoint(BaseAPIView): def delete(self, request, slug, service, pk): try: - importer = Importer.objects.filter( + importer = Importer.objects.get( pk=pk, service=service, workspace__slug=slug ) + # Delete all imported Issues + imported_issues = importer.imported_data.get("issues", []) + Issue.objects.filter(id__in=imported_issues).delete() + + # Delete all imported Labels + imported_labels = importer.imported_data.get("labels", []) + Label.objects.filter(id__in=imported_labels).delete() + + if importer.service == "jira": + imported_modules = importer.imported_data.get("modules", []) + Module.objects.filter(id__in=imported_modules).delete() importer.delete() return Response(status=status.HTTP_204_NO_CONTENT) except Exception as e: @@ -247,6 +259,27 @@ class ImportServiceEndpoint(BaseAPIView): status=status.HTTP_400_BAD_REQUEST, ) + def patch(self, request, slug, service, pk): + try: + importer = Importer.objects.get( + pk=pk, service=service, workspace__slug=slug + ) + serializer = ImporterSerializer(importer, data=request.data, partial=True) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + except Importer.DoesNotExist: + return Response( + {"error": "Importer Does not exists"}, status=status.HTTP_404_NOT_FOUND + ) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + ) + class UpdateServiceImportStatusEndpoint(BaseAPIView): def post(self, request, slug, project_id, service, importer_id): diff --git a/apiserver/plane/db/models/importer.py b/apiserver/plane/db/models/importer.py index a61aae48c..a2d1d3166 100644 --- a/apiserver/plane/db/models/importer.py +++ b/apiserver/plane/db/models/importer.py @@ -33,6 +33,7 @@ class Importer(ProjectBaseModel): token = models.ForeignKey( "db.APIToken", on_delete=models.CASCADE, related_name="importer" ) + imported_data = models.JSONField(null=True) class Meta: verbose_name = "Importer"