Добавлена валидация цены
This commit is contained in:
+56
-1
@@ -12,6 +12,7 @@ DIMENSIONS_ROUND_SCALE = 1
|
||||
MIN_WEIGHT_KG = Decimal("0.01")
|
||||
MIN_DIMENSION_CM = Decimal("0.1")
|
||||
INTEGER_PRICE_QUANTIZER = Decimal("1")
|
||||
KOPECKS_IN_RUBLE = Decimal("100")
|
||||
DOC_SERVICE_MARKERS = ("документ", "document")
|
||||
|
||||
_MISSING = object()
|
||||
@@ -46,6 +47,8 @@ class ProviderPrice:
|
||||
currency: str
|
||||
delivery_days_min: int
|
||||
delivery_days_max: int
|
||||
tariff_code: int | None = None
|
||||
bypass_parcel_type_filter: bool = False
|
||||
|
||||
|
||||
def normalize_delivery_request(
|
||||
@@ -118,6 +121,48 @@ def filter_and_sort_prices(
|
||||
)
|
||||
|
||||
|
||||
def calculate_expected_payment_amount_kopecks(
|
||||
provider_price: object,
|
||||
*,
|
||||
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
) -> int | None:
|
||||
"""Return expected payment amount in kopecks for a RUB provider price."""
|
||||
|
||||
currency = _normalize_text(_get_optional_attr(provider_price, "currency")).upper()
|
||||
if currency != "RUB":
|
||||
return None
|
||||
|
||||
price = _try_to_decimal(_get_optional_attr(provider_price, "price"))
|
||||
if price is None or not price.is_finite() or price <= 0:
|
||||
return None
|
||||
|
||||
adjusted_price = _apply_price_multiplier_and_round(
|
||||
price,
|
||||
price_multiplier=_normalize_price_multiplier(price_multiplier),
|
||||
)
|
||||
if adjusted_price is None:
|
||||
return None
|
||||
|
||||
return int(adjusted_price * KOPECKS_IN_RUBLE)
|
||||
|
||||
|
||||
def is_init_payment_price_valid(
|
||||
requested_amount_kopecks: object,
|
||||
provider_price: object,
|
||||
*,
|
||||
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
) -> bool:
|
||||
expected_amount_kopecks = calculate_expected_payment_amount_kopecks(
|
||||
provider_price,
|
||||
price_multiplier=price_multiplier,
|
||||
)
|
||||
if expected_amount_kopecks is None:
|
||||
return False
|
||||
|
||||
requested_amount = _try_to_int(requested_amount_kopecks)
|
||||
return requested_amount == expected_amount_kopecks
|
||||
|
||||
|
||||
def filter_prices_by_parcel_type(
|
||||
prices: Iterable[ProviderPrice],
|
||||
*,
|
||||
@@ -132,7 +177,8 @@ def filter_prices_by_parcel_type(
|
||||
return [
|
||||
price
|
||||
for price in prices
|
||||
if _matches_parcel_type(price.service_name, normalized_parcel_type)
|
||||
if price.bypass_parcel_type_filter
|
||||
or _matches_parcel_type(price.service_name, normalized_parcel_type)
|
||||
]
|
||||
|
||||
|
||||
@@ -179,9 +225,18 @@ def _normalize_price(
|
||||
currency=currency,
|
||||
delivery_days_min=min_days,
|
||||
delivery_days_max=max_days,
|
||||
tariff_code=_try_to_int(_get_optional_attr(candidate, "tariff_code")),
|
||||
bypass_parcel_type_filter=_extract_bypass_parcel_type_filter(candidate),
|
||||
)
|
||||
|
||||
|
||||
def _extract_bypass_parcel_type_filter(candidate: object) -> bool:
|
||||
value = _get_optional_attr(candidate, "bypass_parcel_type_filter")
|
||||
if value is _MISSING or value is None:
|
||||
return False
|
||||
return bool(value)
|
||||
|
||||
|
||||
def _normalize_weight(weight: Decimal, scale: int) -> Decimal:
|
||||
rounded = _round_half_up(weight, scale)
|
||||
if rounded < MIN_WEIGHT_KG:
|
||||
|
||||
Reference in New Issue
Block a user