From eaf42b8abe4aad5824a13fd301e50030cf1c7ad4 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 19 Sep 2020 20:26:17 +1000 Subject: [PATCH] Instead of creating a custom filter for "latest" parts, simply make use of the existing "ordering" query as part of DRF --- InvenTree/part/api.py | 19 ++++++++++++------- InvenTree/templates/InvenTree/index.html | 8 +++++--- InvenTree/templates/js/part.html | 10 +++++++++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index cf260d7bfa..93cc013da7 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -405,13 +405,6 @@ class PartList(generics.ListCreateAPIView): except (ValueError, Part.DoesNotExist): pass - # Filter by latest part creation date - latest_parts = params.get('latest_parts', None) - - if latest_parts is not None: - # Get the last 5 created parts - queryset = queryset.order_by('-creation_date')[:5] - # Filter invalid BOMs bom_invalid = params.get('bom_invalid', None) @@ -514,6 +507,17 @@ class PartList(generics.ListCreateAPIView): queryset = queryset.filter(pk__in=parts_need_stock) + # Limit choices + 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 permission_classes = [ @@ -539,6 +543,7 @@ class PartList(generics.ListCreateAPIView): ordering_fields = [ 'name', + 'creation_date', ] # Default ordering diff --git a/InvenTree/templates/InvenTree/index.html b/InvenTree/templates/InvenTree/index.html index fa9818069e..a3f99d8b45 100644 --- a/InvenTree/templates/InvenTree/index.html +++ b/InvenTree/templates/InvenTree/index.html @@ -55,10 +55,12 @@ InvenTree | Index {{ block.super }} -loadPartTable("#latest-parts-table", "{% url 'api-part-list' %}", { +loadSimplePartTable("#latest-parts-table", "{% url 'api-part-list' %}", { params: { - "latest_parts": true, - } + ordering: "-creation_date", + limit: 10, + }, + name: 'latest_parts', }); loadPartTable("#starred-parts-table", "{% url 'api-part-list' %}", { diff --git a/InvenTree/templates/js/part.html b/InvenTree/templates/js/part.html index 03c98e09a4..5576d91367 100644 --- a/InvenTree/templates/js/part.html +++ b/InvenTree/templates/js/part.html @@ -155,6 +155,14 @@ function loadPartVariantTable(table, partId, options) { } +function loadSimplePartTable(table, url, options={}) { + + options.disableFilters = true; + + loadPartTable(table, url, options); +} + + function loadPartTable(table, url, options={}) { /* Load part listing data into specified table. * @@ -332,7 +340,7 @@ function loadPartTable(table, url, options={}) { method: 'get', queryParams: filters, groupBy: false, - name: 'part', + name: options.name || 'part', original: params, formatNoMatches: function() { return "{% trans "No parts found" %}"; }, columns: columns,