mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-01 13:06:45 +00:00
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:
commit
7399333256
@ -69,6 +69,13 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
|
|
||||||
return queryset
|
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):
|
def get_serializer(self, *args, **kwargs):
|
||||||
|
|
||||||
kwargs['part_detail'] = True
|
kwargs['part_detail'] = True
|
||||||
@ -79,16 +86,6 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
|
|
||||||
return self.serializer_class(*args, **kwargs)
|
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):
|
def perform_destroy(self, instance):
|
||||||
"""
|
"""
|
||||||
Instead of "deleting" the StockItem
|
Instead of "deleting" the StockItem
|
||||||
@ -392,6 +389,13 @@ class StockList(generics.ListCreateAPIView):
|
|||||||
queryset = StockItem.objects.all()
|
queryset = StockItem.objects.all()
|
||||||
filterset_class = StockFilter
|
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):
|
def create(self, request, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Create a new StockItem object via the API.
|
Create a new StockItem object via the API.
|
||||||
|
@ -265,15 +265,15 @@ class StockItem(MPTTModel):
|
|||||||
|
|
||||||
user = kwargs.pop('user', None)
|
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
|
# If 'add_note = False' specified, then no tracking note will be added for item creation
|
||||||
add_note = kwargs.pop('add_note', True)
|
add_note = kwargs.pop('add_note', True)
|
||||||
|
|
||||||
notes = kwargs.pop('notes', '')
|
notes = kwargs.pop('notes', '')
|
||||||
|
|
||||||
if not self.pk:
|
if self.pk:
|
||||||
# StockItem has not yet been saved
|
|
||||||
add_note = add_note and True
|
|
||||||
else:
|
|
||||||
# StockItem has already been saved
|
# StockItem has already been saved
|
||||||
|
|
||||||
# Check if "interesting" fields have been changed
|
# Check if "interesting" fields have been changed
|
||||||
@ -301,11 +301,10 @@ class StockItem(MPTTModel):
|
|||||||
except (ValueError, StockItem.DoesNotExist):
|
except (ValueError, StockItem.DoesNotExist):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
add_note = False
|
|
||||||
|
|
||||||
super(StockItem, self).save(*args, **kwargs)
|
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 = {
|
tracking_info = {
|
||||||
'status': self.status,
|
'status': self.status,
|
||||||
|
@ -81,6 +81,15 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeModelSerializer):
|
|||||||
- Includes serialization for the item location
|
- 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
|
@staticmethod
|
||||||
def annotate_queryset(queryset):
|
def annotate_queryset(queryset):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user