dev: pagination for spreadhseet and gantt

This commit is contained in:
pablohashescobar 2024-02-26 14:26:37 +05:30
parent 0076b09131
commit 2efa21e8f4
5 changed files with 142 additions and 75 deletions

View File

@ -708,7 +708,7 @@ class CycleIssueViewSet(WebhookMixin, BaseViewSet):
] ]
order_by = request.GET.get("order_by", "created_at") order_by = request.GET.get("order_by", "created_at")
filters = issue_filters(request.query_params, "GET") filters = issue_filters(request.query_params, "GET")
queryset = ( issue_queryset = (
Issue.issue_objects.filter(issue_cycle__cycle_id=cycle_id) Issue.issue_objects.filter(issue_cycle__cycle_id=cycle_id)
.filter(project_id=project_id) .filter(project_id=project_id)
.filter(workspace__slug=slug) .filter(workspace__slug=slug)
@ -773,12 +773,12 @@ class CycleIssueViewSet(WebhookMixin, BaseViewSet):
) )
.order_by(order_by) .order_by(order_by)
) )
if self.fields: def on_results(issues):
issues = IssueSerializer( if self.expand or self.fields:
queryset, many=True, fields=fields if fields else None return IssueSerializer(
).data issues, many=True, expand=self.expand, fields=self.fields
else: ).data
issues = queryset.values( return issues.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -805,7 +805,18 @@ class CycleIssueViewSet(WebhookMixin, BaseViewSet):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
def create(self, request, slug, project_id, cycle_id): def create(self, request, slug, project_id, cycle_id):
issues = request.data.get("issues", []) issues = request.data.get("issues", [])

View File

@ -164,15 +164,12 @@ class IssueListEndpoint(BaseAPIView):
order_by_param=order_by_param, order_by_param=order_by_param,
) )
if self.fields or self.expand: def on_results(issues):
issues = IssueSerializer( if self.expand or self.fields:
issue_queryset, return IssueSerializer(
many=True, issues, many=True, expand=self.expand, fields=self.fields
fields=self.fields, ).data
expand=self.expand, return issues.values(
).data
else:
issues = issue_queryset.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -199,7 +196,17 @@ class IssueListEndpoint(BaseAPIView):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
class IssueViewSet(WebhookMixin, BaseViewSet): class IssueViewSet(WebhookMixin, BaseViewSet):
@ -299,15 +306,12 @@ class IssueViewSet(WebhookMixin, BaseViewSet):
order_by_param=order_by_param, order_by_param=order_by_param,
) )
if self.fields or self.expand: def on_results(issues):
issues = IssueSerializer( if self.expand or self.fields:
issue_queryset, return IssueSerializer(
many=True, issues, many=True, expand=self.expand, fields=self.fields
fields=self.fields, ).data
expand=self.expand, return issues.values(
).data
else:
issues = issue_queryset.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -334,7 +338,18 @@ class IssueViewSet(WebhookMixin, BaseViewSet):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
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)
@ -1296,14 +1311,13 @@ class IssueArchiveViewSet(BaseViewSet):
if show_sub_issues == "true" if show_sub_issues == "true"
else issue_queryset.filter(parent__isnull=True) else issue_queryset.filter(parent__isnull=True)
) )
if self.expand or self.fields:
issues = IssueSerializer( def on_results(issues):
issue_queryset, if self.expand or self.fields:
many=True, return IssueSerializer(
fields=self.fields, issues, many=True, expand=self.expand, fields=self.fields
).data ).data
else: return issues.values(
issues = issue_queryset.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -1330,7 +1344,18 @@ class IssueArchiveViewSet(BaseViewSet):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
def retrieve(self, request, slug, project_id, pk=None): def retrieve(self, request, slug, project_id, pk=None):
issue = ( issue = (
@ -1900,16 +1925,12 @@ class IssueDraftViewSet(BaseViewSet):
issue_queryset = order_issue_queryset( issue_queryset = order_issue_queryset(
issue_queryset=issue_queryset, order_by_param=order_by_param issue_queryset=issue_queryset, order_by_param=order_by_param
) )
# Only use serializer when expand else return by values def on_results(issues):
if self.expand or self.fields: if self.expand or self.fields:
issues = IssueSerializer( return IssueSerializer(
issue_queryset, issues, many=True, expand=self.expand, fields=self.fields
many=True, ).data
fields=self.fields, return issues.values(
expand=self.expand,
).data
else:
issues = issue_queryset.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -1936,7 +1957,17 @@ class IssueDraftViewSet(BaseViewSet):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
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

@ -510,12 +510,12 @@ class ModuleIssueViewSet(WebhookMixin, BaseViewSet):
] ]
filters = issue_filters(request.query_params, "GET") filters = issue_filters(request.query_params, "GET")
issue_queryset = self.get_queryset().filter(**filters) issue_queryset = self.get_queryset().filter(**filters)
if self.fields or self.expand: def on_results(issues):
issues = IssueSerializer( if self.expand or self.fields:
issue_queryset, many=True, fields=fields if fields else None return IssueSerializer(
).data issues, many=True, expand=self.expand, fields=self.fields
else: ).data
issues = issue_queryset.values( return issues.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -542,7 +542,17 @@ class ModuleIssueViewSet(WebhookMixin, BaseViewSet):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
# create multiple issues inside a module # create multiple issues inside a module
def create_module_issues(self, request, slug, project_id, module_id): def create_module_issues(self, request, slug, project_id, module_id):

View File

@ -155,12 +155,12 @@ class GlobalViewIssuesViewSet(BaseViewSet):
issue_queryset=issue_queryset, order_by_param=order_by_param issue_queryset=issue_queryset, order_by_param=order_by_param
) )
if self.fields: def on_results(issues):
issues = IssueSerializer( if self.expand or self.fields:
issue_queryset, many=True, fields=self.fields return IssueSerializer(
).data issues, many=True, expand=self.expand, fields=self.fields
else: ).data
issues = issue_queryset.values( return issues.values(
"id", "id",
"name", "name",
"state_id", "state_id",
@ -187,7 +187,17 @@ class GlobalViewIssuesViewSet(BaseViewSet):
"is_draft", "is_draft",
"archived_at", "archived_at",
) )
return Response(issues, status=status.HTTP_200_OK)
if request.GET.get("layout", "spreadsheet") in [
"layout",
"spreadsheet",
]:
return self.paginate(
request=request,
queryset=issue_queryset,
on_results=on_results
)
return on_results(issues=issue_queryset)
class IssueViewViewSet(BaseViewSet): class IssueViewViewSet(BaseViewSet):

View File

@ -116,14 +116,15 @@ class OffsetPaginator:
if offset < 0: if offset < 0:
raise BadPaginationError("Pagination offset cannot be negative") raise BadPaginationError("Pagination offset cannot be negative")
results = list(queryset[offset:stop]) results = queryset[offset:stop]
if cursor.value != limit: if cursor.value != limit:
results = results[-(limit + 1) :] results = results[-(limit + 1) :]
next_cursor = Cursor(limit, page + 1, False, len(results) > limit) next_cursor = Cursor(limit, page + 1, False, results.count() > limit)
prev_cursor = Cursor(limit, page - 1, True, page > 0) prev_cursor = Cursor(limit, page - 1, True, page > 0)
results = list(results[:limit]) results = results[:limit]
if self.on_results: if self.on_results:
results = self.on_results(results) results = self.on_results(results)
@ -162,11 +163,16 @@ class BasePaginator:
def get_layout(self, request): def get_layout(self, request):
layout = request.GET.get("layout", "list") layout = request.GET.get("layout", "list")
if layout not in ["list", "kanban", "spreadsheet", "calendar", "gantt"]: if layout not in [
"list",
"kanban",
"spreadsheet",
"calendar",
"gantt",
]:
raise ValidationError(detail="Invalid layout given") raise ValidationError(detail="Invalid layout given")
return layout return layout
def paginate( def paginate(
self, self,
request, request,
@ -186,13 +192,12 @@ class BasePaginator:
# Convert the cursor value to integer and float from string # Convert the cursor value to integer and float from string
input_cursor = None input_cursor = None
if request.GET.get(self.cursor_name): try:
try: input_cursor = cursor_cls.from_string(
input_cursor = cursor_cls.from_string( request.GET.get(self.cursor_name, f"{per_page}:0:0"),
request.GET.get(self.cursor_name) )
) except ValueError:
except ValueError: raise ParseError(detail="Invalid cursor parameter.")
raise ParseError(detail="Invalid cursor parameter.")
if not paginator: if not paginator:
paginator = paginator_cls(**paginator_kwargs) paginator = paginator_cls(**paginator_kwargs)