From b3190e5f8cb065d51bd51d3a3f4912fc176ebd55 Mon Sep 17 00:00:00 2001 From: Mardone Date: Tue, 19 Dec 2023 12:04:46 -0300 Subject: [PATCH] Fix: Distinct issues on postgres --- connect/api/v2/paginations.py | 7 +++++++ connect/api/v2/projects/serializers.py | 6 +++++- connect/api/v2/projects/views.py | 13 ++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/connect/api/v2/paginations.py b/connect/api/v2/paginations.py index 2a6f4c25..5d214a64 100644 --- a/connect/api/v2/paginations.py +++ b/connect/api/v2/paginations.py @@ -8,3 +8,10 @@ class CustomCursorPagination(CursorPagination): def paginate_queryset(self, queryset, request, view=None): self.ordering = view.get_ordering() return super().paginate_queryset(queryset, request, view) + + +class OpenedProjectCustomCursorPagination(CursorPagination): + def paginate_queryset(self, queryset, request, view=None): + self.ordering = view.get_ordering() + self.queryset = queryset.order_by(*self.ordering).distinct('project') + return super().paginate_queryset(queryset, request, view) diff --git a/connect/api/v2/projects/serializers.py b/connect/api/v2/projects/serializers.py index 6eb96056..79c62da9 100644 --- a/connect/api/v2/projects/serializers.py +++ b/connect/api/v2/projects/serializers.py @@ -641,5 +641,9 @@ class Meta: project = serializers.SerializerMethodField() def get_project(self, obj): - data = ProjectSerializer(obj.project).data + data = ProjectSerializer( + obj.project, + context=self.context + ).data + return data diff --git a/connect/api/v2/projects/views.py b/connect/api/v2/projects/views.py index b4570b7c..5a2b595a 100644 --- a/connect/api/v2/projects/views.py +++ b/connect/api/v2/projects/views.py @@ -19,7 +19,7 @@ ) from django.utils import timezone -from connect.api.v2.paginations import CustomCursorPagination +from connect.api.v2.paginations import CustomCursorPagination, OpenedProjectCustomCursorPagination class ProjectViewSet( @@ -130,7 +130,7 @@ class OpenedProjectViewSet( serializer_class = OpenedProjectSerializer permission_classes = [IsAuthenticated, ProjectHasPermission, Has2FA] lookup_field = "uuid" - pagination_class = CustomCursorPagination + pagination_class = OpenedProjectCustomCursorPagination def get_queryset(self, **kwargs): if getattr(self, "swagger_fake_view", False): @@ -138,15 +138,14 @@ def get_queryset(self, **kwargs): organization__uuid = self.kwargs["organization_uuid"] projects = Project.objects.filter(organization__uuid=organization__uuid) - opened_projects = super().get_queryset().filter(project__in=projects).order_by('project__uuid', 'day') - filtered_projects = opened_projects.distinct('project__uuid', 'day') - return filtered_projects + opened_projects = super().get_queryset().filter(project__in=projects) + return opened_projects def get_ordering(self): valid_fields = ( opened_project.name for opened_project in OpenedProject._meta.get_fields() ) - ordering = [] + ordering = ["project"] for param in self.request.query_params.getlist('ordering'): if param.startswith('-'): field = param[1:] @@ -154,4 +153,4 @@ def get_ordering(self): field = param if field in valid_fields: ordering.append(param) - return ordering or ["day"] + return ordering or ["project", "day"]