fix: model ordering (#521)

* fix: model ordering

* fix: my pages and create by others pages
This commit is contained in:
pablohashescobar 2023-03-25 11:14:05 +05:30 committed by GitHub
parent c6ba93da72
commit 5152deb2d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 10 deletions

View File

@ -26,9 +26,6 @@ from plane.db.models import (
CycleIssue, CycleIssue,
Issue, Issue,
CycleFavorite, CycleFavorite,
IssueBlocker,
IssueLink,
ModuleIssue,
) )
from plane.bgtasks.issue_activites_task import issue_activity from plane.bgtasks.issue_activites_task import issue_activity
from plane.utils.grouper import group_results from plane.utils.grouper import group_results
@ -95,6 +92,7 @@ class CycleViewSet(BaseViewSet):
filter=Q(issue_cycle__issue__state__group="backlog"), filter=Q(issue_cycle__issue__state__group="backlog"),
) )
) )
.order_by("name", "-is_favorite")
.distinct() .distinct()
) )
@ -409,6 +407,7 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView):
filter=Q(issue_cycle__issue__state__group="backlog"), filter=Q(issue_cycle__issue__state__group="backlog"),
) )
) )
.order_by("name", "-is_favorite")
) )
upcoming_cycle = ( upcoming_cycle = (
@ -452,6 +451,7 @@ class CurrentUpcomingCyclesEndpoint(BaseAPIView):
filter=Q(issue_cycle__issue__state__group="backlog"), filter=Q(issue_cycle__issue__state__group="backlog"),
) )
) )
.order_by("name", "-is_favorite")
) )
return Response( return Response(
@ -524,6 +524,7 @@ class CompletedCyclesEndpoint(BaseAPIView):
filter=Q(issue_cycle__issue__state__group="backlog"), filter=Q(issue_cycle__issue__state__group="backlog"),
) )
) )
.order_by("name", "-is_favorite")
) )
return Response( return Response(
@ -550,6 +551,12 @@ class DraftCyclesEndpoint(BaseAPIView):
def get(self, request, slug, project_id): def get(self, request, slug, project_id):
try: try:
subquery = CycleFavorite.objects.filter(
user=self.request.user,
cycle_id=OuterRef("pk"),
project_id=project_id,
workspace__slug=slug,
)
draft_cycles = ( draft_cycles = (
Cycle.objects.filter( Cycle.objects.filter(
workspace__slug=slug, workspace__slug=slug,
@ -560,6 +567,7 @@ class DraftCyclesEndpoint(BaseAPIView):
.select_related("project") .select_related("project")
.select_related("workspace") .select_related("workspace")
.select_related("owned_by") .select_related("owned_by")
.annotate(is_favorite=Exists(subquery))
.annotate(total_issues=Count("issue_cycle")) .annotate(total_issues=Count("issue_cycle"))
.annotate( .annotate(
completed_issues=Count( completed_issues=Count(
@ -591,6 +599,7 @@ class DraftCyclesEndpoint(BaseAPIView):
filter=Q(issue_cycle__issue__state__group="backlog"), filter=Q(issue_cycle__issue__state__group="backlog"),
) )
) )
.order_by("name", "-is_favorite")
) )
return Response( return Response(

View File

@ -213,7 +213,9 @@ class ImportServiceEndpoint(BaseAPIView):
def get(self, request, slug): def get(self, request, slug):
try: try:
imports = Importer.objects.filter(workspace__slug=slug) imports = Importer.objects.filter(workspace__slug=slug).order_by(
"-created_at"
)
serializer = ImporterSerializer(imports, many=True) serializer = ImporterSerializer(imports, many=True)
return Response(serializer.data) return Response(serializer.data)
except Exception as e: except Exception as e:

View File

@ -293,6 +293,7 @@ class UserWorkSpaceIssues(BaseAPIView):
).select_related("created_by"), ).select_related("created_by"),
) )
) )
.order_by("-created_at")
) )
serializer = IssueSerializer(issues, many=True) serializer = IssueSerializer(issues, many=True)
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)
@ -311,8 +312,10 @@ class WorkSpaceIssuesEndpoint(BaseAPIView):
def get(self, request, slug): def get(self, request, slug):
try: try:
issues = Issue.objects.filter(workspace__slug=slug).filter( issues = (
project__project_projectmember__member=self.request.user Issue.objects.filter(workspace__slug=slug)
.filter(project__project_projectmember__member=self.request.user)
.order_by("-created_at")
) )
serializer = IssueSerializer(issues, many=True) serializer = IssueSerializer(issues, many=True)
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)
@ -338,7 +341,7 @@ class IssueActivityEndpoint(BaseAPIView):
project__project_projectmember__member=self.request.user, project__project_projectmember__member=self.request.user,
) )
.select_related("actor") .select_related("actor")
).order_by("created_by") ).order_by("created_at")
issue_comments = ( issue_comments = (
IssueComment.objects.filter(issue_id=issue_id) IssueComment.objects.filter(issue_id=issue_id)
.filter(project__project_projectmember__member=self.request.user) .filter(project__project_projectmember__member=self.request.user)
@ -565,6 +568,7 @@ class LabelViewSet(BaseViewSet):
.select_related("project") .select_related("project")
.select_related("workspace") .select_related("workspace")
.select_related("parent") .select_related("parent")
.order_by("name")
.distinct() .distinct()
) )
@ -719,6 +723,7 @@ class IssueLinkViewSet(BaseViewSet):
.filter(project_id=self.kwargs.get("project_id")) .filter(project_id=self.kwargs.get("project_id"))
.filter(issue_id=self.kwargs.get("issue_id")) .filter(issue_id=self.kwargs.get("issue_id"))
.filter(project__project_projectmember__member=self.request.user) .filter(project__project_projectmember__member=self.request.user)
.order_by("-created_at")
.distinct() .distinct()
) )

