create_test_user.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. 创建测试用户脚本
  3. 用于快速创建测试账号
  4. """
  5. import sqlite3
  6. import bcrypt
  7. from datetime import datetime
  8. def create_test_user():
  9. """创建测试用户"""
  10. # 连接数据库
  11. conn = sqlite3.connect('annotation_platform.db')
  12. cursor = conn.cursor()
  13. # 测试用户信息
  14. username = "testuser"
  15. email = "test@example.com"
  16. password = "password123"
  17. role = "annotator"
  18. # 检查用户是否已存在
  19. cursor.execute("SELECT id FROM users WHERE username = ?", (username,))
  20. existing_user = cursor.fetchone()
  21. if existing_user:
  22. print(f"用户 '{username}' 已存在,跳过创建")
  23. conn.close()
  24. return
  25. # 生成密码哈希
  26. password_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
  27. # 生成用户 ID
  28. user_id = f"user_{datetime.now().strftime('%Y%m%d%H%M%S')}"
  29. # 插入用户
  30. cursor.execute("""
  31. INSERT INTO users (id, username, email, password_hash, role, created_at)
  32. VALUES (?, ?, ?, ?, ?, ?)
  33. """, (user_id, username, email, password_hash, role, datetime.now()))
  34. conn.commit()
  35. conn.close()
  36. print("=" * 60)
  37. print("测试用户创建成功!")
  38. print("=" * 60)
  39. print(f"用户名: {username}")
  40. print(f"密码: {password}")
  41. print(f"邮箱: {email}")
  42. print(f"角色: {role}")
  43. print(f"用户ID: {user_id}")
  44. print("=" * 60)
  45. print("\n现在可以使用这个账号登录前端应用了!")
  46. print("访问: http://localhost:4200/login")
  47. print("=" * 60)
  48. if __name__ == "__main__":
  49. create_test_user()