Skip to content

Commit

Permalink
feat: Implement discount_remove (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: Sven Eberth <[email protected]>
  • Loading branch information
ArneGudermann and sveneberth authored Oct 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a6d85d6 commit ef8fd5c
Showing 5 changed files with 73 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/viur/shop/modules/api.py
Original file line number Diff line number Diff line change
@@ -367,10 +367,10 @@ def discount_add(
def discount_remove(
self,
*,

discount_key: str | db.Key,
):
...
discount_key = self._normalize_external_key(discount_key, "discount_key")
return JsonResponse(self.shop.discount.remove(discount_key))

@exposed
def shipping_list(
2 changes: 1 addition & 1 deletion src/viur/shop/modules/cart.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pprint
import typing as t # noqa

import viur.shop.types.exceptions as e
@@ -499,7 +500,6 @@ def get_discount_for_leaf(
raise InvalidStateError(f"{pk=} doesn't exist!")
if discount := skel["discount"]:
discounts.append(discount["dest"])
logger.debug(f"{discounts = }")
return discounts

def add_new_parent(self, leaf_skel, **kwargs):
65 changes: 65 additions & 0 deletions src/viur/shop/modules/discount.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
from viur.core.prototypes import List
from viur.core.skeleton import SkeletonInstance
from viur.shop.types import *
from viur.shop.skeletons.discount_condition import DiscountConditionSkel
from .abstract import ShopModuleAbstract
from ..globals import SHOP_LOGGER
from ..types.dc_scope import DiscountValidator
@@ -207,3 +208,67 @@ def current_automatically_discounts(self) -> list[SkeletonInstance]:
discounts.append(skel)
logger.debug(f'current {discounts=}')
return discounts

def remove(
self,
discount_key: db.Key,
) -> t.Any:
if not isinstance(discount_key, db.Key):
raise TypeError(f"discount_key must be an instance of db.Key")
cart_key = self.shop.cart.current_session_cart_key # TODO: parameter?

discount_skel = self.viewSkel()

if not discount_skel.fromDB(discount_key):
raise errors.NotFound
try:
# Todo what we do when we have more than more condition
application_domain = discount_skel["condition"][0]["dest"]["application_domain"]
except KeyError:
raise InvalidStateError("application_domain not set")

if discount_skel["discount_type"] == DiscountType.FREE_ARTICLE:
for cart_skel in self.shop.cart.get_children(parent_cart_key=cart_key):
if cart_skel["discount"] and cart_skel["discount"]["dest"]["key"] == discount_skel["key"]:
break
else:
raise errors.NotFound
self.shop.cart.cart_remove(
cart_key=cart_skel["key"]
)

return { # TODO: what should be returned?
"discount_skel": discount_skel}

elif application_domain == ApplicationDomain.BASKET:
self.shop.cart.cart_update(
cart_key=cart_key,
discount_key=None
)
return { # TODO: what should be returned?
"discount_skel": discount_skel,
}

elif application_domain == ApplicationDomain.ARTICLE:
node_skels = (
self.shop.cart.viewSkel("node").all()
.filter("parentrepo =", cart_key)
.filter("discount.dest.__key__ =", discount_key)
.fetch(100)
)

logger.debug(f"<{len(node_skels)}>{node_skels=}")
for node_skel in node_skels:
# TODO: remove node, if no custom name, shipping, etc. is set? remove_parent flag?
self.shop.cart.cart_update(
cart_key=node_skel["key"],
discount_key=None,
)
if not node_skels:
raise errors.NotFound("Discount not used by any cart")
return { # TODO: what should be returned?
"node_skels": node_skels,
"discount_skel": discount_skel,
}

raise errors.NotImplemented(f'{discount_skel["discount_type"]=} is not implemented yet :(')
1 change: 0 additions & 1 deletion src/viur/shop/skeletons/article.py
Original file line number Diff line number Diff line change
@@ -92,7 +92,6 @@ def shop_price_(self) -> Price:
compute=Compute(lambda skel: skel.shop_price_.to_dict(), ComputeInterval(ComputeMethod.Always))
)
shop_price.type = JsonBone.type

shop_shipping = RawBone( # FIXME: JsonBone doesn't work (https://github.com/viur-framework/viur-core/issues/1092)
compute=Compute(
lambda skel: make_json_dumpable(SHOP_INSTANCE.get().shipping.choose_shipping_skel_for_article(skel)),
6 changes: 5 additions & 1 deletion src/viur/shop/skeletons/cart.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
from viur.core.skeleton import SkeletonInstance
from viur.shop.types import *
from ..globals import SHOP_INSTANCE, SHOP_LOGGER
from ..types.response import make_json_dumpable

logger = SHOP_LOGGER.getChild(__name__)

@@ -294,11 +295,14 @@ def price_(self) -> Price:

shipping = RawBone( # FIXME: JsonBone doesn't work (https://github.com/viur-framework/viur-core/issues/1092)
compute=Compute(
lambda skel: SHOP_INSTANCE.get().shipping.choose_shipping_skel_for_article(skel.article_skel_full),
lambda skel: make_json_dumpable(
SHOP_INSTANCE.get().shipping.choose_shipping_skel_for_article(skel.article_skel_full)
),
ComputeInterval(ComputeMethod.Always)),
)
shipping.type = JsonBone.type


@classmethod
def toDB(cls, skelValues: SkeletonInstance, update_relations: bool = True, **kwargs) -> db.Key:
return super().toDB(skelValues, update_relations, **kwargs)

0 comments on commit ef8fd5c

Please sign in to comment.