#!/usr/bin/env python3 """ Script to create demo data for Team Chat application """ import requests import sys BASE_URL = "http://localhost:8000" def create_demo_data(): print("šŸš€ Creating demo data for Team Chat...") # 1. Register users print("\n1ļøāƒ£ Registering users...") users = [ {"username": "alice", "email": "alice@example.com", "password": "pass123", "full_name": "Alice Smith"}, {"username": "bob", "email": "bob@example.com", "password": "pass123", "full_name": "Bob Jones"}, {"username": "charlie", "email": "charlie@example.com", "password": "pass123", "full_name": "Charlie Brown"}, ] for user in users: try: response = requests.post(f"{BASE_URL}/auth/register", json=user) if response.status_code == 201: print(f" āœ… Created user: {user['username']}") else: print(f" āš ļø User {user['username']} already exists") except Exception as e: print(f" āŒ Error creating user {user['username']}: {e}") # 2. Login as alice print("\n2ļøāƒ£ Logging in as alice...") try: response = requests.post( f"{BASE_URL}/auth/login", json={"username": "alice", "password": "pass123"} ) token = response.json()["access_token"] headers = {"Authorization": f"Bearer {token}"} print(" āœ… Logged in successfully") except Exception as e: print(f" āŒ Login failed: {e}") return # 3. Create departments print("\n3ļøāƒ£ Creating departments...") departments = [ {"name": "Engineering", "description": "Engineering Team"}, {"name": "Marketing", "description": "Marketing Team"}, {"name": "HR", "description": "Human Resources"}, ] dept_ids = [] for dept in departments: try: response = requests.post(f"{BASE_URL}/departments/", json=dept, headers=headers) if response.status_code == 201: dept_id = response.json()["id"] dept_ids.append(dept_id) print(f" āœ… Created department: {dept['name']} (ID: {dept_id})") else: print(f" āš ļø Department {dept['name']} might already exist") except Exception as e: print(f" āŒ Error creating department {dept['name']}: {e}") # 4. Add users to departments print("\n4ļøāƒ£ Adding users to departments...") # Alice (user_id 1) → Engineering (dept 1) # Bob (user_id 2) → Engineering (dept 1) # Charlie (user_id 3) → Marketing (dept 2) user_dept_assignments = [ (1, 1), # Alice → Engineering (2, 1), # Bob → Engineering (3, 2), # Charlie → Marketing ] for user_id, dept_id in user_dept_assignments: try: response = requests.post( f"{BASE_URL}/departments/{dept_id}/users/{user_id}", headers=headers ) if response.status_code == 200: print(f" āœ… Added user {user_id} to department {dept_id}") except Exception as e: print(f" āš ļø Error assigning user {user_id} to dept {dept_id}: {e}") # 5. Create channels print("\n5ļøāƒ£ Creating channels...") channels = [ {"name": "general", "description": "General discussion", "department_id": 1}, {"name": "tech-talk", "description": "Technical discussions", "department_id": 1}, {"name": "campaigns", "description": "Marketing campaigns", "department_id": 2}, ] for channel in channels: try: response = requests.post(f"{BASE_URL}/channels/", json=channel, headers=headers) if response.status_code == 201: print(f" āœ… Created channel: {channel['name']}") except Exception as e: print(f" āŒ Error creating channel {channel['name']}: {e}") # 6. Create messages print("\n6ļøāƒ£ Creating sample messages...") messages = [ {"content": "Welcome to Team Chat! šŸ‘‹", "channel_id": 1}, {"content": "This is our general discussion channel.", "channel_id": 1}, {"content": "Feel free to share ideas and collaborate!", "channel_id": 1}, ] for msg in messages: try: response = requests.post(f"{BASE_URL}/messages/", json=msg, headers=headers) if response.status_code == 201: print(f" āœ… Created message") except Exception as e: print(f" āš ļø Error creating message: {e}") # 7. Create snippets print("\n7ļøāƒ£ Creating code snippets...") snippets = [ { "title": "FastAPI Hello World", "language": "python", "content": """from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"}""", "tags": "fastapi, python, api", "visibility": "organization" }, { "title": "PostgreSQL Connection", "language": "python", "content": """import psycopg2 conn = psycopg2.connect( host="localhost", database="mydb", user="user", password="password" )""", "tags": "postgresql, database, python", "visibility": "organization" }, { "title": "React useState Hook", "language": "javascript", "content": """import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return (

Count: {count}

); }""", "tags": "react, javascript, hooks", "visibility": "organization" }, { "title": "SQL Select with Join", "language": "sql", "content": """SELECT u.username, d.name as department_name, c.name as channel_name FROM users u JOIN user_department ud ON u.id = ud.user_id JOIN departments d ON ud.department_id = d.id JOIN channels c ON c.department_id = d.id WHERE u.is_active = true;""", "tags": "sql, join, query", "visibility": "department", "department_id": 1 } ] for snippet in snippets: try: response = requests.post(f"{BASE_URL}/snippets/", json=snippet, headers=headers) if response.status_code == 201: print(f" āœ… Created snippet: {snippet['title']}") except Exception as e: print(f" āŒ Error creating snippet {snippet['title']}: {e}") print("\nāœ… Demo data creation complete!") print("\nšŸ“ Login credentials:") print(" Username: alice, bob, or charlie") print(" Password: pass123") print("\n🌐 Access the app at: http://localhost:5173") if __name__ == "__main__": try: create_demo_data() except KeyboardInterrupt: print("\n\nāš ļø Aborted by user") sys.exit(1) except Exception as e: print(f"\n\nāŒ Fatal error: {e}") sys.exit(1)