33 lines
2.0 KiB
Python
33 lines
2.0 KiB
Python
|
|
from django.core.management.base import BaseCommand, CommandError
|
||
|
|
from django.db import transaction
|
||
|
|
|
||
|
|
from control.models import PlanCatalogDraft
|
||
|
|
from control.plan_catalog import proposed_catalog
|
||
|
|
|
||
|
|
|
||
|
|
class Command(BaseCommand):
|
||
|
|
help = "预览12档个人流量、个人无限流量、5档团队及团队无限流量;--create 仅创建草稿,不上架、不收费。"
|
||
|
|
|
||
|
|
def add_arguments(self, parser):
|
||
|
|
parser.add_argument("--create", action="store_true", help="创建独立草稿;不改现有Plan/订阅/价格。")
|
||
|
|
|
||
|
|
def handle(self, *args, **options):
|
||
|
|
proposals = proposed_catalog()
|
||
|
|
created_count = 0
|
||
|
|
if options["create"]:
|
||
|
|
with transaction.atomic():
|
||
|
|
for spec in proposals:
|
||
|
|
defaults = {key: value for key, value in spec.items() if key != "slug"}
|
||
|
|
draft, created = PlanCatalogDraft.objects.get_or_create(slug=spec["slug"], defaults=defaults)
|
||
|
|
draft = PlanCatalogDraft.objects.select_for_update().get(pk=draft.pk)
|
||
|
|
# Protect the seed identity. Preserve later pricing/limits/notes
|
||
|
|
# instead of resetting choices made by an administrator.
|
||
|
|
if any(getattr(draft, key) != spec[key] for key in ("scope", "traffic_limit_gb")):
|
||
|
|
raise CommandError(f"草稿标识 {spec['slug']} 已有不同流量或类型,本批次不写入;请人工核对。")
|
||
|
|
draft.full_clean()
|
||
|
|
created_count += int(created)
|
||
|
|
for spec in proposals:
|
||
|
|
self.stdout.write(f"{spec['name']} | {spec['traffic_limit_gb']} GB (0=不限代理流量)")
|
||
|
|
mode = f"创建{created_count}项,保留{len(proposals) - created_count}项已有草稿" if options["create"] else "仅预览,未写入"
|
||
|
|
self.stdout.write(f"共{len(proposals)}项:{mode}。月价留空待确认;独立草稿不参与注册/申请/节点下发。原8元1024G套餐及所有订阅不变。")
|