From 487794a9382064e08156a5b0b130d8f35152f64e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Feb 2021 16:29:25 +1100 Subject: [PATCH] Server side pagination for the part list --- InvenTree/part/api.py | 27 ++++++++++++--------------- InvenTree/stock/api.py | 3 +-- InvenTree/templates/js/part.js | 3 ++- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 9a0e3a2a68..ff4c9b3f94 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -9,6 +9,7 @@ from django_filters.rest_framework import DjangoFilterBackend from django.http import JsonResponse from django.db.models import Q, F, Count, Prefetch, Sum +from rest_framework.pagination import LimitOffsetPagination from rest_framework import status from rest_framework.response import Response from rest_framework import filters, serializers @@ -371,6 +372,8 @@ class PartList(generics.ListCreateAPIView): starred_parts = None + pagination_class = LimitOffsetPagination + def get_serializer(self, *args, **kwargs): # Ensure the request context is passed through @@ -397,11 +400,11 @@ class PartList(generics.ListCreateAPIView): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) + if page is not None: serializer = self.get_serializer(page, many=True) - return self.get_paginated_response(serializer.data) - - serializer = self.get_serializer(queryset, many=True) + else: + serializer = self.get_serializer(queryset, many=True) data = serializer.data @@ -445,7 +448,9 @@ class PartList(generics.ListCreateAPIView): a) For HTTP requests (e.g. via the browseable API) return a DRF response b) For AJAX requests, simply return a JSON rendered response. """ - if request.is_ajax(): + if page is not None: + return self.get_paginated_response(data) + elif request.is_ajax(): return JsonResponse(data, safe=False) else: return Response(data) @@ -641,17 +646,6 @@ class PartList(generics.ListCreateAPIView): queryset = queryset.filter(pk__in=parts_need_stock) - # Limit number of results - limit = params.get('limit', None) - - if limit is not None: - try: - limit = int(limit) - if limit > 0: - queryset = queryset[:limit] - except ValueError: - pass - return queryset filter_backends = [ @@ -674,6 +668,8 @@ class PartList(generics.ListCreateAPIView): ordering_fields = [ 'name', 'creation_date', + 'IPN', + 'in_stock', ] # Default ordering @@ -685,6 +681,7 @@ class PartList(generics.ListCreateAPIView): 'IPN', 'revision', 'keywords', + 'category__name', ] diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index ac4ba8b02e..1bc0817083 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -387,7 +387,6 @@ class StockList(generics.ListCreateAPIView): page = self.paginate_queryset(queryset) if page is not None: - serializer = self.get_serializer(page, many=True) else: serializer = self.get_serializer(queryset, many=True) @@ -476,7 +475,7 @@ class StockList(generics.ListCreateAPIView): if page is not None: return self.get_paginated_response(data) - if request.is_ajax(): + elif request.is_ajax(): return JsonResponse(data, safe=False) else: return Response(data) diff --git a/InvenTree/templates/js/part.js b/InvenTree/templates/js/part.js index 9f770f452e..b12788cfe8 100644 --- a/InvenTree/templates/js/part.js +++ b/InvenTree/templates/js/part.js @@ -366,7 +366,6 @@ function loadPartTable(table, url, options={}) { }); columns.push({ - sortable: true, field: 'description', title: '{% trans "Description" %}', formatter: function(value, row, index, field) { @@ -448,6 +447,8 @@ function loadPartTable(table, url, options={}) { groupBy: false, name: options.name || 'part', original: params, + sidePagination: 'server', + pagination: 'true', formatNoMatches: function() { return '{% trans "No parts found" %}'; }, columns: columns, showColumns: true,