Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Change params to context in InsufficientStock exception

fowczrek82a48c67d935
diff --git a/saleor/core/exceptions.py b/saleor/core/exceptions.py
index 540ef9e091..2269677488 100644
--- a/saleor/core/exceptions.py
+++ b/saleor/core/exceptions.py
@@ -2,11 +2,10 @@ from ..checkout.error_codes import CheckoutErrorCode
class InsufficientStock(Exception):
- def __init__(self, item, order_line=None, warehouse_pk=None):
+ def __init__(self, item, context=None):
super().__init__("Insufficient stock for %r" % (item,))
self.item = item
- self.order_line = order_line
- self.warehouse_pk = warehouse_pk
+ self.context = context
self.code = CheckoutErrorCode.INSUFFICIENT_STOCK
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index 8e9282202f..336ca19c17 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -243,10 +243,10 @@ class OrderFulfill(BaseMutation):
)
except InsufficientStock as exc:
order_line_global_id = graphene.Node.to_global_id(
- "OrderLine", exc.order_line.pk
+ "OrderLine", exc.context["order_line"].pk
)
warehouse_global_id = graphene.Node.to_global_id(
- "Warehouse", exc.warehouse_pk
+ "Warehouse", exc.context["warehouse_pk"]
)
raise ValidationError(
{
diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.py
index 8dad4b1f68..37b668122f 100644
--- a/saleor/warehouse/management.py
+++ b/saleor/warehouse/management.py
@@ -161,14 +161,16 @@ def decrease_stock(order_line: "OrderLine", quantity: int, warehouse_pk: str):
.get(warehouse__pk=warehouse_pk)
)
except Stock.DoesNotExist:
- raise InsufficientStock(order_line.variant, order_line, warehouse_pk)
+ error_context = {"order_line": order_line, "warehouse_pk": warehouse_pk}
+ raise InsufficientStock(order_line.variant, error_context)
quantity_allocated = stock.allocations.aggregate(
quantity_allocated=Coalesce(Sum("quantity_allocated"), 0)
)["quantity_allocated"]
if stock.quantity - quantity_allocated < quantity:
- raise InsufficientStock(order_line.variant, order_line, warehouse_pk)
+ error_context = {"order_line": order_line, "warehouse_pk": warehouse_pk}
+ raise InsufficientStock(order_line.variant, error_context)
stock.quantity = F("quantity") - quantity
stock.save(update_fields=["quantity"])
diff --git a/tests/api/test_fulfillment.py b/tests/api/test_fulfillment.py
index 242c8b17cc..fe33ed51da 100644
--- a/tests/api/test_fulfillment.py
+++ b/tests/api/test_fulfillment.py
@@ -341,8 +341,12 @@ def test_order_fulfill_warehouse_with_insufficient_stock_exception(
},
}
+ error_context = {
+ "order_line": order_line,
+ "warehouse_pk": str(warehouse_no_shipping_zone.pk),
+ }
mock_create_fulfillments.side_effect = InsufficientStock(
- order_line.variant, order_line, warehouse_no_shipping_zone.pk
+ order_line.variant, error_context
)
response = staff_api_client.post_graphql(
diff --git a/tests/test_fulfullments_actions.py b/tests/test_fulfullments_actions.py
index 49a31e89ee..0a57a919e9 100644
--- a/tests/test_fulfullments_actions.py
+++ b/tests/test_fulfullments_actions.py
@@ -262,8 +262,10 @@ def test_create_fulfillments_warehouse_with_out_of_stock(
create_fulfillments(staff_user, order, fulfillment_lines_for_warehouses, True)
assert exc.value.item == order_line1.variant
- assert exc.value.order_line == order_line1
- assert exc.value.warehouse_pk == str(warehouse.pk)
+ assert exc.value.context == {
+ "order_line": order_line1,
+ "warehouse_pk": str(warehouse.pk),
+ }
order.refresh_from_db()
assert FulfillmentLine.objects.filter(fulfillment__order=order).count() == 0
@@ -302,8 +304,10 @@ def test_create_fulfillments_warehouse_without_stock(
create_fulfillments(staff_user, order, fulfillment_lines_for_warehouses, True)
assert exc.value.item == order_line1.variant
- assert exc.value.order_line == order_line1
- assert exc.value.warehouse_pk == str(warehouse_no_shipping_zone.pk)
+ assert exc.value.context == {
+ "order_line": order_line1,
+ "warehouse_pk": str(warehouse_no_shipping_zone.pk),
+ }
order.refresh_from_db()
assert FulfillmentLine.objects.filter(fulfillment__order=order).count() == 0