229 lines
8.4 KiB
Python
229 lines
8.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Database initialization script for Family Hub.
|
|
Creates tables and populates with demo data.
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
from datetime import datetime, timedelta
|
|
|
|
# Add parent directory to path to import from app
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from app.core.database import engine, SessionLocal, Base
|
|
from app.models import User, Chore
|
|
from passlib.context import CryptContext
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
def init_db():
|
|
"""Initialize the database with tables and demo data."""
|
|
print("Initializing Family Hub database...")
|
|
|
|
# Create all tables
|
|
print("Creating database tables...")
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
# Check if data already exists
|
|
existing_users = db.query(User).count()
|
|
if existing_users > 0:
|
|
print(f"Database already has {existing_users} users. Skipping initialization.")
|
|
print(" To reset the database, delete the file and run this script again.")
|
|
return
|
|
|
|
# Create demo users
|
|
print("\nCreating demo users...")
|
|
users_data = [
|
|
{"username": "jess", "email": "jess@family.local", "full_name": "Jess", "is_admin": True},
|
|
{"username": "lou", "email": "lou@family.local", "full_name": "Lou", "is_admin": False},
|
|
{"username": "william", "email": "william@family.local", "full_name": "William", "is_admin": False},
|
|
{"username": "xander", "email": "xander@family.local", "full_name": "Xander", "is_admin": False},
|
|
{"username": "bella", "email": "bella@family.local", "full_name": "Bella", "is_admin": False},
|
|
]
|
|
|
|
users = []
|
|
for user_data in users_data:
|
|
user = User(
|
|
username=user_data["username"],
|
|
email=user_data["email"],
|
|
full_name=user_data["full_name"],
|
|
hashed_password=pwd_context.hash("password123"),
|
|
is_admin=user_data["is_admin"],
|
|
is_active=True
|
|
)
|
|
db.add(user)
|
|
users.append(user)
|
|
admin_badge = " [ADMIN]" if user.is_admin else ""
|
|
print(f" + {user.full_name} ({user.username}){admin_badge}")
|
|
|
|
db.flush() # Flush to get user IDs
|
|
|
|
# Create demo chores
|
|
print("\nCreating demo chores...")
|
|
|
|
# Get user IDs for assignment
|
|
jess = next(u for u in users if u.username == "jess")
|
|
lou = next(u for u in users if u.username == "lou")
|
|
william = next(u for u in users if u.username == "william")
|
|
xander = next(u for u in users if u.username == "xander")
|
|
bella = next(u for u in users if u.username == "bella")
|
|
|
|
demo_chores = [
|
|
# Daily chores
|
|
{
|
|
"title": "Feed the pets",
|
|
"description": "Feed cats in the morning and evening",
|
|
"frequency": "daily",
|
|
"assignment_type": "any_one",
|
|
"points": 5,
|
|
"room": "Kitchen",
|
|
"assigned_user_id": bella.id,
|
|
"status": "pending"
|
|
},
|
|
{
|
|
"title": "Take out trash",
|
|
"description": "Empty kitchen and bathroom bins",
|
|
"frequency": "daily",
|
|
"assignment_type": "any_one",
|
|
"points": 3,
|
|
"room": "Kitchen",
|
|
"assigned_user_id": xander.id,
|
|
"status": "pending"
|
|
},
|
|
{
|
|
"title": "Tidy living room",
|
|
"description": "Pick up toys, straighten cushions, clear coffee table",
|
|
"frequency": "daily",
|
|
"assignment_type": "any_one",
|
|
"points": 5,
|
|
"room": "Living Room",
|
|
"assigned_user_id": william.id,
|
|
"status": "in_progress"
|
|
},
|
|
|
|
# Weekly chores
|
|
{
|
|
"title": "Vacuum entire house",
|
|
"description": "Vacuum all carpeted areas including stairs",
|
|
"frequency": "weekly",
|
|
"assignment_type": "any_one",
|
|
"points": 15,
|
|
"room": "Whole House",
|
|
"assigned_user_id": lou.id,
|
|
"status": "pending"
|
|
},
|
|
{
|
|
"title": "Clean bathrooms",
|
|
"description": "Clean toilets, sinks, mirrors, and mop floors",
|
|
"frequency": "weekly",
|
|
"assignment_type": "any_one",
|
|
"points": 20,
|
|
"room": "Bathrooms",
|
|
"assigned_user_id": jess.id,
|
|
"status": "completed",
|
|
"completed_at": datetime.now() - timedelta(days=1)
|
|
},
|
|
{
|
|
"title": "Mow the lawn",
|
|
"description": "Mow front and back yard, edge walkways",
|
|
"frequency": "weekly",
|
|
"assignment_type": "any_one",
|
|
"points": 25,
|
|
"room": "Yard",
|
|
"assigned_user_id": william.id,
|
|
"status": "pending"
|
|
},
|
|
|
|
# Monthly chores
|
|
{
|
|
"title": "Deep clean kitchen",
|
|
"description": "Clean oven, fridge, cabinets, and behind appliances",
|
|
"frequency": "monthly",
|
|
"assignment_type": "any_one",
|
|
"points": 50,
|
|
"room": "Kitchen",
|
|
"assigned_user_id": jess.id,
|
|
"status": "pending"
|
|
},
|
|
{
|
|
"title": "Wash windows",
|
|
"description": "Clean all interior and exterior windows",
|
|
"frequency": "monthly",
|
|
"assignment_type": "any_one",
|
|
"points": 40,
|
|
"room": "Whole House",
|
|
"assigned_user_id": lou.id,
|
|
"status": "pending"
|
|
},
|
|
{
|
|
"title": "Organize garage",
|
|
"description": "Sort items, sweep floor, arrange tools",
|
|
"frequency": "monthly",
|
|
"assignment_type": "any_one",
|
|
"points": 35,
|
|
"room": "Garage",
|
|
"assigned_user_id": william.id,
|
|
"status": "in_progress"
|
|
},
|
|
{
|
|
"title": "Change air filters",
|
|
"description": "Replace HVAC filters throughout house",
|
|
"frequency": "monthly",
|
|
"assignment_type": "any_one",
|
|
"points": 10,
|
|
"room": "Whole House",
|
|
"assigned_user_id": jess.id,
|
|
"status": "pending"
|
|
},
|
|
|
|
# On-trigger chores
|
|
{
|
|
"title": "Grocery shopping",
|
|
"description": "Weekly grocery shopping trip",
|
|
"frequency": "on_trigger",
|
|
"assignment_type": "any_one",
|
|
"points": 30,
|
|
"room": "Shopping",
|
|
"assigned_user_id": lou.id,
|
|
"status": "pending"
|
|
},
|
|
{
|
|
"title": "Car wash",
|
|
"description": "Wash and vacuum family car",
|
|
"frequency": "on_trigger",
|
|
"assignment_type": "any_one",
|
|
"points": 20,
|
|
"room": "Driveway",
|
|
"assigned_user_id": xander.id,
|
|
"status": "pending"
|
|
},
|
|
]
|
|
|
|
for chore_data in demo_chores:
|
|
chore = Chore(**chore_data)
|
|
db.add(chore)
|
|
|
|
# Pretty print status
|
|
status_marker = "[DONE]" if chore_data["status"] == "completed" else "[WIP]" if chore_data["status"] == "in_progress" else "[TODO]"
|
|
print(f" {status_marker} {chore_data['title']} - {chore_data['frequency']} ({chore_data['room']})")
|
|
|
|
db.commit()
|
|
print(f"\n[SUCCESS] Database initialized with {len(users_data)} users and {len(demo_chores)} demo chores!")
|
|
print("\nLogin credentials:")
|
|
print(" Username: jess (admin) or lou, william, xander, bella")
|
|
print(" Password: password123")
|
|
|
|
except Exception as e:
|
|
print(f"[ERROR] Error initializing database: {e}")
|
|
db.rollback()
|
|
raise
|
|
finally:
|
|
db.close()
|
|
|
|
if __name__ == "__main__":
|
|
init_db()
|