Features: 1) Enforce currency validation when resolving price with no provided currency; 2) Update async logger initialization to use "vendors" scope;

Fixes: 1) Correct typo in error message “probider” to “provider”;

Extra: 1) Remove redundant initialization log during vendor setup; 2) Minor code cleanup in `resolve_price_with_currency` method.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-10-27 01:17:29 +03:00
parent 6d4d651320
commit 9412b0dd7a

View file

@ -34,7 +34,7 @@ from evibes.utils.misc import LogLevel
from payments.errors import RatesError from payments.errors import RatesError
from payments.utils import get_rates from payments.utils import get_rates
async_logger = get_task_logger(__name__) async_logger = get_task_logger("vendors")
logger = logging.getLogger("django") logger = logging.getLogger("django")
@ -106,7 +106,6 @@ class AbstractVendor:
self.vendor_name = vendor_name self.vendor_name = vendor_name
self.currency = currency self.currency = currency
self.blocked_attributes: list[Any] = [] self.blocked_attributes: list[Any] = []
self.log(LogLevel.INFO, f"Initializing {self}...")
def __str__(self) -> str: def __str__(self) -> str:
return self.get_vendor_instance(safe=True).name if self.get_vendor_instance() else self.vendor_name return self.get_vendor_instance(safe=True).name if self.get_vendor_instance() else self.vendor_name
@ -316,14 +315,18 @@ class AbstractVendor:
return round(price, 2) return round(price, 2)
def resolve_price_with_currency(self, price: float | int | Decimal, provider: str, currency: str = "") -> float: def resolve_price_with_currency(self, price: float | int | Decimal, provider: str, currency: str = "") -> float:
if all([not currency, not self.currency]):
raise ValueError("Currency must be provided.")
if currency == self.currency or currency == settings.CURRENCY_CODE: if currency == self.currency or currency == settings.CURRENCY_CODE:
return float(price) return float(price)
rates = get_rates(provider) rates = get_rates(provider)
rate = rates.get(currency or self.currency) if rates else 1 rate = rates.get(currency or self.currency) if rates else 1
if not rate: if not rate:
raise RatesError(f"No rate found for {currency or self.currency} in {rates} with probider {provider}...") raise RatesError(f"No rate found for {currency} in {rates} with probider {provider}...")
return float(round(price / rate, 2)) if rate else float(round(price, 2)) # type: ignore [arg-type, operator] return float(round(price / rate, 2)) if rate else float(round(price, 2)) # type: ignore [arg-type, operator]