From 0f8cddd23de72945e63ef333e3558f693f952b97 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 21 Sep 2023 13:22:20 +1000 Subject: [PATCH] Add validation check to "Convert to Variant" form (#5583) * Add validation check to "Convert to Variant" form - If a supplierpart is assigned, cannot convert stock item * Fix failing unit test --- InvenTree/label/tests.py | 2 +- InvenTree/stock/serializers.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/InvenTree/label/tests.py b/InvenTree/label/tests.py index 9bb3435eac..5d88ccef60 100644 --- a/InvenTree/label/tests.py +++ b/InvenTree/label/tests.py @@ -154,7 +154,7 @@ class LabelTest(InvenTreeAPITestCase): # Test that each element has been rendered correctly self.assertIn(f"part: {part_pk} - {part_name}", content) self.assertIn(f'data: {{"part": {part_pk}}}', content) - self.assertIn("http://testserver/part/1/", content) + self.assertIn(f'http://testserver/part/{part_pk}/', content) self.assertIn("img/blank_image.png", content) self.assertIn("img/inventree.png", content) diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index c6b3676e59..9328c9ce1a 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -600,6 +600,21 @@ class ConvertStockItemSerializer(serializers.Serializer): return part + def validate(self, data): + """Ensure that the stock item is valid for conversion: + + - If a SupplierPart is assigned, we cannot convert! + """ + + data = super().validate(data) + + stock_item = self.context['item'] + + if stock_item.supplier_part is not None: + raise ValidationError(_("Cannot convert stock item with assigned SupplierPart")) + + return data + def save(self): """Save the serializer to convert the StockItem to the selected Part""" data = self.validated_data