Skip to content

Commit 79c7491

Browse files
committed
Add a script for creating admin user from the terminal
1 parent 46f899c commit 79c7491

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ echo -e "\nPLANTY_AUTH_SECRET=$(openssl rand -base64 32)" >> .env
2424
# change other secrets in .env
2525
2626
docker compose up -d
27+
28+
docker exec -it planty-backend-1 uv run python -m planty.scripts.create_admin
2729
```
2830

2931
Open `http://localhost` to access the app.

planty/scripts/create_admin.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
import contextlib
3+
4+
import typer
5+
6+
from planty.application.auth import get_user_db, get_user_manager
7+
from planty.application.schemas import UserCreate
8+
from planty.infrastructure.database import raw_async_session_maker
9+
10+
app = typer.Typer()
11+
12+
13+
get_user_db_context = contextlib.asynccontextmanager(get_user_db)
14+
get_user_manager_context = contextlib.asynccontextmanager(get_user_manager)
15+
16+
17+
async def create_admin_user(email: str, password: str) -> None:
18+
async with raw_async_session_maker() as session:
19+
async with get_user_db_context(session) as user_db:
20+
async with get_user_manager_context(user_db) as user_manager:
21+
user = await user_manager.create(
22+
UserCreate(
23+
email=email,
24+
password=password,
25+
is_superuser=True,
26+
is_active=True,
27+
is_verified=True,
28+
)
29+
)
30+
print(f"User {user} successfully created")
31+
32+
33+
@app.command()
34+
def create_admin(
35+
email: str = typer.Option(..., prompt=True),
36+
password: str = typer.Option(
37+
...,
38+
prompt=True,
39+
hide_input=True,
40+
confirmation_prompt=True,
41+
),
42+
):
43+
asyncio.run(create_admin_user(email, password))
44+
45+
46+
if __name__ == "__main__":
47+
app()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dependencies = [
1919
"pytest-mock>=3.14.0",
2020
"python-dateutil>=2.9.0.post0",
2121
"sqlalchemy>=2.0.36",
22+
"typer>=0.15.1",
2223
"types-aiobotocore[s3]>=2.16.1",
2324
"types-docker>=7.1.0.20241229",
2425
"types-python-dateutil>=2.9.0.20241206",

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)