Spaces:
Running on Zero
Running on Zero
Download app/data/seed_data.py from MustafaHussein/Saudi_Law_Vision: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/spaces/MustafaHussein/Saudi_Law_Vision/resolve/main/app/data/seed_data.py
- Command line
-
hf download hf://spaces/MustafaHussein/Saudi_Law_Vision/app/data/seed_data.py
-
curl -L -o seed_data.py https://huggingface.co/spaces/MustafaHussein/Saudi_Law_Vision/resolve/main/app/data/seed_data.py
4.39 kB
| """ | |
| Seed data for development and testing | |
| Creates initial data in the database | |
| """ | |
| from sqlalchemy.orm import Session | |
| from app.core.security import get_password_hash | |
| from app.models.user import User, UserRole | |
| from app.models.client import Client | |
| from app.core.database import SessionLocal | |
| def init_admin_user(db: Session) -> User: | |
| """Create default admin user""" | |
| admin = db.query(User).filter(User.email == "admin@legaltech.com").first() | |
| if not admin: | |
| admin = User( | |
| email="admin@legaltech.com", | |
| full_name="System Administrator", | |
| full_name_ar="مدير النظام", | |
| hashed_password=get_password_hash("admin123"), | |
| role=UserRole.ADMIN, | |
| is_active=True, | |
| is_superuser=True | |
| ) | |
| db.add(admin) | |
| db.commit() | |
| db.refresh(admin) | |
| print("✅ Created admin user: admin@legaltech.com / admin123") | |
| else: | |
| print("ℹ️ Admin user already exists") | |
| return admin | |
| def init_sample_users(db: Session): | |
| """Create sample users for testing""" | |
| users = [ | |
| { | |
| "email": "lawyer@legaltech.com", | |
| "full_name": "Ahmed AlSalem", | |
| "full_name_ar": "أحمد السالم", | |
| "password": "lawyer123", | |
| "role": UserRole.LAWYER | |
| }, | |
| { | |
| "email": "staff@legaltech.com", | |
| "full_name": "Sara AlOtaibi", | |
| "full_name_ar": "سارة العتيبي", | |
| "password": "staff123", | |
| "role": UserRole.STAFF | |
| }, | |
| { | |
| "email": "client@legaltech.com", | |
| "full_name": "Mohammed AlQahtani", | |
| "full_name_ar": "محمد القحطاني", | |
| "password": "client123", | |
| "role": UserRole.CLIENT | |
| } | |
| ] | |
| for user_data in users: | |
| existing = db.query(User).filter(User.email == user_data["email"]).first() | |
| if not existing: | |
| user = User( | |
| email=user_data["email"], | |
| full_name=user_data["full_name"], | |
| full_name_ar=user_data["full_name_ar"], | |
| hashed_password=get_password_hash(user_data["password"]), | |
| role=user_data["role"], | |
| is_active=True | |
| ) | |
| db.add(user) | |
| print(f"✅ Created user: {user_data['email']}") | |
| db.commit() | |
| def init_sample_clients(db: Session): | |
| """Create sample clients""" | |
| clients = [ | |
| { | |
| "full_name": "Abdullah AlMutairi", | |
| "full_name_ar": "عبدالله المطيري", | |
| "email": "abdullah@example.com", | |
| "phone": "+966501234567", | |
| "national_id": "1234567890", | |
| "address": "Riyadh, Saudi Arabia" | |
| }, | |
| { | |
| "full_name": "Fatima AlHarbi", | |
| "full_name_ar": "فاطمة الحربي", | |
| "email": "fatima@example.com", | |
| "phone": "+966507654321", | |
| "national_id": "2345678901", | |
| "address": "Jeddah, Saudi Arabia" | |
| } | |
| ] | |
| for client_data in clients: | |
| existing = db.query(Client).filter(Client.email == client_data["email"]).first() | |
| if not existing: | |
| client = Client(**client_data) | |
| db.add(client) | |
| print(f"✅ Created client: {client_data['full_name']}") | |
| db.commit() | |
| def seed_database(): | |
| """ | |
| Main function to seed the database | |
| Run this to populate database with initial data | |
| """ | |
| print("=" * 60) | |
| print("SEEDING DATABASE") | |
| print("=" * 60) | |
| db = SessionLocal() | |
| try: | |
| # Create admin and sample users | |
| init_admin_user(db) | |
| init_sample_users(db) | |
| # Create sample clients | |
| init_sample_clients(db) | |
| print("=" * 60) | |
| print("✅ DATABASE SEEDING COMPLETE") | |
| print("=" * 60) | |
| print("\nLogin credentials:") | |
| print(" Admin: admin@legaltech.com / admin123") | |
| print(" Lawyer: lawyer@legaltech.com / lawyer123") | |
| print(" Staff: staff@legaltech.com / staff123") | |
| print(" Client: client@legaltech.com / client123") | |
| print() | |
| except Exception as e: | |
| print(f"❌ Error seeding database: {e}") | |
| db.rollback() | |
| finally: | |
| db.close() | |
| if __name__ == "__main__": | |
| seed_database() | |