From c07cfee0181c535f880f238ca49015e741ec3179 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Wed, 15 Mar 2023 23:21:23 +0530 Subject: [PATCH] refactor: update favorites for project, cycle and module (#434) * dev: refactor favorites * dev: fix typo --- apiserver/plane/api/views/cycle.py | 24 +++++++----------------- apiserver/plane/api/views/module.py | 25 +++++++------------------ apiserver/plane/api/views/project.py | 22 ++++++---------------- 3 files changed, 20 insertions(+), 51 deletions(-) diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index ee70bb477..5f7b46771 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -38,6 +38,12 @@ class CycleViewSet(BaseViewSet): ) def get_queryset(self): + subquery = CycleFavorite.objects.filter( + user=self.request.user, + cycle_id=OuterRef("pk"), + project_id=self.kwargs.get("project_id"), + workspace__slug=self.kwargs.get("slug"), + ) return self.filter_queryset( super() .get_queryset() @@ -47,26 +53,10 @@ class CycleViewSet(BaseViewSet): .select_related("project") .select_related("workspace") .select_related("owned_by") + .annotate(is_favorite=Exists(subquery)) .distinct() ) - def list(self, request, slug, project_id): - try: - subquery = CycleFavorite.objects.filter( - user=self.request.user, - cycle_id=OuterRef("pk"), - project_id=project_id, - workspace__slug=slug, - ) - cycles = self.get_queryset().annotate(is_favorite=Exists(subquery)) - return Response(CycleSerializer(cycles, many=True).data) - except Exception as e: - capture_exception(e) - return Response( - {"error": "Something went wrong please try again later"}, - status=status.HTTP_400_BAD_REQUEST, - ) - def create(self, request, slug, project_id): try: if ( diff --git a/apiserver/plane/api/views/module.py b/apiserver/plane/api/views/module.py index 3633859d5..da7593ea6 100644 --- a/apiserver/plane/api/views/module.py +++ b/apiserver/plane/api/views/module.py @@ -47,11 +47,18 @@ class ModuleViewSet(BaseViewSet): ) def get_queryset(self): + subquery = ModuleFavorite.objects.filter( + user=self.request.user, + module_id=OuterRef("pk"), + project_id=self.kwargs.get("project_id"), + workspace__slug=self.kwargs.get("slug"), + ) return ( super() .get_queryset() .filter(project_id=self.kwargs.get("project_id")) .filter(workspace__slug=self.kwargs.get("slug")) + .annotate(is_favorite=Exists(subquery)) .select_related("project") .select_related("workspace") .select_related("lead") @@ -101,23 +108,6 @@ class ModuleViewSet(BaseViewSet): status=status.HTTP_400_BAD_REQUEST, ) - def list(self, request, slug, project_id): - try: - subquery = ModuleFavorite.objects.filter( - user=self.request.user, - module_id=OuterRef("pk"), - project_id=project_id, - workspace__slug=slug, - ) - modules = self.get_queryset().annotate(is_favorite=Exists(subquery)) - return Response(ModuleSerializer(modules, many=True).data) - except Exception as e: - capture_exception(e) - return Response( - {"error": "Something went wrong please try again later"}, - status=status.HTTP_400_BAD_REQUEST, - ) - class ModuleIssueViewSet(BaseViewSet): serializer_class = ModuleIssueSerializer @@ -307,7 +297,6 @@ class ModuleLinkViewSet(BaseViewSet): class ModuleFavoriteViewSet(BaseViewSet): - permission_classes = [ ProjectEntityPermission, ] diff --git a/apiserver/plane/api/views/project.py b/apiserver/plane/api/views/project.py index 0e51dd156..730875e0c 100644 --- a/apiserver/plane/api/views/project.py +++ b/apiserver/plane/api/views/project.py @@ -64,6 +64,11 @@ class ProjectViewSet(BaseViewSet): return ProjectDetailSerializer def get_queryset(self): + subquery = ProjectFavorite.objects.filter( + user=self.request.user, + project_id=OuterRef("pk"), + workspace__slug=self.kwargs.get("slug"), + ) return self.filter_queryset( super() .get_queryset() @@ -72,25 +77,10 @@ class ProjectViewSet(BaseViewSet): .select_related( "workspace", "workspace__owner", "default_assignee", "project_lead" ) + .annotate(is_favorite=Exists(subquery)) .distinct() ) - def list(self, request, slug): - try: - subquery = ProjectFavorite.objects.filter( - user=self.request.user, - project_id=OuterRef("pk"), - workspace__slug=self.kwargs.get("slug"), - ) - projects = self.get_queryset().annotate(is_favorite=Exists(subquery)) - return Response(ProjectDetailSerializer(projects, many=True).data) - except Exception as e: - capture_exception(e) - return Response( - {"error": "Something went wrong please try again later"}, - status=status.HTTP_400_BAD_REQUEST, - ) - def create(self, request, slug): try: workspace = Workspace.objects.get(slug=slug)