mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-14 02:55:41 +00:00
update api to support custom status
This commit is contained in:
@ -156,7 +156,11 @@ class InvenTreeCustomStatusSerializerMixin:
|
|||||||
):
|
):
|
||||||
setattr(self.instance, follower_field_name, self.initial_data[field])
|
setattr(self.instance, follower_field_name, self.initial_data[field])
|
||||||
|
|
||||||
# Mirror values from follower to leader
|
self.mirror_follower(validated_data)
|
||||||
|
return super().update(instance, validated_data)
|
||||||
|
|
||||||
|
def mirror_follower(self, validated_data):
|
||||||
|
"""Mirror the follower fields to the leader fields."""
|
||||||
for field in self._custom_fields_follower:
|
for field in self._custom_fields_follower:
|
||||||
leader_field_name = field.replace('_custom_key', '')
|
leader_field_name = field.replace('_custom_key', '')
|
||||||
if field in validated_data and leader_field_name not in self.initial_data:
|
if field in validated_data and leader_field_name not in self.initial_data:
|
||||||
@ -171,7 +175,6 @@ class InvenTreeCustomStatusSerializerMixin:
|
|||||||
validated_data[leader_field_name] = validated_data[field]
|
validated_data[leader_field_name] = validated_data[field]
|
||||||
else:
|
else:
|
||||||
raise serializers.ValidationError('Invalid choice')
|
raise serializers.ValidationError('Invalid choice')
|
||||||
return super().update(instance, validated_data)
|
|
||||||
|
|
||||||
def to_representation(self, instance):
|
def to_representation(self, instance):
|
||||||
"""Ensure custom state fields are not served empty."""
|
"""Ensure custom state fields are not served empty."""
|
||||||
|
@ -18,6 +18,8 @@ from taggit.serializers import TagListSerializerField
|
|||||||
import build.models
|
import build.models
|
||||||
import company.models
|
import company.models
|
||||||
import company.serializers as company_serializers
|
import company.serializers as company_serializers
|
||||||
|
import generic.states.fields
|
||||||
|
import InvenTree.fields
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.serializers
|
import InvenTree.serializers
|
||||||
import order.models
|
import order.models
|
||||||
@ -999,13 +1001,15 @@ class ReturnStockItemSerializer(serializers.Serializer):
|
|||||||
item.return_from_customer(location, user=request.user, notes=notes)
|
item.return_from_customer(location, user=request.user, notes=notes)
|
||||||
|
|
||||||
|
|
||||||
class StockChangeStatusSerializer(serializers.Serializer):
|
class StockChangeStatusSerializer(
|
||||||
|
InvenTreeCustomStatusSerializerMixin, serializers.Serializer
|
||||||
|
):
|
||||||
"""Serializer for changing status of multiple StockItem objects."""
|
"""Serializer for changing status of multiple StockItem objects."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Metaclass options."""
|
"""Metaclass options."""
|
||||||
|
|
||||||
fields = ['items', 'status', 'note']
|
fields = ['items', 'status', 'status_custom_key', 'note']
|
||||||
|
|
||||||
items = serializers.PrimaryKeyRelatedField(
|
items = serializers.PrimaryKeyRelatedField(
|
||||||
queryset=StockItem.objects.all(),
|
queryset=StockItem.objects.all(),
|
||||||
@ -1029,6 +1033,14 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
|||||||
label=_('Status'),
|
label=_('Status'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
status_custom_key = generic.states.fields.ExtraCustomChoiceField(
|
||||||
|
choices=stock.status_codes.StockStatus.items(),
|
||||||
|
default=status.default,
|
||||||
|
choice_mdl=StockItem,
|
||||||
|
choice_field='status',
|
||||||
|
is_custom=True,
|
||||||
|
)
|
||||||
|
|
||||||
note = serializers.CharField(
|
note = serializers.CharField(
|
||||||
label=_('Notes'),
|
label=_('Notes'),
|
||||||
help_text=_('Add transaction note (optional)'),
|
help_text=_('Add transaction note (optional)'),
|
||||||
@ -1041,8 +1053,12 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
|||||||
"""Save the serializer to change the status of the selected stock items."""
|
"""Save the serializer to change the status of the selected stock items."""
|
||||||
data = self.validated_data
|
data = self.validated_data
|
||||||
|
|
||||||
|
self.gather_custom_fields()
|
||||||
|
self.mirror_follower(data)
|
||||||
|
|
||||||
items = data['items']
|
items = data['items']
|
||||||
status = data['status']
|
status = data['status']
|
||||||
|
status_custom_key = data['status_custom_key']
|
||||||
|
|
||||||
request = self.context['request']
|
request = self.context['request']
|
||||||
user = getattr(request, 'user', None)
|
user = getattr(request, 'user', None)
|
||||||
@ -1052,7 +1068,7 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
|||||||
items_to_update = []
|
items_to_update = []
|
||||||
transaction_notes = []
|
transaction_notes = []
|
||||||
|
|
||||||
deltas = {'status': status}
|
deltas = {'status': status, 'status_custom_key': status_custom_key}
|
||||||
|
|
||||||
now = InvenTree.helpers.current_time()
|
now = InvenTree.helpers.current_time()
|
||||||
|
|
||||||
@ -1061,11 +1077,12 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
|||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
# Ignore items which are already in the desired status
|
# Ignore items which are already in the desired status
|
||||||
if item.status == status:
|
if item.status == status and item.status_custom_key == status_custom_key:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
item.updated = now
|
item.updated = now
|
||||||
item.status = status
|
item.status = status
|
||||||
|
item.status_custom_key = status_custom_key
|
||||||
items_to_update.append(item)
|
items_to_update.append(item)
|
||||||
|
|
||||||
# Create a new transaction note for each item
|
# Create a new transaction note for each item
|
||||||
@ -1081,7 +1098,9 @@ class StockChangeStatusSerializer(serializers.Serializer):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Update status
|
# Update status
|
||||||
StockItem.objects.bulk_update(items_to_update, ['status', 'updated'])
|
StockItem.objects.bulk_update(
|
||||||
|
items_to_update, ['status', 'status_custom_key', 'updated']
|
||||||
|
)
|
||||||
|
|
||||||
# Create entries
|
# Create entries
|
||||||
StockItemTracking.objects.bulk_create(transaction_notes)
|
StockItemTracking.objects.bulk_create(transaction_notes)
|
||||||
|
@ -737,7 +737,7 @@ function stockChangeStatusFields(items: any[]): ApiFormFieldSet {
|
|||||||
},
|
},
|
||||||
headers: [t`Part`, t`Location`, t`In Stock`, t`Actions`]
|
headers: [t`Part`, t`Location`, t`In Stock`, t`Actions`]
|
||||||
},
|
},
|
||||||
status: {},
|
status_custom_key: {},
|
||||||
note: {}
|
note: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user