-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_local_dev.py
More file actions
96 lines (79 loc) · 3.5 KB
/
init_local_dev.py
File metadata and controls
96 lines (79 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
"""
本地开发快速启动脚本
自动初始化数据库并创建超级用户
"""
import os
import sys
import subprocess
def run_command(cmd, description):
"""运行命令并显示描述"""
print(f"\n{'='*60}")
print(f"{description}")
print(f"{'='*60}")
result = subprocess.run(cmd, shell=True)
if result.returncode != 0:
print(f"失败: {description}")
sys.exit(1)
print(f"完成: {description}")
def main():
# 切换到 backend 目录
backend_dir = os.path.join(os.path.dirname(__file__), 'backend')
os.chdir(backend_dir)
print("""
╔════════════════════════════════════════════════════════╗
║ ║
║ CourseLink 本地开发环境快速初始化工具 ║
║ ║
╚════════════════════════════════════════════════════════╝
""")
# 1. 删除旧数据库(如果存在)
db_file = 'db.sqlite3'
if os.path.exists(db_file):
print(f"发现旧数据库文件: {db_file}")
response = input("是否删除旧数据库并重新初始化? (y/N): ")
if response.lower() == 'y':
os.remove(db_file)
print(f"已删除: {db_file}")
# 2. 删除所有 migrations(除了 __init__.py)
print("\n清理 migration 文件...")
for app in ['chatbot', 'extension', 'accounts']:
migrations_dir = os.path.join(app, 'migrations')
if os.path.exists(migrations_dir):
for file in os.listdir(migrations_dir):
if file.endswith('.py') and file != '__init__.py':
file_path = os.path.join(migrations_dir, file)
os.remove(file_path)
print(f" 删除: {file_path}")
# 3. 创建新的 migrations
run_command('python manage.py makemigrations', '创建数据库迁移文件')
# 4. 执行 migrations
run_command('python manage.py migrate', '初始化数据库')
# 5. 创建超级用户
print(f"\n{'='*60}")
print("创建超级用户账号")
print(f"{'='*60}")
print("提示: 用户名建议用 'admin',密码至少 8 位")
try:
run_command('python manage.py createsuperuser', '创建超级用户')
except KeyboardInterrupt:
print("\n跳过创建超级用户")
# 6. 完成
print(f"""
╔════════════════════════════════════════════════════════╗
║ ║
║ 初始化完成! ║
║ ║
╚════════════════════════════════════════════════════════╝
下一步操作:
1. 启动开发服务器:
cd backend
python manage.py runserver
2. 访问管理后台:
http://127.0.0.1:8000/admin
3. API 文档:
http://127.0.0.1:8000/api/
祝开发愉快!
""")
if __name__ == '__main__':
main()