cli.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import asyncio
  4. import typer
  5. from passlib.context import CryptContext
  6. from common.enums import PlatformEnum
  7. from core.config import settings
  8. from db.asyncsession import LocalAsyncSession
  9. from models.user import Admin
  10. from models.sysdata.role import Role
  11. cli = typer.Typer()
  12. pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto", bcrypt__default_rounds=8)
  13. def hashed_password(password: str) -> str:
  14. return pwd_context.hash(password)
  15. async def _create_superuser():
  16. async with LocalAsyncSession.begin() as session:
  17. admin = Admin(username=settings.ADMIN_USERNAME,
  18. password=hashed_password(settings.ADMIN_PASSWORD),
  19. phone=settings.ADMIN_PHONE,
  20. role_id=settings.ADMIN_ROLE_ID,
  21. creator_id=1,
  22. editor_id=1,
  23. creator_name="admin",
  24. editor_name="admin")
  25. session.add(admin)
  26. await session.commit()
  27. @cli.command("createsuperuser")
  28. def create_superuser():
  29. asyncio.get_event_loop().run_until_complete(_create_superuser())
  30. print("Done")
  31. async def _create_role():
  32. async with LocalAsyncSession.begin() as session:
  33. admin = Role(name="管理员", platform=PlatformEnum.admin)
  34. session.add(admin)
  35. teacher = Role(name="教师", platform=PlatformEnum.teacher)
  36. session.add(teacher)
  37. student = Role(name="学生", platform=PlatformEnum.student)
  38. session.add(student)
  39. await session.commit()
  40. @cli.command("createrole")
  41. def create_role():
  42. asyncio.get_event_loop().run_until_complete(_create_role())
  43. print("Done")
  44. if __name__ == '__main__':
  45. cli()