Refactor category and brand resolution logic.
Improve handling of duplicate and inactive objects during category and brand resolution. Ensures only the most relevant entry is retained while unused duplicates are deleted. Fixes potential issues with object retrieval and creation logic.
This commit is contained in:
parent
0ed8a8d48e
commit
808e2aae25
1 changed files with 18 additions and 2 deletions
20
core/vendors/__init__.py
vendored
20
core/vendors/__init__.py
vendored
|
|
@ -106,7 +106,15 @@ class AbstractVendor:
|
||||||
pass
|
pass
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
return Category.objects.get_or_create(name=category_name, is_active=False)[0]
|
categories = Category.objects.filter(name=category_name)
|
||||||
|
if not categories.exists():
|
||||||
|
return Category.objects.create(name=category_name, is_active=False)
|
||||||
|
elif categories.count() > 1:
|
||||||
|
categories = categories.filter(is_active=True)
|
||||||
|
chosen = categories.first()
|
||||||
|
categories.exlude(uuid=chosen.uuid)
|
||||||
|
categories.delete()
|
||||||
|
return chosen
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def auto_resolve_brand(brand_name: str):
|
def auto_resolve_brand(brand_name: str):
|
||||||
|
|
@ -119,7 +127,15 @@ class AbstractVendor:
|
||||||
pass
|
pass
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
return Brand.objects.get_or_create(name=brand_name, is_active=False)[0]
|
brands = Brand.objects.filter(name=brand_name)
|
||||||
|
if not brands.exists():
|
||||||
|
return Brand.objects.create(name=brand_name, is_active=False)
|
||||||
|
elif brands.count() > 1:
|
||||||
|
brands = brands.filter(is_active=True)
|
||||||
|
chosen = brands.first()
|
||||||
|
brands.exlude(uuid=chosen.uuid)
|
||||||
|
brands.delete()
|
||||||
|
return chosen
|
||||||
|
|
||||||
def resolve_price(self, original_price: int | float, vendor: Vendor = None, category: Category = None) -> float:
|
def resolve_price(self, original_price: int | float, vendor: Vendor = None, category: Category = None) -> float:
|
||||||
if not vendor:
|
if not vendor:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue