seed for the first user creation
This commit is contained in:
@@ -4,6 +4,7 @@ from pathlib import Path
|
|||||||
# import uvicorn
|
# import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from src.database.users.crud import Seed
|
||||||
from src.logging.logger import LoggingMiddleware, ProcessingTimeMiddleware
|
from src.logging.logger import LoggingMiddleware, ProcessingTimeMiddleware
|
||||||
from src.web.protected_routes.auth_routes import router as protected_router
|
from src.web.protected_routes.auth_routes import router as protected_router
|
||||||
from src.web.protected_routes.protected_user_action_routes import (
|
from src.web.protected_routes.protected_user_action_routes import (
|
||||||
@@ -14,8 +15,8 @@ from src.web.protected_routes.protected_user_action_routes import (
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
create_dirs()
|
create_dirs()
|
||||||
|
await create_first_user()
|
||||||
yield
|
yield
|
||||||
print("shutting down")
|
|
||||||
|
|
||||||
|
|
||||||
app=FastAPI(root_path="/", lifespan=lifespan)
|
app=FastAPI(root_path="/", lifespan=lifespan)
|
||||||
@@ -30,7 +31,7 @@ async def root()->dict:
|
|||||||
return {"root":"hello, this is root"}
|
return {"root":"hello, this is root"}
|
||||||
|
|
||||||
|
|
||||||
def create_dirs():
|
def create_dirs()->None:
|
||||||
|
|
||||||
dirs_to_create=("./DB",
|
dirs_to_create=("./DB",
|
||||||
"./uploads/upload",
|
"./uploads/upload",
|
||||||
@@ -40,3 +41,8 @@ def create_dirs():
|
|||||||
|
|
||||||
for x in dirs_to_create:
|
for x in dirs_to_create:
|
||||||
Path(x).mkdir(parents=True, exist_ok=True)
|
Path(x).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
async def create_first_user()->None:
|
||||||
|
seed=Seed()
|
||||||
|
await seed.seed()
|
||||||
|
|
||||||
@@ -10,6 +10,7 @@ from src.models.database_models.model import (
|
|||||||
engine,
|
engine,
|
||||||
)
|
)
|
||||||
from src.models.pydantic_models.model import UserOutDB
|
from src.models.pydantic_models.model import UserOutDB
|
||||||
|
from src.service.auth.jwt import HashService
|
||||||
|
|
||||||
|
|
||||||
class UsersCrudActions:
|
class UsersCrudActions:
|
||||||
@@ -122,3 +123,35 @@ class UsersCrudActions:
|
|||||||
|
|
||||||
await session.flush()
|
await session.flush()
|
||||||
return UserOutDB.model_validate(user_edit)
|
return UserOutDB.model_validate(user_edit)
|
||||||
|
|
||||||
|
class Seed:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.Session=async_sessionmaker(bind=engine)
|
||||||
|
self.hash_service=HashService()
|
||||||
|
|
||||||
|
async def seed(self) -> None:
|
||||||
|
async with self.Session() as session, session.begin():
|
||||||
|
|
||||||
|
existing = (await session.execute(select(User).limit(1))).scalar_one_or_none()
|
||||||
|
if existing is not None:
|
||||||
|
print("Seed skipped: one user is already exist")
|
||||||
|
return
|
||||||
|
|
||||||
|
admin_permission = Permissions(permission="admin")
|
||||||
|
session.add(admin_permission)
|
||||||
|
|
||||||
|
admin_group = PermissionsGroups(group="admin_group", permissions=[admin_permission])
|
||||||
|
session.add(admin_group)
|
||||||
|
|
||||||
|
admin_user = User(
|
||||||
|
first_name="Admin",
|
||||||
|
last_name="Admin",
|
||||||
|
middle_name="Admin",
|
||||||
|
email="admin@admin.com",
|
||||||
|
hashed_password=self.hash_service.plain_to_hash("1234"),
|
||||||
|
direct_permissions=[admin_permission],
|
||||||
|
group=[admin_group],
|
||||||
|
)
|
||||||
|
session.add(admin_user)
|
||||||
|
|
||||||
|
print("Seed completed: admin user and permissions are created, credentials: email - admin@admin.com, password - 1234")
|
||||||
Reference in New Issue
Block a user