Refactor error handling in auto_resolve methods.

Replaced context manager suppress(KeyError) with try-except blocks to handle both KeyError and IndexError in auto_resolve_category and auto_resolve_brand methods. This improves clarity and ensures proper handling of potential IndexError cases.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-05-07 10:40:40 +03:00
parent 7f9f2d66f3
commit e1f54304b6

View file

@ -98,19 +98,27 @@ class AbstractVendor:
@staticmethod
def auto_resolve_category(category_name: str):
if category_name:
with suppress(KeyError):
try:
uuid = process_query(category_name)["categories"][0]["uuid"]
if uuid:
return Category.objects.get(uuid=uuid)
except KeyError:
pass
except IndexError:
pass
return Category.objects.get_or_create(name=category_name, is_active=False)[0]
@staticmethod
def auto_resolve_brand(brand_name: str):
if brand_name:
with suppress(KeyError):
try:
uuid = process_query(brand_name)["brands"][0]["uuid"]
if uuid:
return Brand.objects.get(uuid=uuid)
except KeyError:
pass
except IndexError:
pass
return Brand.objects.get_or_create(name=brand_name, is_active=False)[0]
def resolve_price(self, original_price: int | float, vendor: Vendor = None, category: Category = None) -> float: