From 0b94172ef7f37c963b6eda3a9d9d2b4ab82f88a2 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Tue, 20 Dec 2022 00:31:43 +0530 Subject: [PATCH] refactor: remove bulk assign endpoint, merge it into a single create endpoint and update the create endpoint to maintain db integrity --- apiserver/plane/api/urls.py | 6 ------ apiserver/plane/api/views/__init__.py | 2 +- apiserver/plane/api/views/cycle.py | 30 +++++++++++++++++---------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index d4a9faa6f..b04f9dc3a 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -52,7 +52,6 @@ from plane.api.views import ( AddMemberToProjectEndpoint, ProjectJoinEndpoint, BulkDeleteIssuesEndpoint, - BulkAssignIssuesToCycleEndpoint, ProjectUserViewsEndpoint, ModuleViewSet, ModuleIssueViewSet, @@ -444,11 +443,6 @@ urlpatterns = [ ), name="project-cycle", ), - path( - "workspaces//projects//cycles//bulk-assign-issues/", - BulkAssignIssuesToCycleEndpoint.as_view(), - name="bulk-assign-cycle-issues", - ), ## End Cycles # Issue path( diff --git a/apiserver/plane/api/views/__init__.py b/apiserver/plane/api/views/__init__.py index 5706b1994..b641d6e2c 100644 --- a/apiserver/plane/api/views/__init__.py +++ b/apiserver/plane/api/views/__init__.py @@ -38,7 +38,7 @@ from .workspace import ( from .state import StateViewSet from .shortcut import ShortCutViewSet from .view import ViewViewSet -from .cycle import CycleViewSet, CycleIssueViewSet, BulkAssignIssuesToCycleEndpoint +from .cycle import CycleViewSet, CycleIssueViewSet from .asset import FileAssetEndpoint from .issue import ( IssueViewSet, diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index 8b74f2a10..873f9518c 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -1,9 +1,10 @@ # Third party imports from rest_framework.response import Response from rest_framework import status +from sentry_sdk import capture_exception # Module imports -from . import BaseViewSet, BaseAPIView +from . import BaseViewSet from plane.api.serializers import CycleSerializer, CycleIssueSerializer from plane.api.permissions import ProjectEntityPermission from plane.db.models import Cycle, CycleIssue, Issue @@ -66,26 +67,27 @@ class CycleIssueViewSet(BaseViewSet): .distinct() ) - -class BulkAssignIssuesToCycleEndpoint(BaseAPIView): - - permission_classes = [ - ProjectEntityPermission, - ] - - def post(self, request, slug, project_id, cycle_id): + def create(self, request, slug, project_id, cycle_id): try: - issue_ids = request.data.get("issue_ids") + issues = request.data.get("issue", []) + + if not len(issues): + return Response( + {"error": "Issues are required"}, status=status.HTTP_400_BAD_REQUEST + ) cycle = Cycle.objects.get( workspace__slug=slug, project_id=project_id, pk=cycle_id ) issues = Issue.objects.filter( - pk__in=issue_ids, workspace__slug=slug, project_id=project_id + pk__in=issues, workspace__slug=slug, project_id=project_id ) + # Delete old records in order to maintain the database integrity + CycleIssue.objects.filter(issue_id__in=issues).delete() + CycleIssue.objects.bulk_create( [ CycleIssue( @@ -107,3 +109,9 @@ class BulkAssignIssuesToCycleEndpoint(BaseAPIView): return Response( {"error": "Cycle not found"}, 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_200_OK, + )