From 174bf70cce9dde8eb62bc333a4dc1dd078c2c8be Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Wed, 22 Nov 2023 15:31:29 +0530 Subject: [PATCH] chore: api fixes --- apiserver/plane/api/serializers/issue.py | 2 +- apiserver/plane/api/urls/cycle.py | 2 +- apiserver/plane/api/urls/issue.py | 12 ++++++------ apiserver/plane/api/urls/module.py | 2 +- apiserver/plane/api/urls/state.py | 5 +++++ apiserver/plane/api/views/cycle.py | 8 ++++---- apiserver/plane/api/views/issue.py | 23 +++++++++++------------ apiserver/plane/api/views/module.py | 18 +++++++++++++----- apiserver/plane/api/views/state.py | 22 ++++++++++------------ apiserver/plane/app/views/base.py | 11 +++++++---- apiserver/plane/app/views/cycle.py | 4 ++-- apiserver/plane/app/views/module.py | 4 ++-- 12 files changed, 63 insertions(+), 50 deletions(-) diff --git a/apiserver/plane/api/serializers/issue.py b/apiserver/plane/api/serializers/issue.py index f628c0358..2dafaa7e6 100644 --- a/apiserver/plane/api/serializers/issue.py +++ b/apiserver/plane/api/serializers/issue.py @@ -308,5 +308,5 @@ class IssueActivitySerializer(BaseSerializer): model = IssueActivity exclude = [ "created_by", - "udpated_by", + "updated_by", ] diff --git a/apiserver/plane/api/urls/cycle.py b/apiserver/plane/api/urls/cycle.py index b377aa4bc..f557f8af0 100644 --- a/apiserver/plane/api/urls/cycle.py +++ b/apiserver/plane/api/urls/cycle.py @@ -23,7 +23,7 @@ urlpatterns = [ name="cycle-issues", ), path( - "workspaces//projects//cycles//cycle-issues//", + "workspaces//projects//cycles//cycle-issues//", CycleIssueAPIEndpoint.as_view(), name="cycle-issues", ), diff --git a/apiserver/plane/api/urls/issue.py b/apiserver/plane/api/urls/issue.py index e6770579d..070ea8bd9 100644 --- a/apiserver/plane/api/urls/issue.py +++ b/apiserver/plane/api/urls/issue.py @@ -20,22 +20,22 @@ urlpatterns = [ name="issue", ), path( - "workspaces//projects//issue-labels/", + "workspaces//projects//labels/", LabelAPIEndpoint.as_view(), name="label", ), path( - "workspaces//projects//issue-labels//", + "workspaces//projects//labels//", LabelAPIEndpoint.as_view(), name="label", ), path( - "workspaces//projects//issues//issue-links/", + "workspaces//projects//issues//links/", IssueLinkAPIEndpoint.as_view(), name="link", ), path( - "workspaces//projects//issues//issue-links//", + "workspaces//projects//issues//links//", IssueLinkAPIEndpoint.as_view(), name="link", ), @@ -50,12 +50,12 @@ urlpatterns = [ name="comment", ), path( - "workspaces//projects//issues//activites/", + "workspaces//projects//issues//activities/", IssueActivityAPIEndpoint.as_view(), name="activity", ), path( - "workspaces//projects//issues//activites//", + "workspaces//projects//issues//activities//", IssueActivityAPIEndpoint.as_view(), name="activity", ), diff --git a/apiserver/plane/api/urls/module.py b/apiserver/plane/api/urls/module.py index 7860a0fce..7117a9e8b 100644 --- a/apiserver/plane/api/urls/module.py +++ b/apiserver/plane/api/urls/module.py @@ -19,7 +19,7 @@ urlpatterns = [ name="module-issues", ), path( - "workspaces//projects//modules//module-issues//", + "workspaces//projects//modules//module-issues//", ModuleIssueAPIEndpoint.as_view(), name="module-issues", ), diff --git a/apiserver/plane/api/urls/state.py b/apiserver/plane/api/urls/state.py index cf5eefd53..0676ac5ad 100644 --- a/apiserver/plane/api/urls/state.py +++ b/apiserver/plane/api/urls/state.py @@ -8,4 +8,9 @@ urlpatterns = [ StateAPIEndpoint.as_view(), name="states", ), + path( + "workspaces//projects//states//", + StateAPIEndpoint.as_view(), + name="states", + ), ] \ No newline at end of file diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index 52458f317..8345d7824 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -291,7 +291,7 @@ class CycleAPIEndpoint(WebhookMixin, BaseAPIView): } ), actor_id=str(request.user.id), - issue_id=str(pk), + issue_id=None, project_id=str(project_id), current_instance=None, epoch=int(timezone.now().timestamp()), @@ -476,9 +476,9 @@ class CycleIssueAPIEndpoint(WebhookMixin, BaseAPIView): status=status.HTTP_200_OK, ) - def delete(self, request, slug, project_id, cycle_id, pk): + def delete(self, request, slug, project_id, cycle_id, issue_id): cycle_issue = CycleIssue.objects.get( - pk=pk, workspace__slug=slug, project_id=project_id, cycle_id=cycle_id + issue_id=issue_id, workspace__slug=slug, project_id=project_id, cycle_id=cycle_id ) issue_id = cycle_issue.issue_id cycle_issue.delete() @@ -491,7 +491,7 @@ class CycleIssueAPIEndpoint(WebhookMixin, BaseAPIView): } ), actor_id=str(self.request.user.id), - issue_id=str(self.kwargs.get("pk", None)), + issue_id=str(issue_id), project_id=str(self.kwargs.get("project_id", None)), current_instance=None, epoch=int(timezone.now().timestamp()), diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index f2ffb7f0d..3b43c7ab8 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -352,11 +352,11 @@ class IssueLinkAPIEndpoint(BaseAPIView): .distinct() ) - def get(self, request, slug, project_id, pk=None): + def get(self, request, slug, project_id, issue_id, pk=None): if pk is None: - labels = self.get_queryset() + issue_links = self.get_queryset() serializer = IssueLinkSerializer( - labels, + issue_links, fields=self.fields, expand=self.expand, ) @@ -370,14 +370,13 @@ class IssueLinkAPIEndpoint(BaseAPIView): expand=self.expand, ).data, ) - else: - label = self.get_queryset().get(pk=pk) - serializer = IssueLinkSerializer( - label, - fields=self.fields, - expand=self.expand, - ) - return Response(serializer.data, status=status.HTTP_200_OK) + issue_link = self.get_queryset().get(pk=pk) + serializer = IssueLinkSerializer( + issue_link, + fields=self.fields, + expand=self.expand, + ) + return Response(serializer.data, status=status.HTTP_200_OK) def post(self, request, slug, project_id, issue_id): serializer = IssueLinkSerializer(data=request.data) @@ -590,7 +589,7 @@ class IssueActivityAPIEndpoint(BaseAPIView): serializer = IssueActivitySerializer(issue_activities) return Response(serializer.data, status=status.HTTP_200_OK) - self.paginate( + return self.paginate( request=request, queryset=(issue_activities), on_results=lambda issue_activity: IssueActivitySerializer( diff --git a/apiserver/plane/api/views/module.py b/apiserver/plane/api/views/module.py index 78f721adc..51be6be30 100644 --- a/apiserver/plane/api/views/module.py +++ b/apiserver/plane/api/views/module.py @@ -129,6 +129,14 @@ class ModuleAPIEndpoint(WebhookMixin, BaseAPIView): serializer = ModuleSerializer(module) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + def patch(self, request, slug, project_id, pk): + module = Module.objects.get(pk=pk, project_id=project_id, workspace__slug=slug) + serializer = ModuleSerializer(module, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def get(self, request, slug, project_id, pk=None): if pk: @@ -168,7 +176,7 @@ class ModuleAPIEndpoint(WebhookMixin, BaseAPIView): } ), actor_id=str(request.user.id), - issue_id=str(pk), + issue_id=None, project_id=str(project_id), current_instance=None, epoch=int(timezone.now().timestamp()), @@ -323,7 +331,7 @@ class ModuleIssueAPIEndpoint(WebhookMixin, BaseAPIView): # Capture Issue Activity issue_activity.delay( type="module.activity.created", - requested_data=json.dumps({"modules_list": issues}), + requested_data=json.dumps({"modules_list": str(issues)}), actor_id=str(self.request.user.id), issue_id=None, project_id=str(self.kwargs.get("project_id", None)), @@ -343,9 +351,9 @@ class ModuleIssueAPIEndpoint(WebhookMixin, BaseAPIView): status=status.HTTP_200_OK, ) - def delete(self, request, slug, project_id, module_id, pk): + def delete(self, request, slug, project_id, module_id, issue_id): module_issue = ModuleIssue.objects.get( - workspace__slug=slug, project_id=project_id, module_id=module_id, pk=pk + workspace__slug=slug, project_id=project_id, module_id=module_id, issue_id=issue_id ) module_issue.delete() issue_activity.delay( @@ -357,7 +365,7 @@ class ModuleIssueAPIEndpoint(WebhookMixin, BaseAPIView): } ), actor_id=str(request.user.id), - issue_id=str(pk), + issue_id=str(issue_id), project_id=str(project_id), current_instance=None, epoch=int(timezone.now().timestamp()), diff --git a/apiserver/plane/api/views/state.py b/apiserver/plane/api/views/state.py index 8e7a73d9b..99b3a943f 100644 --- a/apiserver/plane/api/views/state.py +++ b/apiserver/plane/api/views/state.py @@ -23,10 +23,8 @@ class StateAPIEndpoint(BaseAPIView): ] def get_queryset(self): - return self.filter_queryset( - super() - .get_queryset() - .filter(workspace__slug=self.kwargs.get("slug")) + return ( + State.objects.filter(workspace__slug=self.kwargs.get("slug")) .filter(project_id=self.kwargs.get("project_id")) .filter(project__project_projectmember__member=self.request.user) .filter(~Q(name="Triage")) @@ -42,9 +40,9 @@ class StateAPIEndpoint(BaseAPIView): return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - def get(self, request, slug, project_id, pk=None): - if pk: - serializer = StateSerializer(self.get_queryset().get(pk=pk)) + def get(self, request, slug, project_id, state_id=None): + if state_id: + serializer = StateSerializer(self.get_queryset().get(pk=state_id)) return Response(serializer.data, status=status.HTTP_200_OK) return self.paginate( request=request, @@ -57,10 +55,10 @@ class StateAPIEndpoint(BaseAPIView): ).data, ) - def delete(self, request, slug, project_id, pk): + def delete(self, request, slug, project_id, state_id): state = State.objects.get( ~Q(name="Triage"), - pk=pk, + pk=state_id, project_id=project_id, workspace__slug=slug, ) @@ -69,7 +67,7 @@ class StateAPIEndpoint(BaseAPIView): return Response({"error": "Default state cannot be deleted"}, status=False) # Check for any issues in the state - issue_exist = Issue.issue_objects.filter(state=pk).exists() + issue_exist = Issue.issue_objects.filter(state=state_id).exists() if issue_exist: return Response( @@ -80,8 +78,8 @@ class StateAPIEndpoint(BaseAPIView): state.delete() return Response(status=status.HTTP_204_NO_CONTENT) - def patch(self, request, slug, project_id, pk=None): - state = State.objects.filter(workspace__slug=slug, project_id=project_id, pk=pk) + def patch(self, request, slug, project_id, state_id=None): + state = State.objects.filter(workspace__slug=slug, project_id=project_id, pk=state_id) serializer = StateSerializer(state, data=request.data, partial=True) if serializer.is_valid(): serializer.save() diff --git a/apiserver/plane/app/views/base.py b/apiserver/plane/app/views/base.py index de7bafd57..7e3aa82b9 100644 --- a/apiserver/plane/app/views/base.py +++ b/apiserver/plane/app/views/base.py @@ -46,19 +46,22 @@ class WebhookMixin: def finalize_response(self, request, response, *args, **kwargs): response = super().finalize_response(request, response, *args, **kwargs) - if ( self.webhook_event - and self.request.method in ["POST", "PATCH", "DELETE"] + and self.request.method in ["POST", "PATCH"] and response.status_code in [200, 201, 204] ): + # Get the id + object_id = ( + response.data.get("id") if isinstance(response.data, dict) else None + ) + send_webhook.delay( event=self.webhook_event, - event_data=json.dumps(response.data, cls=DjangoJSONEncoder), + event_id=object_id, action=self.request.method, slug=self.workspace_slug, ) - return response diff --git a/apiserver/plane/app/views/cycle.py b/apiserver/plane/app/views/cycle.py index 7228aa088..5a2cf6807 100644 --- a/apiserver/plane/app/views/cycle.py +++ b/apiserver/plane/app/views/cycle.py @@ -688,7 +688,6 @@ class CycleIssueViewSet(WebhookMixin, BaseViewSet): pk=pk, workspace__slug=slug, project_id=project_id, cycle_id=cycle_id ) issue_id = cycle_issue.issue_id - cycle_issue.delete() issue_activity.delay( type="cycle.activity.deleted", requested_data=json.dumps( @@ -698,11 +697,12 @@ class CycleIssueViewSet(WebhookMixin, BaseViewSet): } ), actor_id=str(self.request.user.id), - issue_id=str(self.kwargs.get("pk", None)), + issue_id=str(cycle_issue.issue_id), project_id=str(self.kwargs.get("project_id", None)), current_instance=None, epoch=int(timezone.now().timestamp()), ) + cycle_issue.delete() return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/apiserver/plane/app/views/module.py b/apiserver/plane/app/views/module.py index 28986ea0f..4e10a0629 100644 --- a/apiserver/plane/app/views/module.py +++ b/apiserver/plane/app/views/module.py @@ -461,7 +461,6 @@ class ModuleIssueViewSet(BaseViewSet): module_issue = ModuleIssue.objects.get( workspace__slug=slug, project_id=project_id, module_id=module_id, pk=pk ) - module_issue.delete() issue_activity.delay( type="module.activity.deleted", requested_data=json.dumps( @@ -471,11 +470,12 @@ class ModuleIssueViewSet(BaseViewSet): } ), actor_id=str(request.user.id), - issue_id=str(pk), + issue_id=str(module_issue.issue_id), project_id=str(project_id), current_instance=None, epoch=int(timezone.now().timestamp()), ) + module_issue.delete() return Response(status=status.HTTP_204_NO_CONTENT)