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:
Egor Pavlovich Gorbunov 2025-05-07 19:55:36 +03:00
parent 0ed8a8d48e
commit 808e2aae25

View file

@ -106,7 +106,15 @@ class AbstractVendor:
pass
except IndexError:
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
def auto_resolve_brand(brand_name: str):
@ -119,7 +127,15 @@ class AbstractVendor:
pass
except IndexError:
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:
if not vendor: