Skip to content

Commit

Permalink
Create seperate group pagination class
Browse files Browse the repository at this point in the history
  • Loading branch information
eito-fis committed Jul 1, 2021
1 parent 061d620 commit ae53cf1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 22 additions & 5 deletions mathesar/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ class DefaultLimitOffsetPagination(LimitOffsetPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.count),
('group_count', self.group_count),
('results', data)
]))


class TableLimitOffsetPagination(DefaultLimitOffsetPagination):

def paginate_queryset(self, queryset, request, table_id,
filters=[], order_by=[], group_count_by=[]):
filters=[], order_by=[]):
self.limit = self.get_limit(request)
if self.limit is None:
self.limit = self.default_limit
Expand All @@ -29,6 +28,26 @@ def paginate_queryset(self, queryset, request, table_id,
self.count = table.sa_num_records
self.request = request

return table.get_records(
self.limit, self.offset, filters=filters, order_by=order_by,
)


class TableLimitOffsetGroupPagination(TableLimitOffsetPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.count),
('group_count', self.group_count),
('results', data)
]))

def paginate_queryset(self, queryset, request, table_id,
filters=[], order_by=[], group_count_by=[]):
records = super().paginate_queryset(
queryset, request, table_id, filters=filters, order_by=order_by
)

table = queryset.get(id=table_id)
if group_count_by:
group_count = table.get_group_counts(
group_count_by, self.limit, self.offset,
Expand All @@ -46,6 +65,4 @@ def paginate_queryset(self, queryset, request, table_id,
'results': None,
}

return table.get_records(
self.limit, self.offset, filters=filters, order_by=order_by,
)
return records
6 changes: 4 additions & 2 deletions mathesar/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

from mathesar.database.utils import get_non_default_database_keys
from mathesar.models import Table, Schema, DataFile
from mathesar.pagination import DefaultLimitOffsetPagination, TableLimitOffsetPagination
from mathesar.pagination import (
DefaultLimitOffsetPagination, TableLimitOffsetGroupPagination
)
from mathesar.serializers import (
TableSerializer, SchemaSerializer, RecordSerializer, DataFileSerializer,
)
Expand Down Expand Up @@ -94,7 +96,7 @@ class RecordViewSet(viewsets.ViewSet):
# For sorting parameter formatting, see:
# https://github.com/centerofci/sqlalchemy-filters#sort-format
def list(self, request, table_pk=None):
paginator = TableLimitOffsetPagination()
paginator = TableLimitOffsetGroupPagination()

# Use a form to parse JSON url parameters
filter_form = RecordListFilterForm(request.GET)
Expand Down

0 comments on commit ae53cf1

Please sign in to comment.