Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions inventree/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import inventree.report


logger = logging.getLogger('inventree')


class StockLocation(
inventree.base.BarcodeMixin,
inventree.base.MetadataMixin,
Expand Down Expand Up @@ -58,6 +61,31 @@ class StockItem(

MODEL_TYPE = 'stockitem'

@classmethod
def create(cls, api, data, **kwargs):
""" Override default create method to support multiple object return. """

cls.checkApiVersion(api)

# Ensure the pk value is None so an existing object is not updated
if cls.getPkField() in data.keys():
data.pop(cls.getPkField())

response = api.post(cls.URL, data, **kwargs)

if response is None:
logger.error("Error creating new object")
return None

if isinstance(response, list):
allResponses = []
for element in response:
allResponses.append(cls(api, data=element))
return allResponses

else:
return [cls(api, data=response)]

@classmethod
def adjustStockItems(cls, api: inventree.api.InvenTreeAPI, method: str, items: list, **kwargs):
"""Perform a generic stock 'adjustment' action.
Expand Down
3 changes: 1 addition & 2 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ def test_create_stuff(self):
'part': p.pk,
'quantity': 45,
'notes': 'This is a note',

})
})[0]

self.assertIsNotNone(s)
self.assertEqual(s.part, p.pk)
Expand Down
23 changes: 23 additions & 0 deletions test/test_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ def test_barcode_support(self):

item.unassignBarcode()

def test_serialized(self):
"""Test serializing multiple objects on create"""

# Create items with serial numbers
items = StockItem.create(
self.api,
{
"part": 10004,
"quantity": 3,
"serial_numbers": "1005,1006,1007"
}
)

self.assertEqual(3, len(items))

self.assertEqual('1005', items[0].serial)
self.assertEqual('1006', items[1].serial)
self.assertEqual('1007', items[2].serial)


class StockAdjustTest(InvenTreeTestCase):
"""Unit tests for stock 'adjustment' actions"""
Expand Down Expand Up @@ -407,6 +426,10 @@ def test_assign_stock(self):
}
)

# Verify a single result was returned
self.assertEqual(1, len(assignitem))
assignitem = assignitem[0]

# Assign the item
assignitem.assignStock(customer=customer, notes='Sell on the side')

Expand Down