from django.core.management.base import BaseCommand from tool.models import ToolCategory, Tool class Command(BaseCommand): help = '初始化工具分类和工具数据' def handle(self, *args, **options): categories = { '开发': ToolCategory.objects.get_or_create( name='开发', defaults={'icon': 'CodeOutlined', 'sort_order': 1} )[0], '设计': ToolCategory.objects.get_or_create( name='设计', defaults={'icon': 'PictureOutlined', 'sort_order': 2} )[0], '实用': ToolCategory.objects.get_or_create( name='实用', defaults={'icon': 'ThunderboltOutlined', 'sort_order': 3} )[0], } tools_data = [ {'name': '万能翻译', 'description': '支持200+语种互译,文本翻译、语种识别、图片翻译、语音翻译', 'icon': 'TranslationOutlined', 'url_path': '/baidu-translate', 'category': categories['实用'], 'color': '#667eea', 'usage_count': 12580, 'is_new': True}, {'name': 'JSON格式化', 'description': 'JSON数据格式化、压缩、校验', 'icon': 'CodeOutlined', 'url_path': '/utility/json-formatter', 'category': categories['开发'], 'color': '#6366f1', 'usage_count': 8542}, {'name': '时间戳转换', 'description': 'Unix时间戳与日期时间互转', 'icon': 'ClockCircleOutlined', 'url_path': '/utility/timestamp', 'category': categories['开发'], 'color': '#8b5cf6', 'usage_count': 6231}, {'name': 'Base64编解码', 'description': 'Base64编码与解码工具', 'icon': 'FileTextOutlined', 'url_path': '/utility/base64', 'category': categories['开发'], 'color': '#ec4899', 'usage_count': 5190}, {'name': '编码转换器', 'description': '多种编码格式互转,支持URL/Base64/Unicode/Hex/Binary等', 'icon': 'CodeOutlined', 'url_path': '/utility/encoding-converter', 'category': categories['开发'], 'color': '#f43f5e', 'usage_count': 3876}, {'name': '正则表达式测试', 'description': '在线正则表达式测试与验证', 'icon': 'SearchOutlined', 'url_path': '/utility/regex', 'category': categories['开发'], 'color': '#f97316', 'usage_count': 4523}, {'name': '代码格式化', 'description': '多语言代码格式化工具,支持JS/TS/Python/HTML/CSS等', 'icon': 'CodeOutlined', 'url_path': '/utility/code-formatter', 'category': categories['开发'], 'color': '#06b6d4', 'usage_count': 2000}, {'name': '颜色转换器', 'description': 'HEX、RGB、HSL颜色格式转换', 'icon': 'PictureOutlined', 'url_path': '/utility/color-converter', 'category': categories['设计'], 'color': '#eab308', 'usage_count': 3210}, {'name': '二维码生成', 'description': '生成自定义样式二维码', 'icon': 'QrcodeOutlined', 'url_path': '/qrcode-generator', 'category': categories['实用'], 'color': '#22c55e', 'usage_count': 9876}, {'name': '文本对比', 'description': '文本差异对比工具', 'icon': 'FileTextOutlined', 'url_path': '/utility/text-diff', 'category': categories['开发'], 'color': '#14b8a6', 'usage_count': 2543}, {'name': '计算器', 'description': '科学计算器', 'icon': 'CalculatorOutlined', 'url_path': '/utility/calculator', 'category': categories['实用'], 'color': '#06b6d4', 'usage_count': 7654}, {'name': '日期计算器', 'description': '日期间隔计算与推算', 'icon': 'CalendarOutlined', 'url_path': '/utility/date-calculator', 'category': categories['实用'], 'color': '#3b82f6', 'usage_count': 4321}, {'name': '图片压缩', 'description': '在线图片压缩工具', 'icon': 'CompressOutlined', 'url_path': '/utility/image-compressor', 'category': categories['实用'], 'color': '#6366f1', 'usage_count': 6789, 'is_new': True}, {'name': '取色器', 'description': '颜色拾取、HEX/RGB/HSL转换、历史记录', 'icon': 'PictureOutlined', 'url_path': '/color-picker', 'category': categories['设计'], 'color': '#eab308', 'usage_count': 4280}, {'name': '字符编码转换', 'description': 'GBK/UTF-8/Big5/Shift_JIS 等字符集互转,支持文件上传下载', 'icon': 'CodeOutlined', 'url_path': '/utility/charset-converter', 'category': categories['开发'], 'color': '#1677ff', 'usage_count': 3650}, {'name': '图片编辑', 'description': '在线图片编辑,支持裁剪、滤镜、调节、文字、画笔', 'icon': 'EditOutlined', 'url_path': '/utility/image-editor', 'category': categories['设计'], 'color': '#f59e0b', 'usage_count': 3456}, ] created_count = 0 for tool_data in tools_data: _, created = Tool.objects.update_or_create( url_path=tool_data['url_path'], defaults=tool_data ) if created: created_count += 1 # 禁用已废弃的滑块验证码工具 disabled = Tool.objects.filter(url_path='/utility/slider-captcha').update(is_enabled=False) if disabled: self.stdout.write(self.style.WARNING(f'已禁用 {disabled} 个滑块验证码工具')) self.stdout.write( self.style.SUCCESS( f'创建 {len(categories)} 个分类, 新建 {created_count} 个工具 (共 {len(tools_data)} 个工具)' ) )