Merge pull request #2328 from SchrodingersGat/stock-item-create-fix

Record the user information when creating or updating a stock item
This commit is contained in:
Oliver 2021-11-18 23:34:41 +11:00 committed by GitHub
commit 7399333256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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'] = getattr(self.request, 'user', None)
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'] = getattr(self.request, 'user', None)
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

@ -81,6 +81,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):
"""