013 add price multiplier

This commit is contained in:
Раис Юсупалиев
2026-03-09 16:14:49 +03:00
parent da301d4bc4
commit 2bd884c8d5
20 changed files with 312 additions and 25 deletions
+53 -6
View File
@@ -6,10 +6,12 @@ from decimal import ROUND_HALF_UP, Decimal
from typing import Protocol
DEFAULT_WEIGHT_ROUND_SCALE = 2
DEFAULT_PROVIDER_PRICE_MULTIPLIER = Decimal("1")
MAX_ROUND_SCALE = 6
DIMENSIONS_ROUND_SCALE = 1
MIN_WEIGHT_KG = Decimal("0.01")
MIN_DIMENSION_CM = Decimal("0.1")
INTEGER_PRICE_QUANTIZER = Decimal("1")
_MISSING = object()
@@ -77,12 +79,20 @@ def normalize_delivery_request(
)
def filter_valid_prices(prices: Iterable[object]) -> list[ProviderPrice]:
def filter_valid_prices(
prices: Iterable[object],
*,
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
) -> list[ProviderPrice]:
"""Normalize price records and keep only domain-valid entries."""
normalized_multiplier = _normalize_price_multiplier(price_multiplier)
valid_prices: list[ProviderPrice] = []
for price_entry in prices:
normalized_price = _normalize_price(price_entry)
normalized_price = _normalize_price(
price_entry,
price_multiplier=normalized_multiplier,
)
if normalized_price is not None:
valid_prices.append(normalized_price)
return valid_prices
@@ -94,13 +104,23 @@ def sort_prices_by_price(prices: Iterable[ProviderPrice]) -> list[ProviderPrice]
return sorted(prices, key=lambda price_entry: price_entry.price)
def filter_and_sort_prices(prices: Iterable[object]) -> list[ProviderPrice]:
def filter_and_sort_prices(
prices: Iterable[object],
*,
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
) -> list[ProviderPrice]:
"""Apply domain filtering and return prices ordered by value."""
return sort_prices_by_price(filter_valid_prices(prices))
return sort_prices_by_price(
filter_valid_prices(prices, price_multiplier=price_multiplier)
)
def _normalize_price(candidate: object) -> ProviderPrice | None:
def _normalize_price(
candidate: object,
*,
price_multiplier: Decimal,
) -> ProviderPrice | None:
provider = _normalize_text(_get_optional_attr(candidate, "provider"))
service_name = _normalize_text(_get_optional_attr(candidate, "service_name"))
currency = _normalize_text(_get_optional_attr(candidate, "currency")).upper()
@@ -117,6 +137,12 @@ def _normalize_price(candidate: object) -> ProviderPrice | None:
price = _try_to_decimal(price_raw)
if price is None or not price.is_finite() or price <= 0:
return None
adjusted_price = _apply_price_multiplier_and_round(
price,
price_multiplier=price_multiplier,
)
if adjusted_price is None:
return None
min_days = _try_to_int(min_days_raw)
max_days = _try_to_int(max_days_raw)
@@ -129,7 +155,7 @@ def _normalize_price(candidate: object) -> ProviderPrice | None:
return ProviderPrice(
provider=provider,
service_name=service_name,
price=price,
price=adjusted_price,
currency=currency,
delivery_days_min=min_days,
delivery_days_max=max_days,
@@ -192,6 +218,27 @@ def _round_half_up(value: Decimal, scale: int) -> Decimal:
return value.quantize(quantizer, rounding=ROUND_HALF_UP)
def _normalize_price_multiplier(value: Decimal) -> Decimal:
multiplier = _try_to_decimal(value)
if multiplier is None or not multiplier.is_finite() or multiplier <= 0:
raise ValueError(f"Price multiplier must be a positive finite Decimal: {value!r}")
return multiplier
def _apply_price_multiplier_and_round(
price: Decimal,
*,
price_multiplier: Decimal,
) -> Decimal | None:
adjusted_price = (price * price_multiplier).quantize(
INTEGER_PRICE_QUANTIZER,
rounding=ROUND_HALF_UP,
)
if not adjusted_price.is_finite() or adjusted_price <= 0:
return None
return adjusted_price
def _to_decimal(value: object) -> Decimal:
converted = _try_to_decimal(value)
if converted is None: