diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 03333569ef..f72f2590cc 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -5,6 +5,8 @@ JSON API for the Stock app from django_filters.rest_framework import FilterSet, DjangoFilterBackend from django_filters import NumberFilter +from rest_framework import status + from django.conf.urls import url, include from django.urls import reverse from django.http import JsonResponse @@ -658,6 +660,43 @@ class StockTrackingList(generics.ListCreateAPIView): serializer_class = StockTrackingSerializer permission_classes = [permissions.IsAuthenticated] + def get_serializer(self, *args, **kwargs): + try: + kwargs['item_detail'] = str2bool(self.request.query_params.get('item_detail', False)) + except: + pass + + try: + kwargs['user_detail'] = str2bool(self.request.query_params.get('user_detail', False)) + except: + pass + + kwargs['context'] = self.get_serializer_context() + + return self.serializer_class(*args, **kwargs) + + def create(self, request, *args, **kwargs): + """ Create a new StockItemTracking object + + Here we override the default 'create' implementation, + to save the user information associated with the request object. + """ + + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + + # Record the user who created this Part object + item = serializer.save() + item.user = request.user + item.system = False + + # quantity field cannot be explicitly adjusted here + item.quantity = item.item.quantity + item.save() + + headers = self.get_success_headers(serializer.data) + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + filter_backends = [ DjangoFilterBackend, filters.SearchFilter, diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 7e9a59470b..69cb726f73 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -207,11 +207,24 @@ class StockItemAttachmentSerializer(InvenTreeModelSerializer): class StockTrackingSerializer(InvenTreeModelSerializer): """ Serializer for StockItemTracking model """ + def __init__(self, *args, **kwargs): + + item_detail = kwargs.pop('item_detail', False) + user_detail = kwargs.pop('user_detail', False) + + super().__init__(*args, **kwargs) + + if item_detail is not True: + self.fields.pop('item_detail') + + if user_detail is not True: + self.fields.pop('user_detail') + url = serializers.CharField(source='get_absolute_url', read_only=True) - user = UserSerializerBrief(many=False, read_only=True) + item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True) - item = StockItemSerializerBrief(many=False, read_only=True) + user_detail = UserSerializerBrief(source='user', many=False, read_only=True) class Meta: model = StockItemTracking @@ -219,12 +232,14 @@ class StockTrackingSerializer(InvenTreeModelSerializer): 'pk', 'url', 'item', + 'item_detail', 'date', 'title', 'notes', 'link', 'quantity', 'user', + 'user_detail', 'system', ] diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 38c1d7dcac..517acb15fe 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -38,6 +38,7 @@ return { ordering: '-date', item: {{ item.pk }}, + user_detail: true, }; }, url: "{% url 'api-stock-track' %}", diff --git a/InvenTree/templates/InvenTree/settings/part.html b/InvenTree/templates/InvenTree/settings/part.html index 96507c7ec6..49631cd67d 100644 --- a/InvenTree/templates/InvenTree/settings/part.html +++ b/InvenTree/templates/InvenTree/settings/part.html @@ -6,7 +6,7 @@ {% endblock %} {% block settings %} -