bank-statement-api / auth /seed_users.py
3v324v23's picture
Merge Conflict solved | Build error solved
205f25f
Raw History Blame Contribute Delete
2.62 kB
"""
One-time script to seed/update initial employee accounts.
Usage:
python auth/seed_users.py
- Creates the user if the username AND email are new.
- Updates username + password if the email already exists (handles renames).
- Safe to re-run at any time.
"""
import sys
import os
# Allow importing from project root
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from dotenv import load_dotenv
load_dotenv()
from auth.database import engine, Base, SessionLocal
import auth.models # register models with Base so create_all sees them
from auth.models import User
from auth.service import create_user, hash_password
Base.metadata.create_all(bind=engine)
INITIAL_USERS = [
{
"username": "Befree_employee1",
"password": "Emp1_Test@2026!",
"full_name": "Employee One",
"email": "emp1@befree.com",
"role": "tester",
},
{
"username": "Befree_employee2",
"password": "Emp2_Secure#849",
"full_name": "Employee Two",
"email": "emp2@befree.com",
"role": "tester",
},
{
"username": "Befree_employee3",
"password": "Emp3_Login$572",
"full_name": "Employee Three",
"email": "emp3@befree.com",
"role": "tester",
}
]
for u in INITIAL_USERS:
# Each user gets its own session so a failure on one doesn't cascade
with SessionLocal() as db:
try:
# Check if a user with this email already exists
existing_by_email = db.query(User).filter(User.email == u["email"]).first()
if existing_by_email:
# Update username and password (handles the employee1 -> Befree_employee1 rename)
old_username = existing_by_email.username
existing_by_email.username = u["username"]
existing_by_email.hashed_password = hash_password(u["password"])
existing_by_email.full_name = u["full_name"]
existing_by_email.role = u["role"]
db.commit()
print(f"UPDATE Renamed: {old_username!r} -> {u['username']!r} ({u['role']})")
else:
# Brand new user
user = create_user(db, **u)
print(f"OK Created: {user.username} ({user.role})")
except Exception as e:
db.rollback()
print(f"SKIP Failed: {u['username']} - {e}")
print(
"\nDone. Share credentials with employees and ask them to change passwords after first login."
)