#!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import typer from passlib.context import CryptContext from common.enums import PlatformEnum from core.config import settings from db.asyncsession import LocalAsyncSession from models.user import Admin from models.sysdata.role import Role cli = typer.Typer() pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto", bcrypt__default_rounds=8) def hashed_password(password: str) -> str: return pwd_context.hash(password) async def _create_superuser(): async with LocalAsyncSession.begin() as session: admin = Admin(username=settings.ADMIN_USERNAME, password=hashed_password(settings.ADMIN_PASSWORD), phone=settings.ADMIN_PHONE, role_id=settings.ADMIN_ROLE_ID, creator_id=1, editor_id=1, creator_name="admin", editor_name="admin") session.add(admin) await session.commit() @cli.command("createsuperuser") def create_superuser(): asyncio.get_event_loop().run_until_complete(_create_superuser()) print("Done") async def _create_role(): async with LocalAsyncSession.begin() as session: admin = Role(name="管理员", platform=PlatformEnum.admin) session.add(admin) teacher = Role(name="教师", platform=PlatformEnum.teacher) session.add(teacher) student = Role(name="学生", platform=PlatformEnum.student) session.add(student) await session.commit() @cli.command("createrole") def create_role(): asyncio.get_event_loop().run_until_complete(_create_role()) print("Done") if __name__ == '__main__': cli()