fix: remove weight price multiplier, send Evotor prices as-is to VK
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -19,7 +19,6 @@ class Settings(BaseSettings):
|
|||||||
VK_API_VERSION: str = "5.199"
|
VK_API_VERSION: str = "5.199"
|
||||||
VK_CATEGORY_ID: int = 40932
|
VK_CATEGORY_ID: int = 40932
|
||||||
VK_STOCK_AMOUNT: int = 1000
|
VK_STOCK_AMOUNT: int = 1000
|
||||||
VK_WEIGHT_PRICE_MULTIPLIER: int = 10
|
|
||||||
|
|
||||||
CATALOG_REFRESH_INTERVAL_SECONDS: int = 3600
|
CATALOG_REFRESH_INTERVAL_SECONDS: int = 3600
|
||||||
INVITE_EXPIRE_HOURS: int = 48
|
INVITE_EXPIRE_HOURS: int = 48
|
||||||
|
|||||||
@@ -105,9 +105,9 @@ def _sync_user(db, user_id: int, token: str, group_id: str) -> None:
|
|||||||
price_field = p.get("price")
|
price_field = p.get("price")
|
||||||
if isinstance(price_field, dict):
|
if isinstance(price_field, dict):
|
||||||
price_raw = price_field.get("amount")
|
price_raw = price_field.get("amount")
|
||||||
price = float(price_raw) if price_raw is not None else None
|
price = float(price_raw) / 100 if price_raw is not None else None
|
||||||
else:
|
else:
|
||||||
price = float(price_field) if price_field is not None else None
|
price = float(price_field) / 100 if price_field is not None else None
|
||||||
thumb = None
|
thumb = None
|
||||||
thumb_field = p.get("thumb_photo")
|
thumb_field = p.get("thumb_photo")
|
||||||
if isinstance(thumb_field, dict):
|
if isinstance(thumb_field, dict):
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
VK_API = "https://api.vk.com/method"
|
VK_API = "https://api.vk.com/method"
|
||||||
|
|
||||||
WEIGHT_MEASURES = {"г", "г.", "грамм", "граммов", "гр", "гр."}
|
|
||||||
|
|
||||||
_PHOTO_CACHE: dict[int, str] = {} # user_id → uploaded photo_id for this run
|
_PHOTO_CACHE: dict[int, str] = {} # user_id → uploaded photo_id for this run
|
||||||
|
|
||||||
|
|
||||||
@@ -34,24 +32,14 @@ def _now() -> datetime:
|
|||||||
return datetime.now(timezone.utc).replace(tzinfo=None)
|
return datetime.now(timezone.utc).replace(tzinfo=None)
|
||||||
|
|
||||||
|
|
||||||
def _is_weight(measure: str | None) -> bool:
|
def _calc_price(price: Decimal | None) -> int:
|
||||||
return (measure or "").strip().lower() in WEIGHT_MEASURES
|
"""Return price in kopecks for VK Market API."""
|
||||||
|
|
||||||
|
|
||||||
def _calc_price(price: Decimal | None, measure: str | None) -> float:
|
|
||||||
"""Return price in rubles for VK Market API. Weight items are multiplied."""
|
|
||||||
if price is None:
|
if price is None:
|
||||||
return 0.0
|
return 0
|
||||||
base = float(price)
|
return int(float(price) * 100)
|
||||||
if _is_weight(measure):
|
|
||||||
base *= settings.VK_WEIGHT_PRICE_MULTIPLIER
|
|
||||||
return base
|
|
||||||
|
|
||||||
|
|
||||||
def _build_description(name: str, measure: str | None, evo_description: str | None) -> str:
|
def _build_description(name: str, measure: str | None, evo_description: str | None) -> str:
|
||||||
if _is_weight(measure):
|
|
||||||
price_info = f"{settings.VK_WEIGHT_PRICE_MULTIPLIER}{(measure or '').strip()}"
|
|
||||||
else:
|
|
||||||
price_info = (measure or "").strip()
|
price_info = (measure or "").strip()
|
||||||
desc = f"{name} (цена за {price_info}.)\n\n"
|
desc = f"{name} (цена за {price_info}.)\n\n"
|
||||||
if evo_description:
|
if evo_description:
|
||||||
@@ -169,7 +157,7 @@ def _sync_product(
|
|||||||
token: str,
|
token: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
name = _name_for_vk(product.name)
|
name = _name_for_vk(product.name)
|
||||||
price_rubles = _calc_price(product.price, product.measure_name)
|
price_kopecks = _calc_price(product.price)
|
||||||
desc = _build_description(product.name, product.measure_name, None)
|
desc = _build_description(product.name, product.measure_name, None)
|
||||||
stock = settings.VK_STOCK_AMOUNT if product.allow_to_sell else 0
|
stock = settings.VK_STOCK_AMOUNT if product.allow_to_sell else 0
|
||||||
owner_id = f"-{vk_group_id}"
|
owner_id = f"-{vk_group_id}"
|
||||||
@@ -185,7 +173,7 @@ def _sync_product(
|
|||||||
).first()
|
).first()
|
||||||
album_changed = False
|
album_changed = False
|
||||||
if vk_p:
|
if vk_p:
|
||||||
vk_price = float(vk_p.price) if vk_p.price is not None else 0.0
|
vk_price_kopecks = int(vk_p.price * 100) if vk_p.price is not None else 0
|
||||||
vk_stock = settings.VK_STOCK_AMOUNT if vk_p.availability == 0 else 0
|
vk_stock = settings.VK_STOCK_AMOUNT if vk_p.availability == 0 else 0
|
||||||
vk_name = vk_p.name or ""
|
vk_name = vk_p.name or ""
|
||||||
vk_desc = (vk_p.description or "").strip()
|
vk_desc = (vk_p.description or "").strip()
|
||||||
@@ -193,7 +181,7 @@ def _sync_product(
|
|||||||
album_changed = str(vk_p.album_id) != str(album_id) if album_id else False
|
album_changed = str(vk_p.album_id) != str(album_id) if album_id else False
|
||||||
changed = (
|
changed = (
|
||||||
name != vk_name
|
name != vk_name
|
||||||
or price_rubles != vk_price
|
or price_kopecks != vk_price_kopecks
|
||||||
or curr_desc != vk_desc
|
or curr_desc != vk_desc
|
||||||
or stock != vk_stock
|
or stock != vk_stock
|
||||||
or album_changed
|
or album_changed
|
||||||
@@ -210,7 +198,7 @@ def _sync_product(
|
|||||||
"name": name,
|
"name": name,
|
||||||
"description": desc.strip(),
|
"description": desc.strip(),
|
||||||
"category_id": settings.VK_CATEGORY_ID,
|
"category_id": settings.VK_CATEGORY_ID,
|
||||||
"price": price_rubles,
|
"price": price_kopecks,
|
||||||
"stock_amount": stock,
|
"stock_amount": stock,
|
||||||
}, token, user_id=user_id)
|
}, token, user_id=user_id)
|
||||||
if "error" in resp:
|
if "error" in resp:
|
||||||
@@ -252,7 +240,7 @@ def _sync_product(
|
|||||||
"name": name,
|
"name": name,
|
||||||
"description": desc,
|
"description": desc,
|
||||||
"category_id": settings.VK_CATEGORY_ID,
|
"category_id": settings.VK_CATEGORY_ID,
|
||||||
"price": price_rubles,
|
"price": price_kopecks,
|
||||||
"main_photo_id": photo_id,
|
"main_photo_id": photo_id,
|
||||||
"stock_amount": stock,
|
"stock_amount": stock,
|
||||||
}, token, user_id=user_id)
|
}, token, user_id=user_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user