From 3cffa9b05dd292dc72e2720df7d647153c29a3bc Mon Sep 17 00:00:00 2001 From: Andrey Smakotin Date: Tue, 13 Jan 2026 13:06:39 +0300 Subject: [PATCH] =?UTF-8?q?fix(recommerce):=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D0=B5=D0=B9=20=D0=BC=D0=B0=D1=80=D0=BA?= =?UTF-8?q?=D0=B5=D1=82=D0=B8=D0=BD=D0=B3=D0=BE=D0=B2=D1=8B=D1=85=20=D1=84?= =?UTF-8?q?=D0=BB=D0=B0=D0=B3=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - is_new → new (API ожидает 'new', не 'is_new') - is_popular → popular (API ожидает 'popular', не 'is_popular') - добавить тестовый скрипт test_is_new.py для проверки флагов Co-Authored-By: Claude Opus 4.5 --- myproject/integrations/recommerce/mappers.py | 26 ++-- test_is_new.py | 124 +++++++++++++++++++ 2 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 test_is_new.py diff --git a/myproject/integrations/recommerce/mappers.py b/myproject/integrations/recommerce/mappers.py index 7deca94..05d31ec 100644 --- a/myproject/integrations/recommerce/mappers.py +++ b/myproject/integrations/recommerce/mappers.py @@ -90,16 +90,22 @@ def to_api_product( if photo.image: data[f'images[{idx}]'] = photo.image.url - # Обработка флагов товара (формат: 1/0 для Recommerce API) - if hasattr(product, 'is_new'): - data['is_new'] = 1 if product.is_new else 0 - if hasattr(product, 'is_popular'): - data['is_popular'] = 1 if product.is_popular else 0 - # special - из модели is_special ИЛИ автоматически при скидке - if hasattr(product, 'is_special'): - data['special'] = 1 if (product.is_special or has_discount) else 0 - else: - data['special'] = 1 if has_discount else 0 + # Обработка маркетинговых флагов (формат: 1/0 для Recommerce API) + # Добавляем только если поле указано в fields ИЛИ fields=None (полное обновление) + if fields is None or 'is_new' in fields: + if hasattr(product, 'is_new'): + data['new'] = 1 if product.is_new else 0 + + if fields is None or 'is_popular' in fields: + if hasattr(product, 'is_popular'): + data['popular'] = 1 if product.is_popular else 0 + + # special - из модели is_special ИЛИ автоматически при наличии скидке + if fields is None or 'is_special' in fields: + if hasattr(product, 'is_special'): + data['special'] = 1 if (product.is_special or has_discount) else 0 + else: + data['special'] = 1 if has_discount else 0 return data diff --git a/test_is_new.py b/test_is_new.py new file mode 100644 index 0000000..795b45f --- /dev/null +++ b/test_is_new.py @@ -0,0 +1,124 @@ +""" +Тестовый скрипт для отладки флага new (Новинки) в 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 'new' in result: + print(f" new: {result['new']}") + if 'popular' in result: + print(f" popular: {result['popular']}") + if 'special' in result: + print(f" special: {result['special']}") + if 'marks' in result: + print(f" marks: {result['marks']}") + 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_new_1(): + """Тест: new=1 (установить флаг Новинки)""" + print(f"\n=== TEST: new=1 (установить флаг Новинки) ===") + update_product({'new': 1}) + +def test_new_0(): + """Тест: new=0 (сбросить флаг Новинки)""" + print(f"\n=== TEST: new=0 (сбросить флаг Новинки) ===") + update_product({'new': 0}) + +def test_popular_1(): + """Тест: popular=1 (установить флаг Популярные)""" + print(f"\n=== TEST: popular=1 (установить флаг Популярные) ===") + update_product({'popular': 1}) + +def test_popular_0(): + """Тест: popular=0 (сбросить флаг Популярные)""" + print(f"\n=== TEST: popular=0 (сбросить флаг Популярные) ===") + update_product({'popular': 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_new.py и замени значения") + print("=" * 50) + exit(1) + + print("=" * 50) + print("Тестирование флага new (Новинки) для Recommerce API") + print(f"Store: {STORE_URL}") + print(f"SKU: {SKU}") + print("=" * 50) + + # Сначала смотрим текущее состояние + get_product() + + print("\n" + "=" * 50) + print("Выберите тест:") + print("1 - GET (показать текущее состояние)") + print("2 - new=1 (установить флаг Новинки)") + print("3 - new=0 (сбросить флаг Новинки)") + print("4 - popular=1 (установить флаг Популярные)") + print("5 - popular=0 (сбросить флаг Популярные)") + print("0 - Выход") + print("=" * 50) + + while True: + choice = input("\nВведите номер теста: ").strip() + + if choice == "0": + break + elif choice == "1": + get_product() + elif choice == "2": + test_new_1() + elif choice == "3": + test_new_0() + elif choice == "4": + test_popular_1() + elif choice == "5": + test_popular_0() + else: + print("Неверный выбор")