View File

@ -102,6 +102,7 @@ class ModuleViewSet(BaseViewSet):
filter=Q(issue_module__issue__state__group="backlog"), filter=Q(issue_module__issue__state__group="backlog"),
) )
) )
.order_by("name", "-is_favorite")
) )
def create(self, request, slug, project_id): def create(self, request, slug, project_id):
@ -337,6 +338,7 @@ class ModuleLinkViewSet(BaseViewSet):
.filter(project_id=self.kwargs.get("project_id")) .filter(project_id=self.kwargs.get("project_id"))
.filter(module_id=self.kwargs.get("module_id")) .filter(module_id=self.kwargs.get("module_id"))
.filter(project__project_projectmember__member=self.request.user) .filter(project__project_projectmember__member=self.request.user)
.order_by("-created_at")
.distinct() .distinct()
) )

View File

@ -60,6 +60,8 @@ class PageViewSet(BaseViewSet):
.annotate(is_favorite=Exists(subquery)) .annotate(is_favorite=Exists(subquery))
.order_by(self.request.GET.get("order_by", "-created_at")) .order_by(self.request.GET.get("order_by", "-created_at"))
.prefetch_related("labels") .prefetch_related("labels")
.order_by("name", "-is_favorite")
.prefetch_related( .prefetch_related(
Prefetch( Prefetch(
"blocks", queryset=PageBlock.objects.select_related("page", "issue") "blocks", queryset=PageBlock.objects.select_related("page", "issue")
@ -112,6 +114,7 @@ class PageBlockViewSet(BaseViewSet):
.select_related("workspace") .select_related("workspace")
.select_related("page") .select_related("page")
.select_related("issue") .select_related("issue")
.order_by("sort_order")
.distinct() .distinct()
) )
@ -258,6 +261,7 @@ class RecentPagesEndpoint(BaseAPIView):
.select_related("workspace") .select_related("workspace")
.select_related("owned_by") .select_related("owned_by")
.prefetch_related("labels") .prefetch_related("labels")
.order_by("-updated_by")
) )
earlier_this_week = ( earlier_this_week = (
@ -323,6 +327,7 @@ class FavoritePagesEndpoint(BaseAPIView):
.select_related("workspace") .select_related("workspace")
.select_related("owned_by") .select_related("owned_by")
.prefetch_related("labels") .prefetch_related("labels")
.order_by("name", "-is_favorite")
) )
serializer = PageSerializer(pages, many=True) serializer = PageSerializer(pages, many=True)
@ -342,6 +347,12 @@ class MyPagesEndpoint(BaseAPIView):
def get(self, request, slug, project_id): def get(self, request, slug, project_id):
try: try:
subquery = PageFavorite.objects.filter(
user=request.user,
page_id=OuterRef("pk"),
project_id=project_id,
workspace__slug=slug,
)
pages = ( pages = (
Page.objects.filter( Page.objects.filter(
workspace__slug=slug, project_id=project_id, owned_by=request.user workspace__slug=slug, project_id=project_id, owned_by=request.user
@ -350,6 +361,8 @@ class MyPagesEndpoint(BaseAPIView):
.select_related("workspace") .select_related("workspace")
.select_related("owned_by") .select_related("owned_by")
.prefetch_related("labels") .prefetch_related("labels")
.annotate(is_favorite=Exists(subquery))
.order_by("name", "-is_favorite")
) )
serializer = PageSerializer(pages, many=True) serializer = PageSerializer(pages, many=True)
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)
@ -368,6 +381,12 @@ class CreatedbyOtherPagesEndpoint(BaseAPIView):
def get(self, request, slug, project_id): def get(self, request, slug, project_id):
try: try:
subquery = PageFavorite.objects.filter(
user=request.user,
page_id=OuterRef("pk"),
project_id=project_id,
workspace__slug=slug,
)
pages = ( pages = (
Page.objects.filter( Page.objects.filter(
~Q(owned_by=request.user), ~Q(owned_by=request.user),
@ -378,6 +397,8 @@ class CreatedbyOtherPagesEndpoint(BaseAPIView):
.select_related("workspace") .select_related("workspace")
.select_related("owned_by") .select_related("owned_by")
.prefetch_related("labels") .prefetch_related("labels")
.annotate(is_favorite=Exists(subquery))
.order_by("name", "-is_favorite")
) )
serializer = PageSerializer(pages, many=True) serializer = PageSerializer(pages, many=True)
return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.data, status=status.HTTP_200_OK)

View File

@ -91,7 +91,7 @@ class ProjectViewSet(BaseViewSet):
projects = ( projects = (
self.get_queryset() self.get_queryset()
.annotate(is_favorite=Exists(subquery)) .annotate(is_favorite=Exists(subquery))
.order_by("-is_favorite") .order_by("name", "-is_favorite")
) )
return Response(ProjectDetailSerializer(projects, many=True).data) return Response(ProjectDetailSerializer(projects, many=True).data)
except Exception as e: except Exception as e:
@ -101,7 +101,6 @@ class ProjectViewSet(BaseViewSet):
status=status.HTTP_400_BAD_REQUEST, status=status.HTTP_400_BAD_REQUEST,
) )
def create(self, request, slug): def create(self, request, slug):
try: try:
workspace = Workspace.objects.get(slug=slug) workspace = Workspace.objects.get(slug=slug)

View File

@ -53,6 +53,7 @@ class IssueViewViewSet(BaseViewSet):
.select_related("project") .select_related("project")
.select_related("workspace") .select_related("workspace")
.annotate(is_favorite=Exists(subquery)) .annotate(is_favorite=Exists(subquery))
.order_by("name", "-is_favorite")
.distinct() .distinct()
) )

View File

@ -70,7 +70,9 @@ class WorkSpaceViewSet(BaseViewSet):
lookup_field = "slug" lookup_field = "slug"
def get_queryset(self): 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): def create(self, request):
try: try: