Skip to content

Commit c667110

Browse files
committed
chore: Some refactorings, resolve issues and update to newer viur-core syntax
1 parent c77575d commit c667110

File tree

6 files changed

+20
-34
lines changed

6 files changed

+20
-34
lines changed

src/viur/shop/modules/abstract.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ def session(self) -> dict:
5858
if session is None:
5959
logger.warning(f"Session is None!")
6060
return None
61-
# TODO: custom session name
62-
# TODO: Implement .setdefault() in viur.core.Session
63-
if "shop" not in session:
64-
session["shop"] = {}
65-
session_shop = session["shop"]
61+
session_shop = session.setdefault("shop", {})
6662
if self.moduleName not in session_shop:
6763
session_shop[self.moduleName] = {}
6864
session.markChanged()

src/viur/shop/modules/cart.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ def listRootNodes(self, *args, **kwargs) -> t.Any:
138138
139139
:returns: The rendered representation of the available root-nodes.
140140
"""
141-
if utils.string.is_prefix(self.render.kind, "json"):
142-
# TODO: add in viur-core
143-
current.request.get().response.headers["Content-Type"] = "application/json"
144141
return self.render.listRootNodes([
145142
self.render.renderSkelValues(skel)
146143
for skel in self.getAvailableRootNodes(*args, **kwargs)

src/viur/shop/modules/order.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,10 @@ def _order_set_values(
241241
raise e.InvalidArgumentException("customer_key")
242242
skel.setBoneValue("customer", customer_key)
243243

244-
# TODO(discussion): Do we really want to set this by the frontend?
245-
# Or what are the pre conditions?
246244
if state_ordered != SENTINEL or state_paid != SENTINEL or state_rts != SENTINEL:
247245
# any of these values should be set
248246
if not self.canEdit(skel):
249-
raise core_errors.Forbidden("You are not allowed to change a sate")
247+
raise core_errors.Forbidden("You are not allowed to change a state")
250248
logger.debug("Can change states")
251249
if state_ordered is not SENTINEL:
252250
skel["state_ordered"] = state_ordered

src/viur/shop/shop.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929

3030

3131
class Shop(InstancedModule, Module):
32-
@exposed
33-
def hello(self):
34-
# logger.debug(f"{SHOP_INSTANCE.get().render=}")
35-
# logger.debug(f"{SHOP_INSTANCE_VI.get().render=}")
36-
return f"Welcome to the {self.name} Shop!"
37-
3832
def __init__(
3933
self,
4034
*,

src/viur/shop/skeletons/article.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,15 @@ def shop_is_low_price(self) -> BooleanBone:
100100
def shop_price_(self) -> Price:
101101
return Price.get_or_create(self)
102102

103-
shop_price = RawBone( # FIXME: JsonBone doesn't work (https://github.com/viur-framework/viur-core/issues/1092)
103+
shop_price = JsonBone(
104104
compute=Compute(lambda skel: skel.shop_price_.to_dict(), ComputeInterval(ComputeMethod.Always))
105105
)
106-
shop_price.type = JsonBone.type
107-
shop_shipping = RawBone( # FIXME: JsonBone doesn't work (https://github.com/viur-framework/viur-core/issues/1092)
106+
107+
shop_shipping = JsonBone(
108108
compute=Compute(
109109
lambda skel: make_json_dumpable(SHOP_INSTANCE.get().shipping.choose_shipping_skel_for_article(skel)),
110110
ComputeInterval(ComputeMethod.Always)),
111111
)
112-
shop_shipping.type = JsonBone.type
113112
"""Calculated, cheapest shipping for this article"""
114113

115114
@classmethod

src/viur/shop/types/price.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import json
33
import typing as t # noqa
44

5+
from viur import toolkit
56
from viur.core import current, db, utils
67
from viur.core.skeleton import SkeletonInstance
7-
8-
from viur import toolkit
98
from .enums import ApplicationDomain, ConditionOperator, DiscountType
109
from ..globals import SHOP_INSTANCE, SHOP_LOGGER
1110
from ..types import ConfigurationError
@@ -15,10 +14,13 @@
1514

1615
logger = SHOP_LOGGER.getChild(__name__)
1716

18-
1917
# TODO: Use decimal package instead of floats?
2018
# -> decimal mode in NumericBone?
2119

20+
PRICE_PRECISION: t.Final[int] = 2
21+
"""Precision, how many digits are used to round prices"""
22+
23+
2224
class Price:
2325
cart_discounts: list[SkeletonInstance] = []
2426
article_discount: SkeletonInstance = None
@@ -60,30 +62,30 @@ def retail(self) -> float:
6062

6163
@property
6264
def retail_net(self) -> float:
63-
return toolkit.round_decimal(self.gross_to_net(self.retail, self.vat_rate_percentage), 2)
65+
return toolkit.round_decimal(self.gross_to_net(self.retail, self.vat_rate_percentage), PRICE_PRECISION)
6466

6567
@property
6668
def recommended(self) -> float:
6769
return self.article_skel["shop_price_recommended"]
6870

6971
@property
7072
def recommended_net(self) -> float:
71-
return toolkit.round_decimal(self.gross_to_net(self.recommended, self.vat_rate_percentage), 2)
73+
return toolkit.round_decimal(self.gross_to_net(self.recommended, self.vat_rate_percentage), PRICE_PRECISION)
7274

7375
@property
7476
def saved(self) -> float:
7577
if self.retail is None or self.current is None:
7678
return 0
77-
return toolkit.round_decimal(self.retail - self.current, 2)
79+
return toolkit.round_decimal(self.retail - self.current, PRICE_PRECISION)
7880

7981
@property
8082
def saved_net(self) -> float:
81-
return toolkit.round_decimal(self.gross_to_net(self.saved, self.vat_rate_percentage), 2)
83+
return toolkit.round_decimal(self.gross_to_net(self.saved, self.vat_rate_percentage), PRICE_PRECISION)
8284

8385
@property
8486
def saved_percentage(self) -> float:
8587
try:
86-
return toolkit.round_decimal(self.saved / self.current, 2 or 4) # TODO
88+
return toolkit.round_decimal(self.saved / self.current, PRICE_PRECISION)
8789
except (ZeroDivisionError, TypeError): # One value is None
8890
return 0.0
8991

@@ -92,21 +94,21 @@ def saved_percentage(self) -> float:
9294
def current(self) -> float:
9395
if (not self.is_in_cart or not self.cart_discounts) and self.article_discount:
9496
# only the article_discount is applicable
95-
return toolkit.round_decimal(self.apply_discount(self.article_discount, self.retail), 2)
97+
return toolkit.round_decimal(self.apply_discount(self.article_discount, self.retail), PRICE_PRECISION)
9698
if self.is_in_cart and self.cart_discounts:
9799
# TODO: if self.article_discount:
98100
best_price, best_discounts = self.choose_best_discount_set()
99-
return toolkit.round_decimal(best_price, 2)
101+
return toolkit.round_decimal(best_price, PRICE_PRECISION)
100102
return self.retail
101103

102104
@property
103105
def current_net(self) -> float:
104-
return toolkit.round_decimal(self.gross_to_net(self.current, self.vat_rate_percentage), 2)
106+
return toolkit.round_decimal(self.gross_to_net(self.current, self.vat_rate_percentage), PRICE_PRECISION)
105107

106108
def shop_current_discount(self, article_skel: SkeletonInstance) -> None | tuple[float, "SkeletonInstance"]:
107109
"""Best permanent discount campaign for article"""
108110
best_discount = None
109-
article_price = self.retail or 0.0 # FIXME: how to handle None prices?
111+
article_price = self.retail or 0.0 # FIXME(discuss): how to handle None prices?
110112
if not article_price:
111113
return None
112114
discount_module: "Discount" = SHOP_INSTANCE.get().discount
@@ -186,7 +188,7 @@ def vat_rate_percentage(self) -> float:
186188
def vat_included(self) -> float:
187189
"""Calculate the included vat value based on current price and vat rate"""
188190
try:
189-
return toolkit.round_decimal(self.gross_to_vat(self.current, self.vat_rate_percentage), 2)
191+
return toolkit.round_decimal(self.gross_to_vat(self.current, self.vat_rate_percentage), PRICE_PRECISION)
190192
except TypeError: # One value is None
191193
return 0.0
192194

0 commit comments

Comments
 (0)