Initial certificate system
This commit is contained in:
1
backend/app/db/__init__.py
Normal file
1
backend/app/db/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
5
backend/app/db/base.py
Normal file
5
backend/app/db/base.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
50
backend/app/db/init_db.py
Normal file
50
backend/app/db/init_db.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import hash_password
|
||||
from app.db.base import Base
|
||||
from app.db.session import engine
|
||||
from app.models import AdminUser, ProjectCourse, Role
|
||||
|
||||
|
||||
def create_tables() -> None:
|
||||
import app.models # noqa: F401
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def seed_defaults(db: Session, admin_username: str, admin_password: str) -> None:
|
||||
roles = [
|
||||
("system_admin", "系统管理员"),
|
||||
("certificate_admin", "证书管理员"),
|
||||
("readonly", "只读查看人员"),
|
||||
]
|
||||
for code, name in roles:
|
||||
if not db.query(Role).filter(Role.code == code).first():
|
||||
db.add(Role(code=code, name=name))
|
||||
|
||||
projects = [
|
||||
("DBY", "大本营"),
|
||||
("QSX", "七三线"),
|
||||
]
|
||||
for code, name in projects:
|
||||
if not db.query(ProjectCourse).filter(ProjectCourse.code == code).first():
|
||||
db.add(
|
||||
ProjectCourse(
|
||||
code=code,
|
||||
name=name,
|
||||
default_certificate_name="培训结业证书",
|
||||
default_course_name=name,
|
||||
default_stage_name=None,
|
||||
default_issuer_name="本公司",
|
||||
)
|
||||
)
|
||||
|
||||
if not db.query(AdminUser).filter(AdminUser.username == admin_username).first():
|
||||
db.add(
|
||||
AdminUser(
|
||||
username=admin_username,
|
||||
password_hash=hash_password(admin_password),
|
||||
role_code="system_admin",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
17
backend/app/db/session.py
Normal file
17
backend/app/db/session.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from collections.abc import Generator
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_engine(settings.database_url, pool_pre_ping=True)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user