Download scripts\create_user.py from aionaion/chatdoc: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/scripts%5Ccreate_user.py
- Command line
-
hf download 'hf://spaces/aionaion/chatdoc/scripts\create_user.py'
-
curl -L -o 'scripts\create_user.py' https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/scripts%5Ccreate_user.py
1.59 kB
| #!/usr/bin/env python3 | |
| import sys | |
| import os | |
| import argparse | |
| from pathlib import Path | |
| # Configura o caminho para importar módulos da app | |
| project_root = Path(__file__).resolve().parent.parent | |
| os.chdir(project_root) | |
| sys.path.append(str(project_root)) | |
| from app.db.database import SessionLocal | |
| from app.db.models import User | |
| from app.core.security import get_password_hash | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Criar usuário no banco de dados") | |
| parser.add_argument("username", help="Nome do usuário") | |
| parser.add_argument("password", help="Senha") | |
| parser.add_argument("scopes", help="Escopos separados por vírgula (ex: sistemas,farmaceutico)") | |
| args = parser.parse_args() | |
| db = SessionLocal() | |
| try: | |
| # Verifica se usuário já existe | |
| existing_user = db.query(User).filter(User.username == args.username).first() | |
| if existing_user: | |
| print(f"❌ Erro: Usuário '{args.username}' já existe.") | |
| return | |
| # Cria novo usuário | |
| new_user = User( | |
| username=args.username, | |
| hashed_password=get_password_hash(args.password), | |
| scopes=args.scopes, | |
| is_active=True | |
| ) | |
| db.add(new_user) | |
| db.commit() | |
| db.refresh(new_user) | |
| print(f"✅ Usuário '{new_user.username}' criado com sucesso!") | |
| print(f" Escopos: {new_user.scopes}") | |
| except Exception as e: | |
| print(f"❌ Erro ao criar usuário: {e}") | |
| finally: | |
| db.close() | |
| if __name__ == "__main__": | |
| main() |