#!/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}