dev: grouped paginator

This commit is contained in:
pablohashescobar 2024-03-15 12:02:24 +05:30
parent cd302d061e
commit d90845b260
2 changed files with 73 additions and 62 deletions

View File

@ -243,50 +243,24 @@ class IssueViewSet(WebhookMixin, BaseViewSet):
sub_group_by=sub_group_by, sub_group_by=sub_group_by,
) )
# List Paginate if group_by:
if not group_by and not sub_group_by: if sub_group_by:
return self.paginate( if group_by == sub_group_by:
order_by=order_by_param, return Response(
request=request, {
queryset=issue_queryset, "error": "Group by and sub group by cannot have same parameters"
on_results=lambda issues: issue_on_results( },
group_by=group_by, issues=issues, sub_group_by=sub_group_by status=status.HTTP_400_BAD_REQUEST,
),
) )
else:
if group_by and not sub_group_by:
# Group paginate
return self.paginate( return self.paginate(
request=request, request=request,
order_by=order_by_param, order_by=order_by_param,
queryset=issue_queryset, queryset=issue_queryset,
on_results=lambda issues: issue_on_results( on_results=lambda issues: issue_on_results(
group_by=group_by, issues=issues, sub_group_by=sub_group_by group_by=group_by,
), issues=issues,
paginator_cls=GroupedOffsetPaginator, sub_group_by=sub_group_by,
group_by_fields=issue_group_values(
field=group_by,
slug=slug,
project_id=project_id,
filters=filters,
),
group_by_field_name=group_by,
count_filter=Q(
Q(issue_inbox__status=1)
| Q(issue_inbox__status=-1)
| Q(issue_inbox__status=2)
| Q(issue_inbox__isnull=True),
archived_at__isnull=True,
is_draft=False,
),
)
if group_by and sub_group_by:
return self.paginate(
request=request,
order_by=order_by_param,
queryset=issue_queryset,
on_results=lambda issues: issue_on_results(
group_by=group_by, issues=issues, sub_group_by=sub_group_by
), ),
paginator_cls=SubGroupedOffsetPaginator, paginator_cls=SubGroupedOffsetPaginator,
group_by_fields=issue_group_values( group_by_fields=issue_group_values(
@ -312,6 +286,43 @@ class IssueViewSet(WebhookMixin, BaseViewSet):
is_draft=False, is_draft=False,
), ),
) )
else:
# Group paginate
return self.paginate(
request=request,
order_by=order_by_param,
queryset=issue_queryset,
on_results=lambda issues: issue_on_results(
group_by=group_by,
issues=issues,
sub_group_by=sub_group_by,
),
paginator_cls=GroupedOffsetPaginator,
group_by_fields=issue_group_values(
field=group_by,
slug=slug,
project_id=project_id,
filters=filters,
),
group_by_field_name=group_by,
count_filter=Q(
Q(issue_inbox__status=1)
| Q(issue_inbox__status=-1)
| Q(issue_inbox__status=2)
| Q(issue_inbox__isnull=True),
archived_at__isnull=True,
is_draft=False,
),
)
else:
return self.paginate(
order_by=order_by_param,
request=request,
queryset=issue_queryset,
on_results=lambda issues: issue_on_results(
group_by=group_by, issues=issues, sub_group_by=sub_group_by
),
)
def create(self, request, slug, project_id): def create(self, request, slug, project_id):
project = Project.objects.get(pk=project_id) project = Project.objects.get(pk=project_id)

View File

@ -1,7 +1,7 @@
# Django imports # Django imports
from django.contrib.postgres.aggregates import ArrayAgg from django.contrib.postgres.aggregates import ArrayAgg
from django.contrib.postgres.fields import ArrayField from django.contrib.postgres.fields import ArrayField
from django.db.models import F, Q, UUIDField, Value from django.db.models import Q, UUIDField, Value
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
# Module imports # Module imports