40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
"""
|
|
Usage: python scripts/create_admin.py <username> <password>
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
import os
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from sqlalchemy import select
|
|
from app.services.database import AsyncSessionLocal
|
|
from app.services.auth import hash_password
|
|
from app.models.user import User
|
|
|
|
|
|
async def create_admin(username: str, password: str) -> None:
|
|
async with AsyncSessionLocal() as db:
|
|
async with db.begin():
|
|
exists = await db.execute(select(User).where(User.username == username))
|
|
if exists.scalar_one_or_none():
|
|
print(f"User '{username}' already exists.")
|
|
return
|
|
|
|
user = User(
|
|
username=username,
|
|
password_hash=hash_password(password),
|
|
role="admin",
|
|
is_active=True,
|
|
)
|
|
db.add(user)
|
|
|
|
print(f"Admin user '{username}' created.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python scripts/create_admin.py <username> <password>")
|
|
sys.exit(1)
|
|
asyncio.run(create_admin(sys.argv[1], sys.argv[2]))
|