Add auto_resolve_category method to vendor logic
Introduced the auto_resolve_category method to streamline category resolution using Elasticsearch. This enhances category lookup efficiency and ensures seamless creation or retrieval of categories when necessary.
This commit is contained in:
parent
0e86c96ad3
commit
70cf2c23a1
1 changed files with 10 additions and 0 deletions
10
core/vendors/__init__.py
vendored
10
core/vendors/__init__.py
vendored
|
|
@ -2,6 +2,7 @@ import json
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from math import ceil
|
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
|
||||||
from payments.errors import RatesError
|
from payments.errors import RatesError
|
||||||
from payments.utils import get_rates
|
from payments.utils import get_rates
|
||||||
|
|
@ -94,6 +95,15 @@ class AbstractVendor:
|
||||||
# Default case: treat as a plain string.
|
# Default case: treat as a plain string.
|
||||||
return value, "string"
|
return value, "string"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def auto_resolve_category(category_name: str):
|
||||||
|
if category_name:
|
||||||
|
with suppress(KeyError):
|
||||||
|
uuid = process_query(category_name)["categories"][0]["uuid"]
|
||||||
|
if uuid:
|
||||||
|
return Category.objects.get(uuid=uuid)
|
||||||
|
return Category.objects.get_or_create(name=category_name, is_active=False)[0]
|
||||||
|
|
||||||
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:
|
||||||
vendor = self.get_vendor_instance()
|
vendor = self.get_vendor_instance()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue