from django.db import migrations def seed_level_thresholds(apps, schema_editor): LevelThreshold = apps.get_model('user', 'LevelThreshold') levels = [ (1, 0, '见习生'), (2, 100, '学徒'), (3, 300, '学者'), (4, 600, '探索者'), (5, 1000, '专家'), (6, 1500, '精英'), (7, 2200, '大师'), (8, 3000, '宗师'), (9, 4000, '传说'), (10, 5500, '传奇'), ] for level, xp, title in levels: LevelThreshold.objects.update_or_create( level=level, defaults={'xp_required': xp, 'title': title} ) def seed_task_definitions(apps, schema_editor): TaskDefinition = apps.get_model('user', 'TaskDefinition') daily_tasks = [ { 'name': '每日浏览', 'task_type': 'daily', 'action_type': 'browse', 'description': '浏览任意页面 3 分钟', 'target_count': 3, 'reward_points': 10, 'reward_coins': 1, 'reward_xp': 20, 'sort_order': 1, 'icon': 'EyeOutlined', 'redirect_url': '/', }, { 'name': '每日学习', 'task_type': 'daily', 'action_type': 'learn', 'description': '学习任意课程 5 分钟', 'target_count': 5, 'reward_points': 20, 'reward_coins': 2, 'reward_xp': 40, 'sort_order': 2, 'icon': 'ReadOutlined', 'redirect_url': '/learn', }, { 'name': '每日签到', 'task_type': 'daily', 'action_type': 'checkin', 'description': '完成每日签到', 'target_count': 1, 'reward_points': 5, 'reward_coins': 0, 'reward_xp': 10, 'sort_order': 3, 'icon': 'CheckCircleOutlined', 'redirect_url': '/task-center', }, { 'name': '分享内容', 'task_type': 'daily', 'action_type': 'share', 'description': '分享任意内容到社交平台', 'target_count': 1, 'reward_points': 15, 'reward_coins': 1, 'reward_xp': 30, 'sort_order': 4, 'icon': 'ShareAltOutlined', 'redirect_url': '/', }, ] weekly_tasks = [ { 'name': '学习达人', 'task_type': 'weekly', 'action_type': 'learn', 'description': '本周累计学习 30 分钟', 'target_count': 30, 'reward_points': 100, 'reward_coins': 10, 'reward_xp': 200, 'sort_order': 1, 'icon': 'TrophyOutlined', 'redirect_url': '/learn', }, { 'name': '互动之星', 'task_type': 'weekly', 'action_type': 'post', 'description': '本周发表 5 条评论或帖子', 'target_count': 5, 'reward_points': 80, 'reward_coins': 8, 'reward_xp': 150, 'sort_order': 2, 'icon': 'LikeOutlined', 'redirect_url': '/articles', }, { 'name': '全勤签到', 'task_type': 'weekly', 'action_type': 'checkin', 'description': '本周签到 7 次', 'target_count': 7, 'reward_points': 150, 'reward_coins': 15, 'reward_xp': 300, 'sort_order': 3, 'icon': 'CalendarOutlined', 'redirect_url': '/task-center', }, ] special_tasks = [ { 'name': '完善个人资料', 'task_type': 'special', 'action_type': 'profile', 'description': '设置头像、昵称和个人简介', 'target_count': 1, 'reward_points': 50, 'reward_coins': 5, 'reward_xp': 100, 'sort_order': 1, 'icon': 'UserOutlined', 'redirect_url': '/profile', }, { 'name': '首次签到', 'task_type': 'special', 'action_type': 'checkin', 'description': '完成你的第一次签到', 'target_count': 1, 'reward_points': 30, 'reward_coins': 3, 'reward_xp': 50, 'sort_order': 2, 'icon': 'StarOutlined', 'redirect_url': '/task-center', }, { 'name': '绑定邮箱', 'task_type': 'special', 'action_type': 'bind', 'description': '绑定你的邮箱账号', 'target_count': 1, 'reward_points': 50, 'reward_coins': 5, 'reward_xp': 80, 'sort_order': 3, 'icon': 'MailOutlined', 'redirect_url': '/settings', }, { 'name': '邀请好友', 'task_type': 'special', 'action_type': 'invite', 'description': '成功邀请 1 位好友注册', 'target_count': 1, 'reward_points': 100, 'reward_coins': 10, 'reward_xp': 200, 'sort_order': 4, 'icon': 'TeamOutlined', 'redirect_url': '/wallet/invite', }, ] for task_data in daily_tasks + weekly_tasks + special_tasks: TaskDefinition.objects.update_or_create( name=task_data['name'], task_type=task_data['task_type'], defaults=task_data ) def reverse_func(apps, schema_editor): LevelThreshold = apps.get_model('user', 'LevelThreshold') TaskDefinition = apps.get_model('user', 'TaskDefinition') LevelThreshold.objects.all().delete() TaskDefinition.objects.all().delete() class Migration(migrations.Migration): dependencies = [ ('user', '0008_levelthreshold_taskdefinition_userlevel_and_more'), ] operations = [ migrations.RunPython(seed_level_thresholds, reverse_func), migrations.RunPython(seed_task_definitions, reverse_func), ]