-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialization.py
44 lines (33 loc) · 1.09 KB
/
initialization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from passlib.context import CryptContext
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from database import get_db, engine
from enums import UserRole
from models import User, entities
from config import get_settings
settings = get_settings()
password_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def create_admin_user(db: Session):
admin = db.query(User).filter(User.role == UserRole.ADMIN.value).first()
if admin:
print("Admin already exists")
return
admin_user = User(
username=settings.ADMIN_USERNAME,
email=settings.ADMIN_EMAIL,
password=password_context.hash(settings.ADMIN_PASSWORD),
role=UserRole.ADMIN.value,
)
try:
db.add(admin_user)
db.commit()
db.refresh(admin_user)
print("Admin user created")
except SQLAlchemyError as e:
db.rollback()
print(f"Error while creating admin user: {e}")
def initialize_db():
entities.Base.metadata.create_all(bind=engine)
db = next(get_db())
create_admin_user(db)
db.close()