Record the user information when creating or updating a stock item

This commit is contained in:
Oliver 2021-11-18 21:26:26 +11:00
parent 8a879832ee
commit bfb32f3bf6
3 changed files with 30 additions and 18 deletions

View File

@ -69,6 +69,13 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
return queryset
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['user'] = self.request.user
return ctx
def get_serializer(self, *args, **kwargs):
kwargs['part_detail'] = True
@ -79,16 +86,6 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
return self.serializer_class(*args, **kwargs)
def update(self, request, *args, **kwargs):
"""
Record the user who updated the item
"""
# TODO: Record the user!
# user = request.user
return super().update(request, *args, **kwargs)
def perform_destroy(self, instance):
"""
Instead of "deleting" the StockItem
@ -392,6 +389,13 @@ class StockList(generics.ListCreateAPIView):
queryset = StockItem.objects.all()
filterset_class = StockFilter
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['user'] = self.request.user
return ctx
def create(self, request, *args, **kwargs):
"""
Create a new StockItem object via the API.

View File

@ -265,15 +265,15 @@ class StockItem(MPTTModel):
user = kwargs.pop('user', None)
if user is None:
user = getattr(self, '_user', None)
# If 'add_note = False' specified, then no tracking note will be added for item creation
add_note = kwargs.pop('add_note', True)
notes = kwargs.pop('notes', '')
if not self.pk:
# StockItem has not yet been saved
add_note = add_note and True
else:
if self.pk:
# StockItem has already been saved
# Check if "interesting" fields have been changed
@ -301,11 +301,10 @@ class StockItem(MPTTModel):
except (ValueError, StockItem.DoesNotExist):
pass
add_note = False
super(StockItem, self).save(*args, **kwargs)
if add_note:
# If user information is provided, and no existing note exists, create one!
if user and self.tracking_info.count() == 0:
tracking_info = {
'status': self.status,

View File

@ -79,6 +79,15 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeModelSerializer):
- Includes serialization for the item location
"""
def update(self, instance, validated_data):
"""
Custom update method to pass the user information through to the instance
"""
instance._user = self.context['user']
return super().update(instance, validated_data)
@staticmethod
def annotate_queryset(queryset):
"""