Skip to content

Commit

Permalink
Fix: Distinct issues on postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
zMardone committed Dec 19, 2023
1 parent 7812ce6 commit b3190e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions connect/api/v2/paginations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 5 additions & 1 deletion connect/api/v2/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 6 additions & 7 deletions connect/api/v2/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -130,28 +130,27 @@ 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):
return OpenedProject.objects.none() # pragma: no cover

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:]
else:
field = param
if field in valid_fields:
ordering.append(param)
return ordering or ["day"]
return ordering or ["project", "day"]

0 comments on commit b3190e5

Please sign in to comment.