video-shuoshan/backend/apps/accounts/migrations/0004_data_migrate_default_team.py
seaislee1209 add3af7904 feat: v0.7.0 — 确认弹窗 + 秒数显示统一 + 弹窗拖拽修复 + 团队模型完善
- 新增 ConfirmModal 组件,为6处危险操作添加二次确认弹窗
  (禁用团队/用户/成员、删除视频×3处)
- 所有秒数显示统一为千位分隔符+s后缀(如 36,000s)
- 修复 modal/drawer 在 input 中拖拽导致误关闭的 bug
  (onClick → onMouseDown + e.target === e.currentTarget)
- 团队模型完善:三种角色(超管/团管/成员)、四层额度检查、
  团管成员管理页、超管团队管理页
- 关闭公开注册,所有账号由管理员创建

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 20:16:21 +08:00

60 lines
2.0 KiB
Python

# Generated by Django 4.2.29 on 2026-03-15 11:04
from django.db import migrations
def create_default_team(apps, schema_editor):
"""Create a default team and assign all non-staff users to it."""
Team = apps.get_model('accounts', 'Team')
User = apps.get_model('accounts', 'User')
QuotaConfig = apps.get_model('generation', 'QuotaConfig')
# Read defaults from QuotaConfig if it exists
daily_default = 600
monthly_default = 6000
try:
config = QuotaConfig.objects.get(pk=1)
daily_default = config.default_daily_seconds_limit
monthly_default = config.default_monthly_seconds_limit
except QuotaConfig.DoesNotExist:
pass
# Calculate total seconds already consumed by non-staff users
GenerationRecord = apps.get_model('generation', 'GenerationRecord')
from django.db.models import Sum
total_used = GenerationRecord.objects.filter(
user__is_staff=False
).aggregate(total=Sum('seconds_consumed'))['total'] or 0
# Create default team with a generous initial pool
team = Team.objects.create(
name='默认团队',
total_seconds_pool=max(int(total_used) + 36000, 36000), # used + 10 hours buffer
total_seconds_used=total_used,
monthly_seconds_limit=monthly_default,
daily_member_limit_default=daily_default,
)
# Assign all non-staff users to the default team
User.objects.filter(is_staff=False).update(team=team)
def reverse_migration(apps, schema_editor):
"""Remove default team assignment."""
User = apps.get_model('accounts', 'User')
Team = apps.get_model('accounts', 'Team')
User.objects.filter(team__name='默认团队').update(team=None)
Team.objects.filter(name='默认团队').delete()
class Migration(migrations.Migration):
dependencies = [
('accounts', '0003_add_team_model_and_user_team_fields'),
('generation', '0003_generationrecord_ark_task_id_and_more'),
]
operations = [
migrations.RunPython(create_default_team, reverse_migration),
]