Features: 1) Introduce VendorInactiveError for explicitly handling inactive vendors in core/vendors; 2) Update stock update process to skip inactive vendors and log warnings.

Fixes: 1) Add missing import for `VendorInactiveError` in `core/tasks.py`.

Extra: 1) Refactor exception handling for vendor stock updates for better readability.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-08-07 12:31:54 +03:00
parent dbc135aea0
commit 75709a3aa1
2 changed files with 10 additions and 7 deletions

View file

@ -13,7 +13,7 @@ from django.core.cache import cache
from core.models import Product, Promotion
from core.utils.caching import set_default_cache
from core.vendors import delete_stale
from core.vendors import delete_stale, VendorInactiveError
from evibes.settings import MEDIA_ROOT
logger = get_task_logger(__name__)
@ -43,11 +43,9 @@ def update_products_task():
for vendor_class in vendors_classes:
vendor = vendor_class()
try:
vendor_instance = vendor.get_vendor_instance()
if vendor_instance.is_active:
vendor.update_stock()
else:
continue
vendor.update_stock()
except VendorInactiveError as vie:
logger.warning(f"Skipping {vendor_class} due to error: {vie!s}")
except Exception as e:
logger.warning(f"Skipping {vendor_class} due to error: {e!s}")

View file

@ -59,6 +59,11 @@ class VendorError(Exception):
pass
class VendorInactiveError(VendorError):
pass
class AbstractVendor:
"""
Abstract class defining vendor-related operations and handling.
@ -270,7 +275,7 @@ class AbstractVendor:
vendor = Vendor.objects.get(name=self.vendor_name)
if vendor.is_active:
return vendor
raise VendorError(f"Vendor {self.vendor_name!r} is inactive...")
raise VendorInactiveError(f"Vendor {self.vendor_name!r} is inactive...")
except Vendor.DoesNotExist as dne:
raise Exception(f"No matching vendor found with name {self.vendor_name!r}...") from dne