import os from django.core.management.base import BaseCommand from apps.projects.models import Project, Asset from apps.templates.models import CardTemplate class Command(BaseCommand): help = 'Initialize cards design system with sample data' def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS('Starting initialization...')) self.create_templates() self.create_sample_project() self.stdout.write(self.style.SUCCESS('Initialization complete!')) def create_templates(self): """创建示例模板:每个模板绑定到一个预设主题""" templates = [ { 'id': 'classic', 'name': '经典风格', 'description': '标准扑克牌设计:王冠/权杖/小丑,黑色红色彩色花色', 'color_spade': '#000000', 'color_heart': '#E53935', 'color_club': '#000000', 'color_diamond': '#E53935', 'color_background': '#FFFFFF', 'theme_id': 'classical', 'design_override': { 'border_color': '#333333', 'border_width': 2, 'pip_size_ratio': 0.16, 'corner_size_ratio': 0.13, 'font_family': 'Times New Roman', }, }, { 'id': 'modern', 'name': '现代简约', 'description': '现代人物主题:小孩/女青年/男青年/小丑鱼,浅色背景', 'color_spade': '#333333', 'color_heart': '#E53935', 'color_club': '#333333', 'color_diamond': '#E53935', 'color_background': '#FAFAFA', 'theme_id': 'modern', 'design_override': { 'border_color': '#888888', 'border_width': 1, 'pip_size_ratio': 0.15, 'corner_size_ratio': 0.12, 'font_family': 'Arial', }, }, { 'id': 'cartoon', 'name': '卡通风格', 'description': 'Q版可爱人像,圆润花色图案,暖黄背景', 'color_spade': '#4A4A4A', 'color_heart': '#FF6B9D', 'color_club': '#4A4A4A', 'color_diamond': '#FF6B9D', 'color_background': '#FFF9E6', 'theme_id': 'modern', 'design_override': { 'border_color': '#FF8E72', 'border_width': 3, 'pip_size_ratio': 0.17, 'corner_size_ratio': 0.14, 'font_family': 'Comic Sans MS', }, }, { 'id': 'vintage', 'name': '复古风格', 'description': '复古色调和纹理,深色边框,米色背景', 'color_spade': '#2C1810', 'color_heart': '#8B4513', 'color_club': '#2C1810', 'color_diamond': '#8B4513', 'color_background': '#F5DEB3', 'theme_id': 'minimal', 'design_override': { 'border_color': '#5D3A1A', 'border_width': 4, 'pip_size_ratio': 0.15, 'corner_size_ratio': 0.13, 'font_family': 'Georgia', }, }, ] for td in templates: template, created = CardTemplate.objects.update_or_create( id=td['id'], defaults={ 'name': td['name'], 'description': td['description'], 'color_spade': td['color_spade'], 'color_heart': td['color_heart'], 'color_club': td['color_club'], 'color_diamond': td['color_diamond'], 'color_background': td['color_background'], 'theme_id': td['theme_id'], 'design_override': td['design_override'], 'default_assets': td, }, ) verb = 'created' if created else 'updated' self.stdout.write(f' template {template.id} {verb} (theme={td["theme_id"]})') def create_sample_project(self): """创建示例项目:完整可玩的 54 张牌(应用经典模板)""" from apps.templates.template_apply import apply_template_to_project # 删除旧示例项目 Project.objects.filter(name="示例项目").delete() project = Project.objects.create( name="示例项目", template_id='classic', card_width=750, card_height=1050, export_resolution='standard', export_include_back=True, ) # 应用经典模板(自动写入 design + 复制 4 套古典素材到 asset) tpl = CardTemplate.objects.get(pk='classic') result = apply_template_to_project(project, tpl) self.stdout.write(f' applied template: {result["applied"]} assets') self.stdout.write(self.style.SUCCESS(f'示例项目 ID: {project.id}'))