#!/usr/bin/env python3 """ wire_eduintel.py Wires EduIntel into the Morutabana teacher dashboard. Edits 3 files: views_teacher.py, urls.py, teacher_hub.html Usage: cd ~/leotsha_project python3 wire_eduintel.py """ import re from pathlib import Path BASE = Path("/home/sediba/leotsha_project") # ── 1. Add eduintel view to views_teacher.py ───────────────── VIEWS_FILE = BASE / "leotsha/views_teacher.py" views_content = VIEWS_FILE.read_text() EDUINTEL_VIEW = ''' # ── EduIntel API ───────────────────────────────────────────── def teacher_eduintel(request): """API endpoint: teacher asks EduIntel a curriculum question.""" from django.http import JsonResponse import subprocess, os, re as _re teacher = get_teacher(request) if not teacher: return JsonResponse({"error": "not authenticated"}, status=401) query = request.GET.get("q", "").strip() if not query: return JsonResponse({"error": "no query"}) try: result = subprocess.run( ["python3", "eduintel_generate.py", query], capture_output=True, text=True, timeout=120, cwd="/home/sediba/leotsha_project", env={**os.environ}, ) output = result.stdout # Extract just the answer (between ─── separator and final ━━━) sep = "─" * 20 end = "━" * 20 if sep in output: answer = output.split(sep)[-1] answer = answer.split(end)[0].strip() else: answer = output.strip() if not answer: answer = "EduIntel could not find relevant content. Try rephrasing." except subprocess.TimeoutExpired: answer = "EduIntel is processing. Please try a shorter question." except Exception as e: answer = f"Error: {str(e)}" return JsonResponse({"answer": answer, "query": query}) ''' if "def teacher_eduintel" in views_content: print(" ⏭ teacher_eduintel already in views_teacher.py") else: VIEWS_FILE.write_text(views_content + EDUINTEL_VIEW) print(" ✓ teacher_eduintel added to views_teacher.py") # ── 2. Add URL route to leotsha/urls.py ────────────────────── URLS_FILE = BASE / "leotsha/urls.py" urls_content = URLS_FILE.read_text() OLD_URL = ' path("leave/", t.learner_leave, name="learner_leave"),' NEW_URL = ''' path("leave/", t.learner_leave, name="learner_leave"), path("teacher/eduintel/", t.teacher_eduintel, name="teacher_eduintel"),''' if "teacher_eduintel" in urls_content: print(" ⏭ teacher_eduintel already in urls.py") elif OLD_URL in urls_content: URLS_FILE.write_text(urls_content.replace(OLD_URL, NEW_URL)) print(" ✓ URL route added to leotsha/urls.py") else: print(" ⚠ Could not find anchor in urls.py — adding at end") URLS_FILE.write_text(urls_content.rstrip() + "\n # EduIntel\n path(\"teacher/eduintel/\", t.teacher_eduintel, name=\"teacher_eduintel\"),\n]\n") print(" ✓ URL route added to end of urls.py") # ── 3. Add EduIntel section to teacher_hub.html ────────────── TEMPLATE_FILE = BASE / "templates/teacher_hub.html" template_content = TEMPLATE_FILE.read_text() EDUINTEL_HTML = '''