save code

This commit is contained in:
Your Name
2026-01-27 17:30:17 +08:00
parent a416e2091f
commit 884b9f47a2
17 changed files with 910 additions and 44 deletions
+59
View File
@@ -0,0 +1,59 @@
from app.database.database import SessionLocal
from app.models import User
import bcrypt
def create_initial_users():
"""创建初始用户"""
db = SessionLocal()
try:
# 检查是否已有用户
existing_users = db.query(User).count()
if existing_users > 0:
print(f"Database already has {existing_users} users. Skipping initialization.")
return
# 创建初始用户
password = "123456"
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
users = [
User(
username="admin",
password=hashed_password,
department="IT",
role="admin"
),
User(
username="marketing",
password=hashed_password,
department="Marketing",
role="marketing"
),
User(
username="other",
password=hashed_password,
department="Other",
role="other"
)
]
for user in users:
db.add(user)
db.commit()
print("Initial users created successfully!")
print("Username: admin, Password: 123456")
print("Username: marketing, Password: 123456")
print("Username: other, Password: 123456")
except Exception as e:
db.rollback()
print(f"Error creating initial users: {e}")
finally:
db.close()
if __name__ == "__main__":
create_initial_users()