From 3a81a6c186c3767693be68baf8443889239f7992 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Mon, 6 Mar 2023 18:45:20 +0530 Subject: [PATCH] feat: add endpoint for draft cycles and add validation for creating draft cycles (#355) * feat: add endpoint for draft cycles and add validation for creating draft cycles * fix: key error in cycle create endpoint --- apiserver/plane/api/urls.py | 10 ++++- apiserver/plane/api/views/__init__.py | 1 + apiserver/plane/api/views/cycle.py | 59 ++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index b52c989d9..b73c71b28 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -83,6 +83,7 @@ from plane.api.views import ( CycleDateCheckEndpoint, CurrentUpcomingCyclesEndpoint, CompletedCyclesEndpoint, + DraftCyclesEndpoint, ## End Cycles # Modules ModuleViewSet, @@ -503,12 +504,17 @@ urlpatterns = [ path( "workspaces//projects//cycles/current-upcoming-cycles/", CurrentUpcomingCyclesEndpoint.as_view(), - name="project-cycle", + name="project-cycle-upcoming", ), path( "workspaces//projects//cycles/completed-cycles/", CompletedCyclesEndpoint.as_view(), - name="project-cycle", + name="project-cycle-completed", + ), + path( + "workspaces//projects//cycles/draft-cycles/", + DraftCyclesEndpoint.as_view(), + name="project-cycle-draft", ), ## End Cycles # Issue diff --git a/apiserver/plane/api/views/__init__.py b/apiserver/plane/api/views/__init__.py index 00c652906..4ac45d287 100644 --- a/apiserver/plane/api/views/__init__.py +++ b/apiserver/plane/api/views/__init__.py @@ -45,6 +45,7 @@ from .cycle import ( CycleDateCheckEndpoint, CurrentUpcomingCyclesEndpoint, CompletedCyclesEndpoint, + DraftCyclesEndpoint, ) from .asset import FileAssetEndpoint from .issue import ( diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index de7bb25fd..bade62098 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -45,6 +45,37 @@ class CycleViewSet(BaseViewSet): .distinct() ) + def create(self, request, slug, project_id): + try: + if ( + request.data.get("start_date", None) is None + and request.data.get("end_date", None) is None + ) or ( + request.data.get("start_date", None) is not None + and request.data.get("end_date", None) is not None + ): + serializer = CycleSerializer(data=request.data) + if serializer.is_valid(): + serializer.save( + project_id=project_id, + owned_by=request.user, + ) + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + return Response( + { + "error": "Both start date and end date are either required or are to be null" + }, + status=status.HTTP_400_BAD_REQUEST, + ) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + ) + class CycleIssueViewSet(BaseViewSet): serializer_class = CycleIssueSerializer @@ -275,7 +306,7 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView): class CompletedCyclesEndpoint(BaseAPIView): def get(self, request, slug, project_id): try: - past_cycles = Cycle.objects.filter( + completed_cycles = Cycle.objects.filter( workspace__slug=slug, project_id=project_id, end_date__lte=timezone.now(), @@ -283,7 +314,9 @@ class CompletedCyclesEndpoint(BaseAPIView): return Response( { - "past_cycles": CycleSerializer(past_cycles, many=True).data, + "completed_cycles": CycleSerializer( + completed_cycles, many=True + ).data, }, status=status.HTTP_200_OK, ) @@ -294,3 +327,25 @@ class CompletedCyclesEndpoint(BaseAPIView): {"error": "Something went wrong please try again later"}, status=status.HTTP_400_BAD_REQUEST, ) + + +class DraftCyclesEndpoint(BaseAPIView): + def get(self, request, slug, project_id): + try: + draft_cycles = Cycle.objects.filter( + workspace__slug=slug, + project_id=project_id, + end_date=None, + start_date=None, + ) + + return Response( + {"draft_cycles": CycleSerializer(draft_cycles, many=True).data}, + status=status.HTTP_200_OK, + ) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + )