| 1234567891011121314151617181920212223242526272829303132333435363738 |
- """
- 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
- """
- from sqlalchemy import select
- from app.core import config, security
- from app.core.session import session
- from app.models import User
- def main() -> None:
- print("Start initial data")
- with session() as db:
- result = db.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
- ),
- )
- db.add(new_superuser)
- db.commit()
- print("Superuser was created")
- else:
- print("Superuser already exists in database")
- print("Initial data created")
- if __name__ == "__main__":
- main()
|