Features: 1) Add logging on initialization for vendor instances; 2) Introduce safe parameter to get_vendor_instance method to handle inactive or nonexistent vendors gracefully;
Fixes: 1) Modify `__str__` method to use `get_vendor_instance` with `safe=True` for improved error handling; Extra: 1) Minor code structure improvements in `get_vendor_instance` method.
This commit is contained in:
parent
0c990291a4
commit
6d4d651320
1 changed files with 7 additions and 2 deletions
9
core/vendors/__init__.py
vendored
9
core/vendors/__init__.py
vendored
|
|
@ -106,9 +106,10 @@ 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().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
|
||||||
|
|
||||||
def log(self, level: LogLevel, message: str) -> None:
|
def log(self, level: LogLevel, message: str) -> None:
|
||||||
is_celery_runtime = False
|
is_celery_runtime = False
|
||||||
|
|
@ -359,13 +360,17 @@ class AbstractVendor:
|
||||||
|
|
||||||
return float(psychological)
|
return float(psychological)
|
||||||
|
|
||||||
def get_vendor_instance(self) -> Vendor | None:
|
def get_vendor_instance(self, safe: bool = False) -> Vendor | None:
|
||||||
try:
|
try:
|
||||||
vendor = Vendor.objects.get(name=self.vendor_name)
|
vendor = Vendor.objects.get(name=self.vendor_name)
|
||||||
if vendor.is_active:
|
if vendor.is_active:
|
||||||
return vendor
|
return vendor
|
||||||
|
if safe:
|
||||||
|
return None
|
||||||
raise VendorInactiveError(f"Vendor {self.vendor_name!r} is inactive...")
|
raise VendorInactiveError(f"Vendor {self.vendor_name!r} is inactive...")
|
||||||
except Vendor.DoesNotExist as dne:
|
except Vendor.DoesNotExist as dne:
|
||||||
|
if safe:
|
||||||
|
return None
|
||||||
raise Exception(f"No matching vendor found with name {self.vendor_name!r}...") from dne
|
raise Exception(f"No matching vendor found with name {self.vendor_name!r}...") from dne
|
||||||
|
|
||||||
def get_products(self) -> None:
|
def get_products(self) -> None:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue