import os from django.core.management.base import BaseCommand from django.core.files.storage import default_storage from apps.projects.models import Project, Asset, CardLayer 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_default_assets() 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': '#FF0000', 'color_club': '#000000', 'color_diamond': '#FF0000', 'color_background': '#FFFFFF', }, { 'id': 'modern', 'name': '现代简约', 'description': '扁平化设计,简洁线条', 'color_spade': '#333333', 'color_heart': '#E53935', 'color_club': '#333333', 'color_diamond': '#E53935', 'color_background': '#FAFAFA', } ] for template_data in templates: try: template = CardTemplate.objects.get(id=template_data['id']) if not template.default_assets: template.default_assets = template_data template.save() except CardTemplate.DoesNotExist: template = CardTemplate.objects.create( id=template_data['id'], name=template_data['name'], description=template_data['description'], color_spade=template_data['color_spade'], color_heart=template_data['color_heart'], color_club=template_data['color_club'], color_diamond=template_data['color_diamond'], color_background=template_data['color_background'], default_assets=template_data ) def create_default_assets(self): """创建默认花色素材""" suits = { 'spade': 0xE27B60, 'heart': 0xE27B60, 'club': 0xE27B60, 'diamond': 0xE27B60 } materials = 'backend/media/assets' os.makedirs(materials, exist_ok=True) for suit_name, color_code in suits.items(): # 创建简单SVG花色图案 svg_path = os.path.join('backend/media/assets', f'{suit_name}.svg') storyboardSVG = ''' ^ ''' with open(svg_path, 'w', encoding='utf-8') as f: f.write(storyboardSVG) # 创建Asset记录 Asset.objects.create( asset_type='suit_symbol', asset_key=suit_name, color=f'#{color_code:06X}' ) def create_sample_project(self): """创建示例项目""" try: project = Project.objects.get(name="示例项目") self.stdout.write(self.style.WARNING('示例项目已存在,跳过创建')) return except Project.DoesNotExist: project = Project.objects.create( name="示例项目", template_id='classic', card_width=750, card_height=1050, export_resolution='standard', export_include_back=True ) # 创建示例素材 suit_assets = [ {'type': 'suit_symbol', 'key': 'spade'}, {'type': 'suit_symbol', 'key': 'heart'}, {'type': 'suit_symbol', 'key': 'club'}, {'type': 'suit_symbol', 'key': 'diamond'}, ] for asset_data in suit_assets: Asset.objects.create( asset_type=asset_data['type'], asset_key=asset_data['key'], width=60, height=60 ) # 创建JQK示例素材记录(临时) face_cards = [ {'type': 'face_card', 'key': 'spade-J'}, {'type': 'face_card', 'key': 'spade-Q'}, {'type': 'face_card', 'key': 'spade-K'}, ] for face_card in face_cards: Asset.objects.create( asset_type=face_card['type'], asset_key=face_card['key'], width=300, height=500 ) self.stdout.write(self.style.SUCCESS(f'项目 "{project.name}" 已创建'))