from django.core.management.base import BaseCommand from apidirectory.models import ApiCategory, ApiItem class Command(BaseCommand): help = 'Seed API directory with initial data' def handle(self, *args, **options): self.stdout.write('Seeding API directory...') categories_data = [ {'name': '数据查询', 'icon': 'SearchOutlined', 'sort_order': 1}, {'name': '位置服务', 'icon': 'EnvironmentOutlined', 'sort_order': 2}, {'name': '金融货币', 'icon': 'DollarOutlined', 'sort_order': 3}, {'name': '工具服务', 'icon': 'ToolOutlined', 'sort_order': 4}, {'name': '翻译服务', 'icon': 'TranslationOutlined', 'sort_order': 5}, ] categories = {} for cat_data in categories_data: cat, created = ApiCategory.objects.get_or_create( name=cat_data['name'], defaults=cat_data ) categories[cat_data['name']] = cat if created: self.stdout.write(f' Created category: {cat.name}') items_data = [ { 'name': '天气查询', 'description': '根据城市名称查询实时天气信息,支持全球城市和多种温度单位', 'url_path': '/api/weather', 'method': 'GET', 'category': '数据查询', 'is_new': True, 'color': '#10b981', 'image_url': 'https://images.unsplash.com/photo-1504608524841-42fe6f032b4b?w=800&h=600&fit=crop', }, { 'name': '空气质量指数', 'description': '查询指定城市的空气质量指数AQI、PM2.5、PM10等环境数据', 'url_path': '/api/air-quality/', 'method': 'GET', 'category': '数据查询', 'is_new': True, 'color': '#3b82f6', 'image_url': 'https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=800&h=600&fit=crop', }, { 'name': 'IP地址定位', 'description': '根据IP地址获取地理位置信息,支持自动识别客户端IP', 'url_path': '/api/getIpData/', 'method': 'GET', 'category': '位置服务', 'color': '#8b5cf6', 'image_url': 'https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=800&h=600&fit=crop', }, { 'name': '二维码生成', 'description': '根据URL或文本生成二维码图片', 'url_path': '/api/qrcode-generator/', 'method': 'POST', 'category': '工具服务', 'color': '#ec4899', 'image_url': 'https://images.unsplash.com/photo-1558591710-4b4a1ae0f04d?w=800&h=600&fit=crop', }, { 'name': '百度翻译', 'description': '支持200+语言互译,自动检测源语言', 'url_path': '/api/baiduFanyi/', 'method': 'POST', 'category': '翻译服务', 'color': '#06b6d4', 'is_enabled': True, 'image_url': 'https://images.unsplash.com/photo-1456513080510-7bf3a84b82f8?w=800&h=600&fit=crop', }, { 'name': '文本比较', 'description': '比较两段文本的差异,支持多种比较模式', 'url_path': '/api/tool/text-diff/', 'method': 'POST', 'category': '工具服务', 'color': '#14b8a6', 'image_url': 'https://images.unsplash.com/photo-1518770660439-4636190af475?w=800&h=600&fit=crop', }, { 'name': '图片压缩', 'description': '在线压缩图片,支持批量处理和质量调节', 'url_path': '/api/tool/image-compressor', 'method': 'POST', 'category': '工具服务', 'color': '#84cc16', 'image_url': 'https://images.unsplash.com/photo-1542038784456-1ea8e935640e?w=800&h=600&fit=crop', }, { 'name': '短链接生成', 'description': '将长链接转换为短链接,支持自定义后缀', 'url_path': '/api/shorturl/shorten/', 'method': 'POST', 'category': '工具服务', 'color': '#f97316', 'image_url': 'https://images.unsplash.com/photo-1557682172-298e090bd0f1?w=800&h=600&fit=crop', }, ] for item_data in items_data: cat_name = item_data.pop('category') item_data['category'] = categories[cat_name] item, created = ApiItem.objects.update_or_create( url_path=item_data['url_path'], defaults=item_data ) if created: self.stdout.write(f' Created item: {item.name} ({item.url_path})') else: self.stdout.write(f' Updated item: {item.name} ({item.url_path})') self.stdout.write(self.style.SUCCESS('API directory seeded successfully!'))