| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- """
- Put here any Python code that must be runned before application startup.
- It is included in `init.sh` script.
- By defualt `main` create a superuser if not exists
- """
- import asyncio
- from sqlalchemy import select
- from app.core import config, security
- from app.core.session import async_session
- from app.models import User
- async def main() -> None:
- print("Start initial data")
- async with async_session() as session:
- result = await session.execute(
- select(User).where(User.email == config.settings.FIRST_SUPERUSER_EMAIL)
- )
- user = result.scalars().first()
- if user is None:
- new_superuser = User(
- email=config.settings.FIRST_SUPERUSER_EMAIL,
- hashed_password=security.get_password_hash(
- config.settings.FIRST_SUPERUSER_PASSWORD
- ),
- )
- session.add(new_superuser)
- await session.commit()
- print("Superuser was created")
- else:
- print("Superuser already exists in database")
- print("Initial data created")
- if __name__ == "__main__":
- asyncio.run(main())
|