Files
MathApp/subscription/migrations/0014_bazaar_skus_2026_10.py

44 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
شناسه‌ی کالای بازار برای اشتراک‌های سه‌ماهه و شش‌ماهه (۲۰۲۶-۱۰-۱۲).
از پیشخانِ بازار (عکسِ سینیور): «اشتراکی ۹۰ روزه» = 10343، «اشتراکی ۱۸۰ روزه» = 10344. بازار هر دو را زیرِ
com.tadrisino.math.app شناخت (پرسشِ API با توکنِ ساختگی: «purchase not found»، نه «product not found»).
10342 هم در بازار هست ولی کسی نگفته چیست؛ عمداً به هیچ پلنی وصل نشده.
پلن‌ها با مدت پیدا می‌شوند، نه با id (idها بین دیتابیس‌ها فرق دارند). اگر دقیقاً یک پلنِ فروشیِ هم‌مدت
نباشد، migration می‌شکند تا دستی دیده شود؛ SKUی که ادمین از پیش گذاشته دست نمی‌خورد.
"""
from django.core.cache import cache
from django.db import migrations
SKUS = {90: '10343', 180: '10344'}
def forward(apps, schema_editor):
Plan = apps.get_model('subscription', 'Plan')
for days, sku in SKUS.items():
if Plan.objects.filter(bazaar_sku=sku).exists():
continue
plans = list(Plan.objects.filter(kind='subscription', on_sale=True, duration_days=days))
if len(plans) != 1:
raise RuntimeError(
f'SKUِ بازار {sku}: باید دقیقاً یک اشتراکِ فروشیِ {days} روزه باشد، {len(plans)} تا پیدا شد.')
plan = plans[0]
if plan.bazaar_sku:
continue
plan.bazaar_sku = sku
plan.save(update_fields=['bazaar_sku'])
# مدلِ تاریخیِ migration سیگنالِ post_save (subscription/receivers.py) را نمی‌زند؛ بی‌این فهرستِ پلن‌ها تا یک
# ساعت SKUی خالی نشان می‌داد (روی سرور دیده شد). کلید همان PLANS_CACHE_KEY است، کپی تا migration به view وابسته نشود.
cache.delete('subscription:plans:v3')
def backward(apps, schema_editor):
Plan = apps.get_model('subscription', 'Plan')
Plan.objects.filter(bazaar_sku__in=SKUS.values()).update(bazaar_sku='')
class Migration(migrations.Migration):
dependencies = [('subscription', '0013_monthly_plans_2026_10')]
operations = [migrations.RunPython(forward, backward)]