-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_create_default_admin.py
More file actions
61 lines (47 loc) · 1.83 KB
/
script_create_default_admin.py
File metadata and controls
61 lines (47 loc) · 1.83 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Creates the default admin user for the app."""
import asyncio
from sqlalchemy import func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from app.core import settings
from app.db import db
from app.db.actions.user_crud import create_user
from app.db.models.user_model import User
from app.schemas.user_schemas import UserCreate
async def create_default_admin(db_session: AsyncSession):
"""Create a default admin user if it does not already exist.
Args:
----
db_session (AsyncSession): The SQLAlchemy asynchronous session used to
interact with the database.
Returns:
-------
None: The function does not return a value but prints messages to
indicate the outcome.
"""
query = select(func.count()).select_from(User).filter(User.is_admin)
result = await db_session.execute(query)
user_count = result.scalar()
if user_count and user_count > 0:
print("Admin user already exists on the service app.")
return
default_admin_user = UserCreate(
username=settings.load_settings().DEFAULT_ADMIN_USER,
password=settings.load_settings().DEFAULT_ADMIN_PASSWORD,
is_admin=True,
)
db_user = await create_user(db_session=db_session, user=default_admin_user)
if db_user:
print(
f"The default admin user created successfully! "
f"User/Password: {settings.load_settings().DEFAULT_ADMIN_USER}/"
f"{settings.load_settings().DEFAULT_ADMIN_PASSWORD}",
)
else:
print("The default admin could not be created!")
async def main():
"""Call entry point for the script to create the default admin user."""
async with db.get_session_context_manager() as session:
await create_default_admin(session)
if __name__ == "__main__":
asyncio.run(main())