import os import json from app.database import engine, SessionLocal, Base from app.models import Question, AdminUser from app.auth import hash_password def seed_db(): Base.metadata.create_all(bind=engine) db = SessionLocal() try: # 1. Admin User admin_user = os.getenv("ADMIN_USER", "admin") admin_pass = os.getenv("ADMIN_PASS", "Alohomora2026!") existing_admin = db.query(AdminUser).filter(AdminUser.username == admin_user).first() if not existing_admin: db.add(AdminUser( username=admin_user, password_hash=hash_password(admin_pass) )) db.commit() print(f"Created default admin user: {admin_user}") # 2. Existing Questions from legacy/fragen.json legacy_questions_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "legacy", "fragen.json") if os.path.exists(legacy_questions_path): with open(legacy_questions_path, "r", encoding="utf-8") as f: legacy_questions = json.load(f) count = 0 for idx, q in enumerate(legacy_questions): # Check if question text already in DB existing = db.query(Question).filter(Question.question == q.get("frage", "").strip()).first() if not existing: db.add(Question( category="harry_potter", question=q.get("frage", "").strip(), canonical_answer=q.get("antwort", "").strip(), hint=q.get("tipp", "").strip(), status="active", sort_order=idx + 1, submitted_by="Admin" )) count += 1 db.commit() print(f"Imported {count} active questions from legacy/fragen.json") # 3. Existing Suggestions from legacy/fragen_vorschlaege.json legacy_sugg_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "legacy", "fragen_vorschlaege.json") if os.path.exists(legacy_sugg_path): with open(legacy_sugg_path, "r", encoding="utf-8") as f: legacy_sugg = json.load(f) count_sugg = 0 for q in legacy_sugg: existing = db.query(Question).filter(Question.question == q.get("frage", "").strip()).first() if not existing: db.add(Question( category="harry_potter", question=q.get("frage", "").strip(), canonical_answer=q.get("antwort", "").strip(), hint=q.get("tipp", "").strip(), status="needs_review", sort_order=999, submitted_by="Gast (Legacy Vorschlag)" )) count_sugg += 1 db.commit() print(f"Imported {count_sugg} pending suggestions from legacy/fragen_vorschlaege.json") finally: db.close() if __name__ == "__main__": seed_db()