#!/usr/bin/env python # -*- coding: utf-8 -*- from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker from core.config import settings from core.security import hashed_password from crud.base import CRUDBase from models.user import SysUser from schemas.user import CreateAdminUser class Crud(CRUDBase): def create(self, db: Session, user: CreateAdminUser): db_obj = SysUser(username=user.username, password=hashed_password(user.password), phone=user.phone, role_id=user.role_id, last_login_time=user.last_login_time, last_login_ip=user.last_login_ip) db.add(db_obj) db.commit() db.refresh(db_obj) return user def init_db(db: Session) -> None: # admin crud_admin = Crud(SysUser) admin = CreateAdminUser(username=settings.ADMIN_USERNAME, password=settings.ADMIN_PASSWORD, phone="13579246810", role_id=settings.ADMIN_ROLE_ID) crud_admin.create(db, admin) # permission # enforcer = get_enforcer() # enforcer.add_permission_for_user("*", "/*", "*") if __name__ == '__main__': engine = create_engine(settings.SYNC_MYSQL_URI, pool_pre_ping=True) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) db = SessionLocal() init_db(db)