2020import typing as t # noqa
2121
2222from viur import toolkit
23- from viur .core import current , db , utils
24- from viur .core .skeleton import RefSkel , RelSkel , SkeletonInstance
23+ from viur .core import conf , current , db , utils
24+ from viur .core .skeleton import SkeletonInstance
25+
2526from .enums import ApplicationDomain , ConditionOperator , DiscountType
2627from .exceptions import InvalidStateError
2728from ..globals import SHOP_INSTANCE , SHOP_LOGGER
2829from ..types import ConfigurationError , DiscountValidationContext
29- from viur .core .skeleton .utils import is_skeletoninstance_of
3030
3131if t .TYPE_CHECKING :
3232 from ..modules import Discount
3333
3434logger = SHOP_LOGGER .getChild (__name__ )
3535
36+ if conf .version >= (3 , 8 , 15 ): # TODO: 3,8,16
37+ from viur .core .skeleton .utils import is_skeletoninstance_of
38+ else :
39+ def is_skeletoninstance_of (
40+ obj : t .Any ,
41+ skel_cls : type ["Skeleton" ],
42+ * ,
43+ accept_ref_skel : bool = True ,
44+ ) -> bool :
45+ """
46+ Checks whether an object is an SkeletonInstance that belongs to a specific Skeleton class.
47+
48+ :param obj: The object to check.
49+ :param skel_cls: The skeleton class that will be checked against ``obj``.
50+ :param accept_ref_skel: If True, ``obj`` can also be just a RefSkelFor``skel_cls``.
51+ If False, no ``RefSkel`` is accepted.
52+ """
53+ from . import RefSkel , Skeleton , SkeletonInstance
54+
55+ if not issubclass (skel_cls , Skeleton ):
56+ raise TypeError (f"{ skel_cls = } is not a Skeleton." )
57+
58+ if not isinstance (obj , SkeletonInstance ):
59+ return False
60+ if issubclass (obj .skeletonCls , skel_cls ):
61+ return True
62+ return False
63+
3664# TODO: Use decimal package instead of floats?
3765# -> decimal mode in NumericBone?
3866
@@ -72,8 +100,6 @@ def __init__(self, src_object: SkeletonInstance):
72100 # logger.debug(f"Creating new price object based on {src_object=}")
73101 shop = SHOP_INSTANCE .get ()
74102 if is_skeletoninstance_of (src_object , shop .cart .leafSkelCls ):
75- # if isinstance(src_object, SkeletonInstance) and (issubclass(src_object.skeletonCls, shop.cart.leafSkelCls)
76- # or issubclass(src_object.skeletonCls, RefSkel) and issubclass( src_object.skeletonCls.skeletonCls,shop.cart.leafSkelCls)):
77103 self .is_in_cart = True
78104 self .cart_leaf = src_object
79105 self .article_skel = toolkit .without_render_preparation (src_object .article_skel_full )
@@ -84,13 +110,10 @@ def __init__(self, src_object: SkeletonInstance):
84110 self .cart_discounts = []
85111 self .cart_discounts = [toolkit .get_full_skel_from_ref_skel (d ) for d in self .cart_discounts ]
86112 elif is_skeletoninstance_of (src_object , shop .article_skel ):
87- # elif isinstance(src_object, SkeletonInstance) and (issubclass(src_object.skeletonCls, shop.article_skel)
88- # or issubclass(src_object.skeletonCls, RefSkel) and issubclass( src_object.skeletonCls.skeletonCls,shop.article_skel)):
89113 self .is_in_cart = False
90114 self .article_skel = toolkit .without_render_preparation (src_object )
91115 else :
92116 logger .error (f"Unsupported type { type (src_object )= } | { src_object .skeletonCls = } " )
93- logger .error (f"Unsupported type { type (src_object )= } | { type (src_object .skeletonCls )= } " )
94117 raise TypeError (f"Unsupported type { type (src_object )} " )
95118
96119 # logger.debug(f"{self.article_skel = }")
0 commit comments