Files
DjangoAdmin_FastAPI_EleVue/scripts/seed.py
T
ClaudeandClaude 63bebedf05 feat(phase5): 解耦与收尾清理
- core/redis_client.py 移除 scheduler 耦合,lifespan 由 core/app.py 组合(Redis→调度器→Redis)
- main.py 改从 core.app 导入 create_app
- 删除 apps/、extensions/ 目录;utils 删除已迁移垫片与死代码 formatter.py
- 新增 deploy/(nginx、supervisor)、scripts/(init_db、seed)、logs/(结构化日志落盘)
- core/app.py 接入 core.logger setup_logging
- 更新 CLAUDE.md 反映 core/shared/modules 新结构

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-07 10:16:44 +08:00

47 lines
1.6 KiB
Python
Raw 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.
"""
数据种子脚本:重置指定用户密码为 bcrypt 哈希。
用法:
python scripts/seed.py admin 123456 # 重置 admin 用户密码为 123456
python scripts/seed.py admin 123456 3 # 指定用户 ID 3
"""
import argparse
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def main():
parser = argparse.ArgumentParser(description="重置用户密码")
parser.add_argument("username", help="用户名")
parser.add_argument("password", nargs="?", default="123456", help="新密码(默认 123456)")
parser.add_argument("user_id", nargs="?", type=int, default=None, help="用户 ID(可选,避免同名冲突)")
args = parser.parse_args()
from config import DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD
from core.password import generate_salt, encrypt_password
import pymysql
salt = generate_salt()
hashed = encrypt_password(args.password, salt)
conn = pymysql.connect(host=DB_HOST, port=int(DB_PORT), user=DB_USERNAME,
password=DB_PASSWORD, database=DB_DATABASE)
cur = conn.cursor()
if args.user_id:
cur.execute("UPDATE fastapi_user SET salt=%s, password=%s WHERE id=%s",
(salt, hashed, args.user_id))
else:
cur.execute("UPDATE fastapi_user SET salt=%s, password=%s WHERE username=%s",
(salt, hashed, args.username))
conn.commit()
print(f"updated {cur.rowcount} row(s) for username={args.username}")
cur.close()
conn.close()
if __name__ == "__main__":
main()