update torob web hok
This commit is contained in:
+13
-10
@@ -17,13 +17,16 @@ TOROB_WEBHOOK_MIN_INTERVAL_SECONDS = 3.1
|
|||||||
TOROB_WEBHOOK_MAX_RETRIES = 3
|
TOROB_WEBHOOK_MAX_RETRIES = 3
|
||||||
|
|
||||||
|
|
||||||
def _shop_product_url(product: ProductModel) -> str:
|
def _shop_product_url(product: ProductModel, variant: ProductVariant | None = None) -> str:
|
||||||
domain = getattr(settings, "DOMAIN", None) or getattr(settings, "API_DOMAIN", None) or ""
|
domain = getattr(settings, "DOMAIN", None) or getattr(settings, "API_DOMAIN", None) or ""
|
||||||
if domain.startswith("http://") or domain.startswith("https://"):
|
if domain.startswith("http://") or domain.startswith("https://"):
|
||||||
base = domain.rstrip("/")
|
base = domain.rstrip("/")
|
||||||
else:
|
else:
|
||||||
base = f"https://{domain}".rstrip("/") if domain else ""
|
base = f"https://{domain}".rstrip("/") if domain else ""
|
||||||
return f"{base}/product/{product.slug}/"
|
url = f"{base}/product/{product.slug}/"
|
||||||
|
if variant is not None:
|
||||||
|
url = f"{url}?variant={variant.pk}"
|
||||||
|
return url
|
||||||
|
|
||||||
|
|
||||||
def _variant_page_unique(product: ProductModel, variant: ProductVariant) -> str:
|
def _variant_page_unique(product: ProductModel, variant: ProductVariant) -> str:
|
||||||
@@ -124,18 +127,18 @@ def send_torob_product_webhook(product_ids):
|
|||||||
if not product.slug:
|
if not product.slug:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
page_url = _shop_product_url(product)
|
|
||||||
parsed = urlparse(page_url)
|
|
||||||
if not (parsed.scheme in {"http", "https"} and parsed.netloc):
|
|
||||||
logger.warning("Skipping product %s due to invalid page_url: %s", product.pk, page_url)
|
|
||||||
continue
|
|
||||||
hosts.add(parsed.netloc.lower())
|
|
||||||
|
|
||||||
variants = list(product.variants.all())
|
variants = list(product.variants.all())
|
||||||
if not variants:
|
if not variants:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for variant in variants:
|
for variant in variants:
|
||||||
|
variant_page_url = _shop_product_url(product, variant)
|
||||||
|
parsed = urlparse(variant_page_url)
|
||||||
|
if not (parsed.scheme in {"http", "https"} and parsed.netloc):
|
||||||
|
logger.warning("Skipping variant %s due to invalid page_url: %s", variant.pk, variant_page_url)
|
||||||
|
continue
|
||||||
|
hosts.add(parsed.netloc.lower())
|
||||||
|
|
||||||
# Validate variant has images before sending to Torob
|
# Validate variant has images before sending to Torob
|
||||||
# Per spec: image_links is required, so skip variants without images
|
# Per spec: image_links is required, so skip variants without images
|
||||||
images = list(variant.images.all())
|
images = list(variant.images.all())
|
||||||
@@ -148,7 +151,7 @@ def send_torob_product_webhook(product_ids):
|
|||||||
|
|
||||||
items.append(
|
items.append(
|
||||||
{
|
{
|
||||||
"page_url": page_url,
|
"page_url": variant_page_url,
|
||||||
"page_unique": _variant_page_unique(product, variant),
|
"page_unique": _variant_page_unique(product, variant),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user