Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Rework validating the shipping zone on the fly
Pacu2df12675c587a
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex a4d4729deb..88996d701c 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -25,7 +25,7 @@ from ..discount.utils import (get_products_voucher_discount, get_shipping_voucher_discount,get_value_voucher_discount, increase_voucher_usage)from ..order.models import Order-from ..shipping import ShippingMethodType+from ..shipping.models import ShippingMethodfrom .forms import (AddressChoiceForm, AnonymousUserBillingForm, AnonymousUserShippingForm,BillingAddressChoiceForm)@@ -761,42 +761,16 @@ def get_taxes_for_cart(cart, default_taxes):return default_taxes-def value_in_range(minimum, maximum, value):- """Check if value is in the certain range.- If the maximum in None, then there's no upper limit."""- if maximum is None:- return value >= minimum- return value >= minimum and value <= maximum---def shipping_method_applicable(price, weight, method):- """Checks if all shipping method requirements are fullfilled- (eg. minimum order value or maximum order weight).- """- if method.type == ShippingMethodType.PRICE_BASED:- return value_in_range(- minimum=method.minimum_order_price,- maximum=method.maximum_order_price, value=price)- return value_in_range(- minimum=method.minimum_order_weight,- maximum=method.maximum_order_weight, value=weight)--def is_valid_shipping_method(cart, taxes, discounts):"""Check if shipping method is valid and remove (if not)."""if not cart.shipping_method:return False- shipping_outside_the_shipping_zone = (- cart.shipping_address.country.code not in- cart.shipping_method.shipping_zone.countries)- if shipping_outside_the_shipping_zone:- clear_shipping_method(cart)- return False- is_valid_shipping = shipping_method_applicable(+ valid_methods = ShippingMethod.objects.applicable_shipping_methods(price=cart.get_subtotal(discounts, taxes).gross,- weight=cart.get_total_weight(), method=cart.shipping_method)- if not is_valid_shipping:+ weight=cart.get_total_weight(),+ country_code=cart.shipping_address.country.code)+ if cart.shipping_method not in valid_methods:clear_shipping_method(cart)return Falsereturn Truediff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex 14892991ec..499af11a47 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -18,7 +18,7 @@ from saleor.checkout.utils import (get_prices_of_products_in_discounted_categories, get_taxes_for_cart,get_voucher_discount_for_cart, get_voucher_for_cart,is_valid_shipping_method, recalculate_cart_discount,- remove_voucher_from_cart, shipping_method_applicable, value_in_range)+ remove_voucher_from_cart)from saleor.core.exceptions import InsufficientStockfrom saleor.core.utils.taxes import (ZERO_MONEY, ZERO_TAXED_MONEY, get_taxes_for_country)@@ -45,57 +45,6 @@ def test_country_form_country_choices():assert form.fields['country'].choices == expected_choices- 'minimum, maximum, value, result',- (- (10, None, 1500, True),- (10, 20, 15, True),- (10, 20, 20, True),- (10, 20, 10, True),- (10, 20, 9, False),- (10, 21, 9, False)))-def test_value_in_range(minimum, maximum, value, result):- assert result == value_in_range(minimum, maximum, value)--- 'weight, minimum_order_weight, maximum_order_weight, result',- (- (Weight(kg=15), Weight(kg=10), Weight(kg=20), True),- (Weight(kg=15), Weight(kg=15), Weight(kg=20), True),- (Weight(kg=15), Weight(kg=10), Weight(kg=15), True),- (Weight(kg=15), Weight(kg=10), None, True),- (Weight(kg=26), Weight(kg=10), Weight(kg=25), False),- (Weight(kg=9), Weight(kg=10), Weight(kg=15), False)))-def test_weight_shipping_method_applicable(- weight, minimum_order_weight, maximum_order_weight, result):- shipping_method = Mock(- type=ShippingMethodType.WEIGHT_BASED,- minimum_order_weight=minimum_order_weight,- maximum_order_weight=maximum_order_weight)- assert result == shipping_method_applicable(- Money(0, 'USD'), weight, shipping_method)--- 'price, minimum_order_price, maximum_order_price, result',- (- (Money(15, 'USD'), Money(10, 'USD'), Money(20, 'USD'), True),- (Money(15, 'USD'), Money(15, 'USD'), Money(20, 'USD'), True),- (Money(15, 'USD'), Money(10, 'USD'), Money(15, 'USD'), True),- (Money(15, 'USD'), Money(10, 'USD'), None, True),- (Money(26, 'USD'), Money(10, 'USD'), Money(25, 'USD'), False),- (Money(9, 'USD'), Money(10, 'USD'), Money(15, 'USD'), False)))-def test_price_shipping_method_applicable(- price, minimum_order_price, maximum_order_price, result):- shipping_method = Mock(- type=ShippingMethodType.PRICE_BASED,- minimum_order_price=minimum_order_price,- maximum_order_price=maximum_order_price)- assert result == shipping_method_applicable(- price, Weight(kg=0), shipping_method)--def test_is_valid_shipping_method_no_shipping_method(vatlayer):cart = Mock(shipping_method=False)assert not is_valid_shipping_method(cart, vatlayer, None)