From e0e43f3fc357c707ab7ed0b66cdcbde419da9b88 Mon Sep 17 00:00:00 2001 From: mguschin Date: Tue, 12 May 2026 22:49:13 +0300 Subject: [PATCH] fix: VK Market write API expects rubles, read amount field is kopecks market.add/edit accept price in rubles; market.get returns price.amount in kopecks. Cache stores rubles (amount/100), send rubles on write. Co-Authored-By: Claude Sonnet 4.6 --- web/tasks/vk_catalog.py | 5 ++--- web/tasks/vk_sync.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/web/tasks/vk_catalog.py b/web/tasks/vk_catalog.py index 5c7493e..c8c3cb4 100644 --- a/web/tasks/vk_catalog.py +++ b/web/tasks/vk_catalog.py @@ -104,10 +104,9 @@ def _sync_user(db, user_id: int, token: str, group_id: str) -> None: album_id = str(p["albums_ids"][0]) if p.get("albums_ids") else None price_field = p.get("price") if isinstance(price_field, dict): - price_raw = price_field.get("amount") - price = float(price_raw) / 100 if price_raw is not None else None + price = float(price_field.get("amount", 0)) / 100 if price_field.get("amount") is not None else None else: - price = float(price_field) / 100 if price_field is not None else None + price = float(price_field) if price_field is not None else None thumb = None thumb_field = p.get("thumb_photo") if isinstance(thumb_field, dict): diff --git a/web/tasks/vk_sync.py b/web/tasks/vk_sync.py index 4adde25..4890dca 100644 --- a/web/tasks/vk_sync.py +++ b/web/tasks/vk_sync.py @@ -32,11 +32,11 @@ def _now() -> datetime: return datetime.now(timezone.utc).replace(tzinfo=None) -def _calc_price(price: Decimal | None) -> int: - """Return price in kopecks for VK Market API.""" +def _calc_price(price: Decimal | None) -> float: + """Return price in rubles for VK Market API.""" if price is None: - return 0 - return int(float(price) * 100) + return 0.0 + return float(price) def _build_description(name: str, measure: str | None, evo_description: str | None) -> str: @@ -157,7 +157,7 @@ def _sync_product( token: str, ) -> None: name = _name_for_vk(product.name) - price_kopecks = _calc_price(product.price) + price_rubles = _calc_price(product.price) desc = _build_description(product.name, product.measure_name, None) stock = settings.VK_STOCK_AMOUNT if product.allow_to_sell else 0 owner_id = f"-{vk_group_id}" @@ -173,7 +173,7 @@ def _sync_product( ).first() album_changed = False if vk_p: - vk_price_kopecks = int(vk_p.price * 100) if vk_p.price is not None else 0 + vk_price = float(vk_p.price) if vk_p.price is not None else 0.0 vk_stock = settings.VK_STOCK_AMOUNT if vk_p.availability == 0 else 0 vk_name = vk_p.name or "" vk_desc = (vk_p.description or "").strip() @@ -181,7 +181,7 @@ def _sync_product( album_changed = str(vk_p.album_id) != str(album_id) if album_id else False changed = ( name != vk_name - or price_kopecks != vk_price_kopecks + or price_rubles != vk_price or curr_desc != vk_desc or stock != vk_stock or album_changed @@ -198,7 +198,7 @@ def _sync_product( "name": name, "description": desc.strip(), "category_id": settings.VK_CATEGORY_ID, - "price": price_kopecks, + "price": price_rubles, "stock_amount": stock, }, token, user_id=user_id) if "error" in resp: @@ -240,7 +240,7 @@ def _sync_product( "name": name, "description": desc, "category_id": settings.VK_CATEGORY_ID, - "price": price_kopecks, + "price": price_rubles, "main_photo_id": photo_id, "stock_amount": stock, }, token, user_id=user_id)