From f2982a59ad72fed39a0cbe47bcd6cadea0bbf3d7 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 8 May 2018 22:30:32 +1000 Subject: [PATCH] Added StockTracking API /api/stock/track/ ListCreate API view StockItem page now shows bootrap-table of tracking notes --- InvenTree/stock/api.py | 38 +++++++++++++----- InvenTree/stock/models.py | 3 ++ InvenTree/stock/serializers.py | 25 ++++++++++++ InvenTree/stock/templates/stock/item.html | 47 +++++++++++++++++------ 4 files changed, 93 insertions(+), 20 deletions(-) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 3d7a44af73..7516824318 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -7,8 +7,11 @@ from django.conf.urls import url, include # from InvenTree.models import FilterChildren from .models import StockLocation, StockItem +from .models import StockItemTracking + from .serializers import StockItemSerializer, StockQuantitySerializer from .serializers import LocationSerializer +from .serializers import StockTrackingSerializer from InvenTree.views import TreeSerializer from InvenTree.serializers import DraftRUDView @@ -242,19 +245,34 @@ class StockStocktakeEndpoint(generics.UpdateAPIView): return response.Response(serializer.data) -class AddStockEndpoint(generics.UpdateAPIView): +class StockTrackingList(generics.ListCreateAPIView): - queryset = StockItem.objects.all() - serializer_class = StockQuantitySerializer - permission_classes = (permissions.IsAuthenticatedOrReadOnly,) + queryset = StockItemTracking.objects.all() + serializer_class = StockTrackingSerializer + permission_classes = [permissions.IsAuthenticatedOrReadOnly,] - def update(self, request, *args, **kwargs): - object = self.get_object() - object.add_stock(request.data['quantity']) + filter_backends = [ + DjangoFilterBackend, + filters.SearchFilter, + filters.OrderingFilter, + ] - serializer = self.get_serializer(object) + filter_fields = [ + 'item', + 'user', + ] + + ordering = '-date' + + ordering_fields = [ + 'date', + ] + + search_fields = [ + 'title', + 'notes', + ] - return response.Response(serializer.data) class LocationDetail(generics.RetrieveUpdateDestroyAPIView): @@ -297,6 +315,8 @@ stock_api_urls = [ url(r'move/?', StockMove.as_view(), name='api-stock-move'), + url(r'track/?', StockTrackingList.as_view(), name='api-stock-track'), + url(r'^tree/?', StockCategoryTree.as_view(), name='api-stock-tree'), url(r'^.*$', StockList.as_view(), name='api-stock-list'), diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 604e4836a1..3533b185cb 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -320,6 +320,9 @@ class StockItemTracking(models.Model): """ Stock tracking entry """ + def get_absolute_url(self): + return '/stock/track/{id}/'.format(id=self.id) + # Stock item item = models.ForeignKey(StockItem, on_delete=models.CASCADE, related_name='tracking_info') diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 54d4dbf883..16ef568616 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -1,6 +1,7 @@ from rest_framework import serializers from .models import StockItem, StockLocation +from .models import StockItemTracking from part.serializers import PartBriefSerializer @@ -19,6 +20,30 @@ class LocationBriefSerializer(serializers.ModelSerializer): ] +class StockTrackingSerializer(serializers.ModelSerializer): + + url = serializers.CharField(source='get_absolute_url', read_only=True) + + class Meta: + model = StockItemTracking + fields = [ + 'pk', + 'url', + 'item', + 'date', + 'title', + 'notes', + 'user', + 'system', + ] + + read_only_fields = [ + 'date', + 'user', + 'system', + ] + + class StockItemSerializer(serializers.ModelSerializer): """ Serializer for a StockItem """ diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 6b8738d4ef..2540ea3bd8 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -113,17 +113,8 @@
-
    - {% for track in item.tracking_info.all %} -
  • - {{ track.title }} - {% if track.notes %} -
    {{ track.notes }} - {% endif %} - {{ track.date }}{% if track.user %} - {{ track.user }}{% endif %} -
  • - {% endfor %} -
+ +
@@ -185,4 +176,38 @@ redirect: "{% url 'part-stock' item.part.id %}" }); }); + + $('#track-table').bootstrapTable({ + sortable: true, + search: true, + method: 'get', + columns: [ + { + field: 'date', + title: 'Date', + sortable: true, + }, + { + field: 'title', + title: 'Description', + sortable: true, + formatter: function(value, row, index, field) { + var html = "" + value + ""; + + if (row.notes) { + html += "
" + row.notes + ""; + } + + return html; + } + }, + { + sortable: true, + field: 'user', + title: 'User', + } + ], + url: "{% url 'api-stock-track' %}", + }) + {% endblock %} \ No newline at end of file