Repository navigation
Expand file tree
/
Copy pathcreate_user_cli.py
More file actions
196 lines (162 loc) · 5.88 KB
/
Copy pathcreate_user_cli.py
File metadata and controls
196 lines (162 loc) · 5.88 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
"""
命令行用户创建工具
用于在服务器上创建用户(无需GUI)
用法:
python create_user_cli.py # 交互模式
python create_user_cli.py -u username -p password [-e email] [--admin|--super-admin]
python create_user_cli.py --username username --password password [--email email] [--admin|--super-admin]
"""
import os
import sys
import argparse
def setup_django():
"""初始化 Django 环境"""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stadium_booking.settings')
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import django
django.setup()
def create_user_cli(username, password, email='', user_type='regular'):
"""
创建用户
Args:
username: 用户名
password: 密码
email: 邮箱(可选)
user_type: 用户类型 ('super_admin', 'admin' 或 'regular')
"""
from django.contrib.auth.models import User
from booking.models import Profile
try:
# 检查用户是否已存在
if User.objects.filter(username=username).exists():
print(f"错误:用户 '{username}' 已存在")
return False
# 创建用户
user = User.objects.create_user(
username=username,
password=password,
email=email,
is_staff=(user_type in ('admin', 'super_admin')),
is_superuser=(user_type == 'super_admin')
)
# 创建 Profile
Profile.objects.create(
user=user,
user_type=user_type
)
if user_type == 'super_admin':
type_name = "超级管理员"
elif user_type == 'admin':
type_name = "管理员"
else:
type_name = "普通用户"
print(f"\n✓ 用户创建成功!")
print(f" 用户名: {username}")
print(f" 邮箱: {email or '未设置'}")
print(f" 类型: {type_name}")
return True
except Exception as e:
print(f"错误:创建失败 - {str(e)}")
return False
def interactive_mode():
"""交互模式"""
print("\n" + "="*50)
print(" 体育场馆预约系统 - 创建用户")
print("="*50)
# 获取用户名
while True:
username = input("\n请输入用户名: ").strip()
if username:
break
print("用户名不能为空")
# 获取密码
while True:
password = input("请输入密码: ").strip()
if not password:
print("密码不能为空")
continue
if len(password) < 6:
print("密码长度至少为6位")
continue
break
# 获取邮箱(可选)
email = input("请输入邮箱(可选,回车跳过): ").strip()
# 选择用户类型
print("\n用户类型:")
print(" 1. 普通用户 - 可预约场地、查看预约")
print(" 2. 管理员 - 可管理场地、设置时间段、管理预约")
print(" 3. 超级管理员 - 拥有全部权限,可管理所有用户")
while True:
choice = input("\n请选择 (1/2/3): ").strip()
if choice == '1':
user_type = 'regular'
break
elif choice == '2':
user_type = 'admin'
break
elif choice == '3':
user_type = 'super_admin'
break
print("无效选择,请输入 1、2 或 3")
# 确认信息
print("\n" + "-"*50)
print("确认信息:")
print(f" 用户名: {username}")
print(f" 密码: {'*' * len(password)}")
print(f" 邮箱: {email or '未设置'}")
type_display = {'super_admin': '超级管理员', 'admin': '管理员', 'regular': '普通用户'}.get(user_type, user_type)
print(f" 类型: {type_display}")
print("-"*50)
confirm = input("\n确认创建? (y/n): ").strip().lower()
if confirm != 'y':
print("已取消")
return False
return create_user_cli(username, password, email, user_type)
def main():
parser = argparse.ArgumentParser(
description='体育场馆预约系统 - 创建用户工具',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
示例:
python create_user_cli.py # 交互模式
python create_user_cli.py -u test -p 123456 # 创建普通用户
python create_user_cli.py -u admin -p 123456 --admin # 创建管理员
python create_user_cli.py -u user1 -p pass123 -e a@b.com # 带邮箱的管理员
python create_user_cli.py -u super -p 123456 --super-admin # 创建超级管理员
"""
)
parser.add_argument('-u', '--username', help='用户名')
parser.add_argument('-p', '--password', help='密码')
parser.add_argument('-e', '--email', default='', help='邮箱(可选)')
parser.add_argument('--admin', action='store_true', help='创建为管理员')
parser.add_argument('--super-admin', action='store_true', help='创建为超级管理员')
args = parser.parse_args()
# 初始化 Django
setup_django()
# 如果没有参数,进入交互模式
if not args.username and not args.password:
interactive_mode()
return
# 命令行模式
if not args.username:
print("错误:用户名不能为空")
print("使用 --help 查看帮助")
sys.exit(1)
if not args.password:
print("错误:密码不能为空")
print("使用 --help 查看帮助")
sys.exit(1)
if len(args.password) < 6:
print("错误:密码长度至少为6位")
sys.exit(1)
if args.super_admin:
user_type = 'super_admin'
elif args.admin:
user_type = 'admin'
else:
user_type = 'regular'
success = create_user_cli(args.username, args.password, args.email, user_type)
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()