From 9e16989c914f0ddcdb654892310813179e8a5e6d Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 11 Dec 2021 00:25:59 +1100 Subject: [PATCH] Add same breadcrumb tree for StockLocation and StockItem --- InvenTree/stock/api.py | 21 +++++++++++++++++++ InvenTree/stock/serializers.py | 14 +++++++++++++ .../stock/templates/stock/item_base.html | 20 ++++++++++++++++++ InvenTree/stock/templates/stock/location.html | 19 +++++++++++++++++ .../stock/templates/stock/stock_app_base.html | 3 ++- 5 files changed, 76 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 26787878a8..9961bb7bae 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -277,6 +277,24 @@ class StockLocationList(generics.ListCreateAPIView): ] +class StockLocationTree(generics.ListAPIView): + """ + API endpoint for accessing a list of StockLocation objects, + ready for rendering as a tree + """ + + queryset = StockLocation.objects.all() + serializer_class = StockSerializers.LocationTreeSerializer + + filter_backends = [ + DjangoFilterBackend, + filters.OrderingFilter, + ] + + # Order by tree level (top levels first) and then name + ordering = ['level', 'name'] + + class StockFilter(rest_filters.FilterSet): """ FilterSet for StockItem LIST API @@ -1182,6 +1200,9 @@ class LocationDetail(generics.RetrieveUpdateDestroyAPIView): stock_api_urls = [ url(r'^location/', include([ + + url(r'^tree/', StockLocationTree.as_view(), name='api-location-tree'), + url(r'^(?P\d+)/', LocationDetail.as_view(), name='api-location-detail'), url(r'^.*$', StockLocationList.as_view(), name='api-location-list'), ])), diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index fb78eaeaa0..e69cd90f82 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -390,6 +390,20 @@ class SerializeStockItemSerializer(serializers.Serializer): ) +class LocationTreeSerializer(InvenTree.serializers.InvenTreeModelSerializer): + """ + Serializer for a simple tree view + """ + + class Meta: + model = StockLocation + fields = [ + 'pk', + 'name', + 'parent', + ] + + class LocationSerializer(InvenTree.serializers.InvenTreeModelSerializer): """ Detailed information about a stock location """ diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 64b45ed0c8..65f5f21000 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -9,9 +9,15 @@ {% endblock %} {% block breadcrumbs %} + {% include 'stock/loc_link.html' with location=item.location %} {% endblock %} +{% block breadcrumb_tree %} + +{% endblock breadcrumb_tree %} + + {% block heading %} {% trans "Stock Item" %}: {{ item.part.full_name}} {% endblock heading %} @@ -611,4 +617,18 @@ $('#serial-number-search').click(function() { findStockItemBySerialNumber({{ item.part.pk }}); }); +enableBreadcrumbTree({ + label: 'stockitem', + url: '{% url "api-location-tree" %}', + {% if item.location %} + selected: {{ item.location.pk }}, + {% endif %} + processNode: function(node) { + node.text = node.name; + node.href = `/stock/item/${node.pk}/`; + + return node; + } +}); + {% endblock %} diff --git a/InvenTree/stock/templates/stock/location.html b/InvenTree/stock/templates/stock/location.html index 6a201e610c..39b9faedb4 100644 --- a/InvenTree/stock/templates/stock/location.html +++ b/InvenTree/stock/templates/stock/location.html @@ -7,6 +7,10 @@ {% include "stock/location_sidebar.html" %} {% endblock %} +{% block breadcrumb_tree %} + +{% endblock breadcrumb_tree %} + {% block heading %} {% if location %} {% trans "Stock Location" %}: {{ location.name }} @@ -348,4 +352,19 @@ enableSidebar('stocklocation'); + // Enable breadcrumb tree view + enableBreadcrumbTree({ + label: 'location', + url: '{% url "api-location-tree" %}', + {% if location %} + selected: {{ location.pk }}, + {% endif %} + processNode: function(node) { + node.text = node.name; + node.href = `/stock/location/${node.pk}/`; + + return node; + } + }); + {% endblock %} diff --git a/InvenTree/stock/templates/stock/stock_app_base.html b/InvenTree/stock/templates/stock/stock_app_base.html index 3da3fad240..93994ebd21 100644 --- a/InvenTree/stock/templates/stock/stock_app_base.html +++ b/InvenTree/stock/templates/stock/stock_app_base.html @@ -18,9 +18,10 @@ {% endblock %} {% block breadcrumbs %} + {% if item %} {% include 'stock/loc_link.html' with location=item.location %} {% else %} {% include 'stock/loc_link.html' with location=location %} {% endif %} -{% endblock %} +{% endblock breadcrumbs %}