Skip to content

Commit 467ff23

Browse files
committed
refactor
1 parent 4dcc9d7 commit 467ff23

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/viur/shop/skeletons/cart.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,8 @@ def unserialize_compute(self, skel: "SkeletonInstance", name: str) -> bool:
148148
if getattr(self, "_prevent_compute", False): # avoid recursion errors
149149
return False
150150

151-
if skel["shipping_status"] == ShippingStatus.USER: # should be unserialized from entity
152-
# Ensure it's still a valid shipping for the cart
153-
try:
154-
shipping_key = skel.dbEntity["shipping"]["dest"].key
155-
except (KeyError, TypeError, AttributeError):
156-
shipping_key = None
157-
if shipping_key is not None:
158-
self._prevent_compute = True
159-
try:
160-
applicable_shippings = SHOP_INSTANCE.get().shipping.get_shipping_skels_for_cart(
161-
cart_skel=skel, use_cache=True,
162-
)
163-
for shipping in applicable_shippings:
164-
if shipping["dest"]["key"] == shipping_key:
165-
return False
166-
else:
167-
logger.warning(f"Invalid shipping. {shipping_key=!r} not found in applicable_shippings")
168-
skel.setBoneValue("shipping", None)
169-
skel.setBoneValue("shipping_status", ShippingStatus.CHEAPEST)
170-
finally:
171-
self._prevent_compute = False
151+
if skel["shipping_status"] == ShippingStatus.USER and self._is_valid_user_shipping(skel):
152+
return False # should be unserialized from entity
172153

173154
if skel["is_frozen"]: # locked, unserialize the latest stored value from entity
174155
return False
@@ -190,6 +171,30 @@ def unserialize_compute(self, skel: "SkeletonInstance", name: str) -> bool:
190171

191172
return super().unserialize_compute(skel, name)
192173

174+
def _is_valid_user_shipping(self, skel: SkeletonInstance) -> bool:
175+
"""Ensure it's still a valid shipping for the cart"""
176+
try:
177+
shipping_key = skel.dbEntity["shipping"]["dest"].key
178+
except (KeyError, TypeError, AttributeError):
179+
shipping_key = None
180+
if shipping_key is None:
181+
return True
182+
self._prevent_compute = True
183+
try:
184+
applicable_shippings = SHOP_INSTANCE.get().shipping.get_shipping_skels_for_cart(
185+
cart_skel=skel, use_cache=True,
186+
)
187+
for shipping in applicable_shippings:
188+
if shipping["dest"]["key"] == shipping_key:
189+
return True
190+
else:
191+
logger.warning(f"Invalid shipping. {shipping_key=!r} not found in applicable_shippings")
192+
skel.setBoneValue("shipping", None)
193+
skel.setBoneValue("shipping_status", ShippingStatus.CHEAPEST)
194+
return False
195+
finally:
196+
self._prevent_compute = False
197+
193198

194199
class CartNodeSkel(TreeSkel):
195200
kindName = "{{viur_shop_modulename}}_cart_node"

0 commit comments

Comments
 (0)