From 75709a3aa166915d555a719c4a8c695bffafaa37 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Thu, 7 Aug 2025 12:31:54 +0300 Subject: [PATCH] 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. --- core/tasks.py | 10 ++++------ core/vendors/__init__.py | 7 ++++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/core/tasks.py b/core/tasks.py index cdd2cc96..510038ef 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -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}") diff --git a/core/vendors/__init__.py b/core/vendors/__init__.py index b0868fa0..4ee378f8 100644 --- a/core/vendors/__init__.py +++ b/core/vendors/__init__.py @@ -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