mirror of
https://github.com/inventree/InvenTree.git
synced 2026-04-08 12:31:06 +00:00
Allow bulk edit of stock batch code (#11499)
* Allow bulk edit of stock batch code Closes https://github.com/inventree/InvenTree/issues/10817 * Bump API version * Add unit test
This commit is contained in:
@@ -594,17 +594,24 @@ class BulkUpdateMixin(BulkOperationMixin):
|
||||
|
||||
n = queryset.count()
|
||||
|
||||
instance_data = []
|
||||
|
||||
with transaction.atomic():
|
||||
# Perform object update
|
||||
# Note that we do not perform a bulk-update operation here,
|
||||
# as we want to trigger any custom post_save methods on the model
|
||||
|
||||
# Run validation first
|
||||
for instance in queryset:
|
||||
serializer = self.get_serializer(instance, data=data, partial=True)
|
||||
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
|
||||
return Response({'success': f'Updated {n} items'}, status=200)
|
||||
instance_data.append(serializer.data)
|
||||
|
||||
return Response(
|
||||
{'success': f'Updated {n} items', 'items': instance_data}, status=200
|
||||
)
|
||||
|
||||
|
||||
class ParameterListMixin:
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
"""InvenTree API version information."""
|
||||
|
||||
# InvenTree API version
|
||||
INVENTREE_API_VERSION = 462
|
||||
INVENTREE_API_VERSION = 463
|
||||
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
|
||||
|
||||
INVENTREE_API_TEXT = """
|
||||
|
||||
v463 -> 2026-03-12 : https://github.com/inventree/InvenTree/pull/11499
|
||||
- Allow "bulk update" actions against StockItem endpoint
|
||||
|
||||
v462 -> 2026-03-12 : https://github.com/inventree/InvenTree/pull/11497
|
||||
- Allow "ScheduledTask" API endpoint to be filtered by "name" field
|
||||
|
||||
|
||||
@@ -1050,7 +1050,11 @@ class StockOutputOptions(OutputConfiguration):
|
||||
|
||||
|
||||
class StockList(
|
||||
DataExportViewMixin, StockApiMixin, OutputOptionsMixin, ListCreateDestroyAPIView
|
||||
DataExportViewMixin,
|
||||
BulkUpdateMixin,
|
||||
StockApiMixin,
|
||||
OutputOptionsMixin,
|
||||
ListCreateDestroyAPIView,
|
||||
):
|
||||
"""API endpoint for list view of Stock objects.
|
||||
|
||||
|
||||
@@ -2006,6 +2006,33 @@ class StockItemTest(StockAPITestCase):
|
||||
self.assertEqual(tracking.deltas['status'], StockStatus.OK.value)
|
||||
self.assertEqual(tracking.deltas['status_logical'], StockStatus.OK.value)
|
||||
|
||||
def test_bulk_batch_change(self):
|
||||
"""Test that we can bulk-change batch code for a set of stock items."""
|
||||
url = reverse('api-stock-list')
|
||||
|
||||
# Find the first 10 stock items
|
||||
items = StockItem.objects.all()[:10]
|
||||
self.assertEqual(len(items), 10)
|
||||
|
||||
response = self.patch(
|
||||
url,
|
||||
data={'items': [item.pk for item in items], 'batch': 'NEW-BATCH-CODE'},
|
||||
max_query_count=300,
|
||||
)
|
||||
|
||||
data = response.data
|
||||
|
||||
self.assertEqual(data['success'], 'Updated 10 items')
|
||||
self.assertEqual(len(data['items']), 10)
|
||||
|
||||
for item in data['items']:
|
||||
self.assertEqual(item['batch'], 'NEW-BATCH-CODE')
|
||||
|
||||
# Check database items also
|
||||
for item in items:
|
||||
item.refresh_from_db()
|
||||
self.assertEqual(item.batch, 'NEW-BATCH-CODE')
|
||||
|
||||
|
||||
class StocktakeTest(StockAPITestCase):
|
||||
"""Series of tests for the Stocktake API."""
|
||||
|
||||
Reference in New Issue
Block a user