Improved API filtering for PartCategory

This commit is contained in:
Oliver Walters 2021-04-20 20:15:06 +10:00
parent 38eea21f4f
commit c5fa0bb459
6 changed files with 37 additions and 59 deletions

View File

@ -60,27 +60,39 @@ class CategoryList(generics.ListCreateAPIView):
queryset = PartCategory.objects.all()
serializer_class = part_serializers.CategorySerializer
def get_queryset(self):
def filter_queryset(self, queryset):
"""
Custom filtering:
- Allow filtering by "null" parent to retrieve top-level part categories
"""
cat_id = self.request.query_params.get('parent', None)
queryset = super().filter_queryset(queryset)
queryset = super().get_queryset()
params = self.request.query_params
if cat_id is not None:
cat_id = params.get('parent', None)
cascade = str2bool(params.get('cascade', False))
# Look for top-level categories
if isNull(cat_id):
if not cascade:
queryset = queryset.filter(parent=None)
else:
try:
cat_id = int(cat_id)
queryset = queryset.filter(parent=cat_id)
except ValueError:
category = PartCategory.objects.get(pk=cat_id)
if cascade:
parents = category.get_descendants(include_self=True)
parent_ids = [p.id for p in parents]
queryset = queryset.filter(parent__in=parent_ids)
else:
queryset = queryset.filter(parent=category)
except (ValueError, PartCategory.DoesNotExist):
pass
return queryset

View File

@ -1,42 +0,0 @@
{% extends "part/category.html" %}
{% load i18n %}
{% load inventree_extras %}
{% load static %}
{% block menubar %}
{% include 'part/category_navbar.html' with tab='subcategories' %}
{% endblock %}
{% block category_content %}
<div class='panel panel-default panel-inventree'>
<div class='panel-heading'>
<h4>{% trans "Subcategories" %}</h4>
</div>
<table class='table table-striped table-condensed' id='subcategory-table'></table>
</div>
{% endblock %}
{% block js_ready %}
{{ block.super }}
enableNavbar({
label: 'category',
toggleId: '#category-menu-toggle',
});
loadPartCategoryTable($('#subcategory-table'), {
params: {
{% if category %}
parent: {{ category.pk }}
{% else %}
parent: 'null'
{% endif %}
}
});
{% endblock %}

View File

@ -94,7 +94,7 @@ category_urls = [
url(r'^new/', views.CategoryCreate.as_view(), name='category-create'),
# Top level subcategory display
url(r'^subcategory/', views.PartIndex.as_view(template_name='part/category_subcategory.html'), name='category-index-subcategory'),
url(r'^subcategory/', views.PartIndex.as_view(template_name='part/subcategory.html'), name='category-index-subcategory'),
# Category detail views
url(r'(?P<pk>\d+)/', include([
@ -102,7 +102,7 @@ category_urls = [
url(r'^delete/', views.CategoryDelete.as_view(), name='category-delete'),
url(r'^parameters/', include(category_parameter_urls)),
url(r'^subcategory/', views.CategoryDetail.as_view(template_name='part/category_subcategory.html'), name='category-subcategory'),
url(r'^subcategory/', views.CategoryDetail.as_view(template_name='part/subcategory.html'), name='category-subcategory'),
url(r'^parametric/', views.CategoryParametric.as_view(), name='category-parametric'),
# Anything else

View File

@ -281,13 +281,13 @@ class StockLocationList(generics.ListCreateAPIView):
queryset = StockLocation.objects.all()
serializer_class = LocationSerializer
def get_queryset(self):
def filter_queryset(self, queryset):
"""
Custom filtering:
- Allow filtering by "null" parent to retrieve top-level stock locations
"""
queryset = super().get_queryset()
queryset = super().filter_queryset(queryset)
params = self.request.query_params

View File

@ -32,6 +32,7 @@
</div>
</div>
</div>
<table class='table table-striped table-condensed' data-toolbar='#button-toolbar' id='sublocation-table'></table>
</div>

View File

@ -546,6 +546,7 @@ function loadPartCategoryTable(table, options) {
title: '{% trans "Select" %}',
searchable: false,
switchable: false,
visible: false,
},
{
field: 'name',
@ -565,6 +566,12 @@ function loadPartCategoryTable(table, options) {
switchable: true,
sortable: false,
},
{
field: 'pathstring',
title: '{% trans "Path" %}',
switchable: true,
sortable: false,
},
{
field: 'parts',
title: '{% trans "Parts" %}',