From 5152deb2d069626490e43ca61a4c867b5fed5452 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Sat, 25 Mar 2023 11:14:05 +0530 Subject: [PATCH] fix: model ordering (#521) * fix: model ordering * fix: my pages and create by others pages --- apiserver/plane/api/views/cycle.py | 15 ++++++++++++--- apiserver/plane/api/views/importer.py | 4 +++- apiserver/plane/api/views/issue.py | 11 ++++++++--- apiserver/plane/api/views/module.py | 2 ++ apiserver/plane/api/views/page.py | 21 +++++++++++++++++++++ apiserver/plane/api/views/project.py | 3 +-- apiserver/plane/api/views/view.py | 1 + apiserver/plane/api/views/workspace.py | 4 +++- 8 files changed, 51 insertions(+), 10 deletions(-) diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index 59fdc5f0b..6b5cb2e4c 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -26,9 +26,6 @@ from plane.db.models import ( CycleIssue, Issue, CycleFavorite, - IssueBlocker, - IssueLink, - ModuleIssue, ) from plane.bgtasks.issue_activites_task import issue_activity from plane.utils.grouper import group_results @@ -95,6 +92,7 @@ class CycleViewSet(BaseViewSet): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .order_by("name", "-is_favorite") .distinct() ) @@ -409,6 +407,7 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .order_by("name", "-is_favorite") ) upcoming_cycle = ( @@ -452,6 +451,7 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .order_by("name", "-is_favorite") ) return Response( @@ -524,6 +524,7 @@ class CompletedCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .order_by("name", "-is_favorite") ) return Response( @@ -550,6 +551,12 @@ class DraftCyclesEndpoint(BaseAPIView): def get(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, + ) draft_cycles = ( Cycle.objects.filter( workspace__slug=slug, @@ -560,6 +567,7 @@ class DraftCyclesEndpoint(BaseAPIView): .select_related("project") .select_related("workspace") .select_related("owned_by") + .annotate(is_favorite=Exists(subquery)) .annotate(total_issues=Count("issue_cycle")) .annotate( completed_issues=Count( @@ -591,6 +599,7 @@ class DraftCyclesEndpoint(BaseAPIView): filter=Q(issue_cycle__issue__state__group="backlog"), ) ) + .order_by("name", "-is_favorite") ) return Response( diff --git a/apiserver/plane/api/views/importer.py b/apiserver/plane/api/views/importer.py index 7d2bc14b4..dd52d2dd2 100644 --- a/apiserver/plane/api/views/importer.py +++ b/apiserver/plane/api/views/importer.py @@ -213,7 +213,9 @@ class ImportServiceEndpoint(BaseAPIView): def get(self, request, slug): try: - imports = Importer.objects.filter(workspace__slug=slug) + imports = Importer.objects.filter(workspace__slug=slug).order_by( + "-created_at" + ) serializer = ImporterSerializer(imports, many=True) return Response(serializer.data) except Exception as e: diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index b6c265fba..9447fff95 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -293,6 +293,7 @@ class UserWorkSpaceIssues(BaseAPIView): ).select_related("created_by"), ) ) + .order_by("-created_at") ) serializer = IssueSerializer(issues, many=True) return Response(serializer.data, status=status.HTTP_200_OK) @@ -311,8 +312,10 @@ class WorkSpaceIssuesEndpoint(BaseAPIView): def get(self, request, slug): try: - issues = Issue.objects.filter(workspace__slug=slug).filter( - project__project_projectmember__member=self.request.user + issues = ( + Issue.objects.filter(workspace__slug=slug) + .filter(project__project_projectmember__member=self.request.user) + .order_by("-created_at") ) serializer = IssueSerializer(issues, many=True) return Response(serializer.data, status=status.HTTP_200_OK) @@ -338,7 +341,7 @@ class IssueActivityEndpoint(BaseAPIView): project__project_projectmember__member=self.request.user, ) .select_related("actor") - ).order_by("created_by") + ).order_by("created_at") issue_comments = ( IssueComment.objects.filter(issue_id=issue_id) .filter(project__project_projectmember__member=self.request.user) @@ -565,6 +568,7 @@ class LabelViewSet(BaseViewSet): .select_related("project") .select_related("workspace") .select_related("parent") + .order_by("name") .distinct() ) @@ -719,6 +723,7 @@ class IssueLinkViewSet(BaseViewSet): .filter(project_id=self.kwargs.get("project_id")) .filter(issue_id=self.kwargs.get("issue_id")) .filter(project__project_projectmember__member=self.request.user) + .order_by("-created_at") .distinct() ) diff --git a/apiserver/plane/api/views/module.py b/apiserver/plane/api/views/module.py index 6112fcba0..207381072 100644 --- a/apiserver/plane/api/views/module.py +++ b/apiserver/plane/api/views/module.py @@ -102,6 +102,7 @@ class ModuleViewSet(BaseViewSet): filter=Q(issue_module__issue__state__group="backlog"), ) ) + .order_by("name", "-is_favorite") ) def create(self, request, slug, project_id): @@ -337,6 +338,7 @@ class ModuleLinkViewSet(BaseViewSet): .filter(project_id=self.kwargs.get("project_id")) .filter(module_id=self.kwargs.get("module_id")) .filter(project__project_projectmember__member=self.request.user) + .order_by("-created_at") .distinct() ) diff --git a/apiserver/plane/api/views/page.py b/apiserver/plane/api/views/page.py index a903acd8a..868a03996 100644 --- a/apiserver/plane/api/views/page.py +++ b/apiserver/plane/api/views/page.py @@ -60,6 +60,8 @@ class PageViewSet(BaseViewSet): .annotate(is_favorite=Exists(subquery)) .order_by(self.request.GET.get("order_by", "-created_at")) .prefetch_related("labels") + + .order_by("name", "-is_favorite") .prefetch_related( Prefetch( "blocks", queryset=PageBlock.objects.select_related("page", "issue") @@ -112,6 +114,7 @@ class PageBlockViewSet(BaseViewSet): .select_related("workspace") .select_related("page") .select_related("issue") + .order_by("sort_order") .distinct() ) @@ -258,6 +261,7 @@ class RecentPagesEndpoint(BaseAPIView): .select_related("workspace") .select_related("owned_by") .prefetch_related("labels") + .order_by("-updated_by") ) earlier_this_week = ( @@ -323,6 +327,7 @@ class FavoritePagesEndpoint(BaseAPIView): .select_related("workspace") .select_related("owned_by") .prefetch_related("labels") + .order_by("name", "-is_favorite") ) serializer = PageSerializer(pages, many=True) @@ -342,6 +347,12 @@ class MyPagesEndpoint(BaseAPIView): def get(self, request, slug, project_id): try: + subquery = PageFavorite.objects.filter( + user=request.user, + page_id=OuterRef("pk"), + project_id=project_id, + workspace__slug=slug, + ) pages = ( Page.objects.filter( workspace__slug=slug, project_id=project_id, owned_by=request.user @@ -350,6 +361,8 @@ class MyPagesEndpoint(BaseAPIView): .select_related("workspace") .select_related("owned_by") .prefetch_related("labels") + .annotate(is_favorite=Exists(subquery)) + .order_by("name", "-is_favorite") ) serializer = PageSerializer(pages, many=True) return Response(serializer.data, status=status.HTTP_200_OK) @@ -368,6 +381,12 @@ class CreatedbyOtherPagesEndpoint(BaseAPIView): def get(self, request, slug, project_id): try: + subquery = PageFavorite.objects.filter( + user=request.user, + page_id=OuterRef("pk"), + project_id=project_id, + workspace__slug=slug, + ) pages = ( Page.objects.filter( ~Q(owned_by=request.user), @@ -378,6 +397,8 @@ class CreatedbyOtherPagesEndpoint(BaseAPIView): .select_related("workspace") .select_related("owned_by") .prefetch_related("labels") + .annotate(is_favorite=Exists(subquery)) + .order_by("name", "-is_favorite") ) serializer = PageSerializer(pages, many=True) return Response(serializer.data, status=status.HTTP_200_OK) diff --git a/apiserver/plane/api/views/project.py b/apiserver/plane/api/views/project.py index 6a817c12a..f592fd083 100644 --- a/apiserver/plane/api/views/project.py +++ b/apiserver/plane/api/views/project.py @@ -91,7 +91,7 @@ class ProjectViewSet(BaseViewSet): projects = ( self.get_queryset() .annotate(is_favorite=Exists(subquery)) - .order_by("-is_favorite") + .order_by("name", "-is_favorite") ) return Response(ProjectDetailSerializer(projects, many=True).data) except Exception as e: @@ -101,7 +101,6 @@ class ProjectViewSet(BaseViewSet): status=status.HTTP_400_BAD_REQUEST, ) - def create(self, request, slug): try: workspace = Workspace.objects.get(slug=slug) diff --git a/apiserver/plane/api/views/view.py b/apiserver/plane/api/views/view.py index f03866cfa..bcd0be957 100644 --- a/apiserver/plane/api/views/view.py +++ b/apiserver/plane/api/views/view.py @@ -53,6 +53,7 @@ class IssueViewViewSet(BaseViewSet): .select_related("project") .select_related("workspace") .annotate(is_favorite=Exists(subquery)) + .order_by("name", "-is_favorite") .distinct() ) diff --git a/apiserver/plane/api/views/workspace.py b/apiserver/plane/api/views/workspace.py index a1778c5f3..a1c18f995 100644 --- a/apiserver/plane/api/views/workspace.py +++ b/apiserver/plane/api/views/workspace.py @@ -70,7 +70,9 @@ class WorkSpaceViewSet(BaseViewSet): lookup_field = "slug" def get_queryset(self): - return self.filter_queryset(super().get_queryset().select_related("owner")) + return self.filter_queryset( + super().get_queryset().select_related("owner") + ).order_by("name") def create(self, request): try: