93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
from django.core.management.base import BaseCommand
|
||
from django.contrib.auth import get_user_model
|
||
from django.utils import timezone
|
||
|
||
from plans.models import Plan
|
||
from domains.models import Domain
|
||
|
||
|
||
User = get_user_model()
|
||
|
||
|
||
class Command(BaseCommand):
|
||
help = "导入演示套餐并可选创建演示域名。使用 --user-id 为指定用户创建域名。"
|
||
|
||
def add_arguments(self, parser):
|
||
parser.add_argument('--user-id', type=int, help='为该用户创建示例域名(可选)')
|
||
|
||
def handle(self, *args, **options):
|
||
free, _ = Plan.objects.get_or_create(
|
||
name='Free',
|
||
defaults={
|
||
'description': '基础免费套餐,每域名每月包含 15GB。',
|
||
'billing_mode': 'per_domain_monthly',
|
||
'base_price_per_domain': 0,
|
||
'included_traffic_gb_per_domain': 15,
|
||
'overage_price_per_gb': 0.50,
|
||
'allow_overage': True,
|
||
'is_active': True,
|
||
'is_public': True,
|
||
'features': {
|
||
'waf_enabled': False,
|
||
'http3_enabled': False,
|
||
'logs_enabled': False,
|
||
'websocket_enabled': True,
|
||
}
|
||
}
|
||
)
|
||
pro, _ = Plan.objects.get_or_create(
|
||
name='Pro',
|
||
defaults={
|
||
'description': '专业版,每域名每月包含 200GB,支持更多功能。',
|
||
'billing_mode': 'per_domain_monthly',
|
||
'base_price_per_domain': 49.00,
|
||
'included_traffic_gb_per_domain': 200,
|
||
'overage_price_per_gb': 0.30,
|
||
'allow_overage': True,
|
||
'is_active': True,
|
||
'is_public': True,
|
||
'features': {
|
||
'waf_enabled': True,
|
||
'http3_enabled': True,
|
||
'logs_enabled': True,
|
||
'websocket_enabled': True,
|
||
}
|
||
}
|
||
)
|
||
self.stdout.write(self.style.SUCCESS(f"已准备套餐:Free({free.id}), Pro({pro.id})"))
|
||
|
||
user_id = options.get('user_id')
|
||
if not user_id:
|
||
self.stdout.write(self.style.NOTICE("未提供 --user-id,跳过示例域名创建。"))
|
||
return
|
||
user = User.objects.filter(id=user_id).first()
|
||
if not user:
|
||
self.stdout.write(self.style.ERROR(f"用户 {user_id} 不存在,跳过域名创建。"))
|
||
return
|
||
|
||
today = timezone.now().date()
|
||
month_start = today.replace(day=1)
|
||
# 下月第一天
|
||
if month_start.month == 12:
|
||
next_month_start = month_start.replace(year=month_start.year + 1, month=1, day=1)
|
||
else:
|
||
next_month_start = month_start.replace(month=month_start.month + 1, day=1)
|
||
current_cycle_end = next_month_start - timezone.timedelta(days=1)
|
||
|
||
d, created = Domain.objects.get_or_create(
|
||
user=user,
|
||
name='example.com',
|
||
defaults={
|
||
'status': Domain.STATUS_PENDING,
|
||
'current_plan': free,
|
||
'current_cycle_start': month_start,
|
||
'current_cycle_end': current_cycle_end,
|
||
'cname_targets': {'www.example.com': 'www.cdn.example.com'},
|
||
'origin_config': {'host': 'origin.example.com', 'protocol': 'http', 'port': 80},
|
||
'edge_server_id': None,
|
||
}
|
||
)
|
||
if created:
|
||
self.stdout.write(self.style.SUCCESS(f"已创建演示域名:{d.name}(用户ID {user.id})"))
|
||
else:
|
||
self.stdout.write(self.style.WARNING(f"演示域名已存在:{d.name}(用户ID {user.id})")) |