Spaces:
Sleeping
Sleeping
Download scripts/create_admin.py from GodRad/Rag_SaaS_Sys_V4: direct link, hf CLI and curl.
- Browser
- Download file 1.9 kB
-
https://huggingface.co/spaces/GodRad/Rag_SaaS_Sys_V4/resolve/main/scripts/create_admin.py
- Command line
-
hf download hf://spaces/GodRad/Rag_SaaS_Sys_V4/scripts/create_admin.py
-
curl -L -o create_admin.py https://huggingface.co/spaces/GodRad/Rag_SaaS_Sys_V4/resolve/main/scripts/create_admin.py
1.9 kB
| import sys | |
| import os | |
| import bcrypt | |
| import uuid | |
| from datetime import datetime | |
| # Add root to sys.path | |
| sys.path.append(os.getcwd()) | |
| from app.models.database import SessionLocal | |
| from app.models.orm import AdminUserModel | |
| def hash_password(password: str) -> str: | |
| """Hash a password using bcrypt""" | |
| return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') | |
| def create_admin(username, email, password): | |
| db = SessionLocal() | |
| try: | |
| # Check if exists | |
| existing = db.query(AdminUserModel).filter( | |
| (AdminUserModel.username == username) | (AdminUserModel.email == email) | |
| ).first() | |
| if existing: | |
| print(f"Error: Admin with username '{username}' or email '{email}' already exists.") | |
| return | |
| admin_id = str(uuid.uuid4()) | |
| admin_user = AdminUserModel( | |
| id=admin_id, | |
| username=username, | |
| email=email, | |
| hashed_password=hash_password(password), | |
| role="admin", | |
| created_at=datetime.utcnow(), | |
| is_active=True | |
| ) | |
| db.add(admin_user) | |
| db.commit() | |
| print(f"Successfully created admin user: {username}") | |
| print(f"Admin ID: {admin_id}") | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| db.rollback() | |
| finally: | |
| db.close() | |
| if __name__ == "__main__": | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Create initial admin user") | |
| parser.add_argument("--username", required=True, help="Admin username") | |
| parser.add_argument("--email", required=True, help="Admin email") | |
| parser.add_argument("--password", required=True, help="Admin password") | |
| args = parser.parse_args() | |
| create_admin(args.username, args.email, args.password) | |