From 5d007c07a230b9257714c48201faa6bba8e8eeb5 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Wed, 7 May 2025 02:40:23 +0300 Subject: [PATCH] Add auto_resolve_brand method to handle brand resolution Introduced a new static method, auto_resolve_brand, to simplify and automate the resolution or creation of Brand objects based on a given brand name. Updated imports in core/vendors/__init__.py to include the Brand model. This enhances the system's ability to process brands effectively. --- core/vendors/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/vendors/__init__.py b/core/vendors/__init__.py index 317fb5fb..3f30b18d 100644 --- a/core/vendors/__init__.py +++ b/core/vendors/__init__.py @@ -3,7 +3,7 @@ from contextlib import suppress from math import ceil from core.elasticsearch import process_query -from core.models import AttributeValue, Category, Product, Stock, Vendor +from core.models import AttributeValue, Category, Product, Stock, Vendor, Brand from payments.errors import RatesError from payments.utils import get_rates @@ -104,6 +104,15 @@ class AbstractVendor: return Category.objects.get(uuid=uuid) 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): + uuid = process_query(brand_name)["brands"][0]["uuid"] + if uuid: + return Brand.objects.get(uuid=uuid) + 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: if not vendor: vendor = self.get_vendor_instance()