Files
boss_dp/scripts/fill_boss_ids_db.py
ddrwode 038f105ec5 haha
2026-02-27 14:11:12 +08:00

55 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
直接操作数据库,为缺少 boss_id 的账号填充。
有 boss_username 的用其作为 boss_id占位真实 uid 需通过检测登录获取);
无 boss_username 的用 worker_id+browser_name 生成唯一标识。
用法: python scripts/fill_boss_ids_db.py [--dry-run]
"""
import os
import sys
# 项目根目录
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
import django
django.setup()
from django.db.models import Q
from server.models import BossAccount
def main():
dry_run = "--dry-run" in sys.argv
if dry_run:
print("【 dry-run 模式,不实际写入 】")
qs = BossAccount.objects.filter(Q(boss_id="") | Q(boss_id__isnull=True))
total = qs.count()
if total == 0:
print("没有需要填充的账号")
return 0
updated = 0
for acc in qs:
if acc.boss_username:
new_id = str(acc.boss_username).strip()
else:
new_id = f"_{acc.worker_id}_{acc.browser_name}".replace(" ", "_")[:64]
if not dry_run:
acc.boss_id = new_id
acc.save(update_fields=["boss_id"])
updated += 1
print(f" {'[dry] ' if dry_run else ''}id={acc.id} {acc.browser_name}@{acc.worker_id} -> boss_id={new_id}")
print(f"共处理 {updated}/{total}")
return 0
if __name__ == "__main__":
sys.exit(main())