feat(recommerce): флаг special для акционных товаров
- Исправлен формат флага: special=1/0 вместо is_special="true"/"false" - Добавлен тестовый скрипт test_is_special.py для отладки API Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -50,10 +50,12 @@ def to_api_product(
|
|||||||
if category_sku:
|
if category_sku:
|
||||||
data['parent_category_sku'] = category_sku
|
data['parent_category_sku'] = category_sku
|
||||||
|
|
||||||
|
# Вычисляем has_discount до блока price (нужно для is_special)
|
||||||
|
has_discount = product.sale_price and product.price and product.sale_price < product.price
|
||||||
|
|
||||||
if 'price' in fields:
|
if 'price' in fields:
|
||||||
# Recommerce ожидает price[amount] и price[currency] в form-data формате
|
# Recommerce ожидает price[amount] и price[currency] в form-data формате
|
||||||
# Значения передаём как строки (так работает в проверенном скрипте)
|
# Значения передаём как строки (так работает в проверенном скрипте)
|
||||||
has_discount = product.sale_price and product.price and product.sale_price < product.price
|
|
||||||
|
|
||||||
if has_discount:
|
if has_discount:
|
||||||
# Есть скидка: текущая цена = sale_price, старая = price
|
# Есть скидка: текущая цена = sale_price, старая = price
|
||||||
@@ -93,6 +95,8 @@ def to_api_product(
|
|||||||
data['is_new'] = "true" if product.is_new else "false"
|
data['is_new'] = "true" if product.is_new else "false"
|
||||||
if hasattr(product, 'is_popular'):
|
if hasattr(product, 'is_popular'):
|
||||||
data['is_popular'] = "true" if product.is_popular else "false"
|
data['is_popular'] = "true" if product.is_popular else "false"
|
||||||
|
# special - автоматически при наличии скидки (формат: 1/0, не is_special)
|
||||||
|
data['special'] = 1 if has_discount else 0
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|||||||
148
test_is_special.py
Normal file
148
test_is_special.py
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
"""
|
||||||
|
Тестовый скрипт для отладки флага is_special в Recommerce API
|
||||||
|
Без Django - напрямую через requests
|
||||||
|
"""
|
||||||
|
import requests
|
||||||
|
|
||||||
|
# === НАСТРОЙКИ - ЗАПОЛНИ ===
|
||||||
|
STORE_URL = "https://mixflowers.by" # Замени на свой URL
|
||||||
|
API_TOKEN = "baac4380c190c4c3fed7a89977fd6155b846c7e8" # Замени на свой токен
|
||||||
|
# ===========================
|
||||||
|
|
||||||
|
SKU = "re-3560"
|
||||||
|
|
||||||
|
def get_headers():
|
||||||
|
return {
|
||||||
|
'x-auth-token': API_TOKEN,
|
||||||
|
'Accept': 'application/json',
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_product():
|
||||||
|
"""Получить товар и показать текущие флаги"""
|
||||||
|
print(f"\n=== GET товар {SKU} ===")
|
||||||
|
url = f"{STORE_URL}/api/v1/catalog/products/{SKU}"
|
||||||
|
try:
|
||||||
|
resp = requests.get(url, headers=get_headers(), timeout=15)
|
||||||
|
print(f"Status: {resp.status_code}")
|
||||||
|
if resp.status_code == 200:
|
||||||
|
result = resp.json()
|
||||||
|
# Показываем интересующие поля
|
||||||
|
if 'marks' in result:
|
||||||
|
print(f" marks: {result['marks']}")
|
||||||
|
if 'is_special' in result:
|
||||||
|
print(f" is_special: {result['is_special']}")
|
||||||
|
if 'price' in result:
|
||||||
|
print(f" price: {result['price']}")
|
||||||
|
if 'price_old' in result:
|
||||||
|
print(f" price_old: {result['price_old']}")
|
||||||
|
print(f"\nПолный ответ: {result}")
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
print(f"Ошибка: {resp.text}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Ошибка: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def update_product(data):
|
||||||
|
"""Обновить товар"""
|
||||||
|
url = f"{STORE_URL}/api/v1/catalog/products/{SKU}"
|
||||||
|
print(f"POST {url}")
|
||||||
|
print(f"Data: {data}")
|
||||||
|
try:
|
||||||
|
resp = requests.post(url, headers=get_headers(), data=data, timeout=15)
|
||||||
|
print(f"Status: {resp.status_code}")
|
||||||
|
print(f"Response: {resp.text[:500] if resp.text else 'empty'}")
|
||||||
|
return resp
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Ошибка: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def test_is_special_string():
|
||||||
|
"""Тест: is_special как строка 'true'"""
|
||||||
|
print(f"\n=== TEST: is_special='true' (строка) ===")
|
||||||
|
update_product({'is_special': 'true'})
|
||||||
|
|
||||||
|
def test_is_special_bool():
|
||||||
|
"""Тест: is_special как boolean True"""
|
||||||
|
print(f"\n=== TEST: is_special=True (будет отправлено как 'True') ===")
|
||||||
|
update_product({'is_special': True})
|
||||||
|
|
||||||
|
def test_is_special_int():
|
||||||
|
"""Тест: is_special как int 1"""
|
||||||
|
print(f"\n=== TEST: is_special=1 (int) ===")
|
||||||
|
update_product({'is_special': 1})
|
||||||
|
|
||||||
|
def test_marks_special():
|
||||||
|
"""Тест: marks[special] вместо is_special"""
|
||||||
|
print(f"\n=== TEST: marks[special]='true' ===")
|
||||||
|
update_product({'marks[special]': 'true'})
|
||||||
|
|
||||||
|
def test_reset_special():
|
||||||
|
"""Сброс is_special в false"""
|
||||||
|
print(f"\n=== RESET: is_special='false' ===")
|
||||||
|
update_product({'is_special': 'false'})
|
||||||
|
|
||||||
|
def test_special_1():
|
||||||
|
"""Тест: special=1 (как в админке!)"""
|
||||||
|
print(f"\n=== TEST: special=1 (формат админки) ===")
|
||||||
|
update_product({'special': 1})
|
||||||
|
|
||||||
|
def test_special_0():
|
||||||
|
"""Сброс: special=0"""
|
||||||
|
print(f"\n=== RESET: special=0 ===")
|
||||||
|
update_product({'special': 0})
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if "your-store" in STORE_URL or "your-api" in API_TOKEN:
|
||||||
|
print("=" * 50)
|
||||||
|
print("ОШИБКА: Заполни STORE_URL и API_TOKEN в скрипте!")
|
||||||
|
print("Открой test_is_special.py и замени значения")
|
||||||
|
print("=" * 50)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
print("=" * 50)
|
||||||
|
print("Тестирование флага is_special для Recommerce API")
|
||||||
|
print(f"Store: {STORE_URL}")
|
||||||
|
print(f"SKU: {SKU}")
|
||||||
|
print("=" * 50)
|
||||||
|
|
||||||
|
# Сначала смотрим текущее состояние
|
||||||
|
get_product()
|
||||||
|
|
||||||
|
print("\n" + "=" * 50)
|
||||||
|
print("Выберите тест:")
|
||||||
|
print("1 - GET (показать текущее состояние)")
|
||||||
|
print("2 - is_special='true' (строка)")
|
||||||
|
print("3 - is_special=True (bool)")
|
||||||
|
print("4 - is_special=1 (int)")
|
||||||
|
print("5 - marks[special]='true'")
|
||||||
|
print("6 - Сброс is_special='false'")
|
||||||
|
print("7 - special=1 (ФОРМАТ АДМИНКИ!)")
|
||||||
|
print("8 - special=0 (сброс)")
|
||||||
|
print("0 - Выход")
|
||||||
|
print("=" * 50)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
choice = input("\nВведите номер теста: ").strip()
|
||||||
|
|
||||||
|
if choice == "0":
|
||||||
|
break
|
||||||
|
elif choice == "1":
|
||||||
|
get_product()
|
||||||
|
elif choice == "2":
|
||||||
|
test_is_special_string()
|
||||||
|
elif choice == "3":
|
||||||
|
test_is_special_bool()
|
||||||
|
elif choice == "4":
|
||||||
|
test_is_special_int()
|
||||||
|
elif choice == "5":
|
||||||
|
test_marks_special()
|
||||||
|
elif choice == "6":
|
||||||
|
test_reset_special()
|
||||||
|
elif choice == "7":
|
||||||
|
test_special_1()
|
||||||
|
elif choice == "8":
|
||||||
|
test_special_0()
|
||||||
|
else:
|
||||||
|
print("Неверный выбор")
|
||||||
Reference in New Issue
Block a user