From 9d98c429d665f4f4f30b15eb254518af6b636305 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 14 Apr 2019 11:59:07 +1000 Subject: [PATCH 1/3] Complex filtering for parts - Allow filtering against child categories - Could potentially be slooow --- InvenTree/part/api.py | 30 +++++++++++++++++++-- InvenTree/part/templates/part/category.html | 1 + 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 6e5add9433..e119188def 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -5,7 +5,9 @@ from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters from rest_framework import generics, permissions +from django.db.models import Q from django.conf.urls import url, include +from django.shortcuts import get_object_or_404 from .models import Part, PartCategory, BomItem from .models import SupplierPart @@ -65,9 +67,33 @@ class PartDetail(DraftRUDView): class PartList(generics.ListCreateAPIView): - queryset = Part.objects.all() + #queryset = Part.objects.all() serializer_class = PartSerializer + def get_queryset(self): + print("Get queryset") + + # Does the user wish to filter by category? + cat_id = self.request.query_params.get('category', None) + + if cat_id: + print("Getting category:", cat_id) + category = get_object_or_404(PartCategory, pk=cat_id) + + # Filter by the supplied category + flt = Q(category=cat_id) + + if self.request.query_params.get('include_child_categories', None): + childs = category.getUniqueChildren() + for child in childs: + if child == cat_id: continue + flt |= Q(category=child) + + return Part.objects.filter(flt) + + # Default - return all parts + return Part.objects.all() + permission_classes = [ permissions.IsAuthenticatedOrReadOnly, ] @@ -79,7 +105,7 @@ class PartList(generics.ListCreateAPIView): ] filter_fields = [ - 'category', + #'category', ] ordering_fields = [ diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index ec75d2c874..23a16dd05f 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -120,6 +120,7 @@ return { {% if category %} category: {{ category.id }}, + include_child_categories: true, {% endif %} } }, From 00c21d521e7bf675cd2a4c87757791b1f8eeeeb7 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 14 Apr 2019 12:25:21 +1000 Subject: [PATCH 2/3] Always display category - Display full category path --- InvenTree/part/templates/part/category.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index 23a16dd05f..111efe7fcf 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -149,21 +149,19 @@ field: 'description', title: 'Description', }, - {% if category == None %} { sortable: true, field: 'category', title: 'Category', formatter: function(value, row, index, field) { if (row.category) { - return renderLink(row.category.name, row.category.url); + return renderLink(row.category.pathstring, row.category.url); } else { return ''; } } }, - {% endif %} { field: 'total_stock', title: 'Stock', From 2202dfd55d40081d9e1fe39c9003e7d12af5871f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 14 Apr 2019 12:30:06 +1000 Subject: [PATCH 3/3] Peppy fixes --- .travis.yml | 1 - InvenTree/part/api.py | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 82bc201184..09f5fa6f2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ dist: xenial language: python python: - 3.5 - - 3.6 addons: apt-packages: diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index e119188def..024d61a4a5 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -67,7 +67,6 @@ class PartDetail(DraftRUDView): class PartList(generics.ListCreateAPIView): - #queryset = Part.objects.all() serializer_class = PartSerializer def get_queryset(self): @@ -86,11 +85,13 @@ class PartList(generics.ListCreateAPIView): if self.request.query_params.get('include_child_categories', None): childs = category.getUniqueChildren() for child in childs: - if child == cat_id: continue + # Ignore the top-level category (already filtered) + if child == cat_id: + continue flt |= Q(category=child) return Part.objects.filter(flt) - + # Default - return all parts return Part.objects.all() @@ -105,7 +106,6 @@ class PartList(generics.ListCreateAPIView): ] filter_fields = [ - #'category', ] ordering_fields = [