Files
encoach_backend_v4/custom_addons/encoach_ai/controllers/media_controller.py
Yamen Ahmad 6ec68160c8 feat: institutional + support + training admin sections (backend + frontend)
Ship three fully-wired admin areas end-to-end with APIs, seeds, tests and docs.

Backend (new `encoach_lms_api` addon + existing addons):
- Institutional: academic years/terms, departments, admission registers & admissions,
  courses/batches, lessons, fees (terms + student fees + invoicing with income-account
  auto-wiring), gradebook (assignments/grades), library, facilities (encoach.asset),
  student leave, result templates + marksheets (incl. delete-with-cascade).
- Support: `encoach.ticket` model + CRUD/assignee routes; payment records derived
  from `op.student.fees.details` and `account.move`; platform settings backed by
  `encoach.code` and `ir.config_parameter` (packages + grading config).
- Training: `encoach.vocab.item` + `encoach.grammar.rule` (plus progress models)
  with CRUD, pagination, search/level filters, and upsert-style progress endpoints.
  Odoo 19 compatibility: `_sql_constraints` replaced with `@api.constrains`;
  `ValidationError`/`UserError` mapped to HTTP 400.

Frontend:
- Rewire institutional admin pages (Academic Year Manager, Admissions, Courses,
  Lessons, Fees, Gradebook, Library, Facilities, Student Leave, Marksheets,
  Taxonomy, Resources) to real APIs with React Query invalidation and dialogs.
- New typed services: `payments.service.ts`, `platformSettings.service.ts`,
  `training.service.ts`. Updated `fees/gradebook/lms/courseware/taxonomy/
  resources/student-progress/generation` services + related types.
- Rewrite `VocabularyPage`, `GrammarPage`, `PaymentRecordPage`, `SettingsPage`,
  `TicketsPage` to consume live data with search/filter/progress/CRUD flows.
- New shared components: `TaxonomyCascade`, `MaterialViewer`, `teacher/TeacherLibrary`.
- Favicons/branding assets and misc. UX polish across teacher/student pages.

Tooling & QA:
- Seeders: `seed_demo.py`, `seed_demo_data.py`, `seed_institutional.py` (idempotent,
  covers institutional + support + training fixtures incl. income-account wiring).
- API write-flow test suites: `test_write_flows.py` (institutional),
  `test_support_flows.py` (support), `test_training_flows.py` (training),
  `test_ai_full.py`. All suites pass end-to-end.
- Docs: add `docs/PROJECT_SUMMARY.md` with per-section scope, artifacts and QA.
- `.gitignore`: ignore `pgdata_bak_*/`, `frontend/.vite/`, `frontend/dist/`,
  `frontend/node_modules/`.

Made-with: Cursor
2026-04-19 03:13:23 +04:00

149 lines
6.4 KiB
Python

"""REST endpoints for AI media generation — TTS, avatar videos."""
import base64
import json
import logging
from odoo import http
from odoo.http import request, Response
_logger = logging.getLogger(__name__)
def _json_response(data, status=200):
return Response(json.dumps(data, default=str), status=status, content_type="application/json")
def _get_json():
try:
return json.loads(request.httprequest.data or "{}")
except Exception:
return {}
class MediaController(http.Controller):
"""Handles /api/exam/*/media and avatar endpoints from media.service.ts."""
def _get_tts_provider(self):
return request.env["ir.config_parameter"].sudo().get_param("encoach_ai.tts_provider", "polly")
def _get_tts(self):
"""Get the configured TTS provider."""
provider = self._get_tts_provider()
if provider == "elevenlabs":
from odoo.addons.encoach_ai.services.elevenlabs_service import ElevenLabsService
return ElevenLabsService(request.env)
from odoo.addons.encoach_ai.services.polly_service import PollyService
return PollyService(request.env)
def _synthesize(self, text, body):
"""Dispatch TTS call with correct kwargs for each provider."""
tts = self._get_tts()
provider = self._get_tts_provider()
if provider == "elevenlabs":
gender = body.get("gender", "female")
language = body.get("language", "en-GB")
voice_key = f"{gender}_{'british' if 'GB' in language else 'american'}"
return tts.synthesize(text, voice_id=body.get("voice_id"), voice_key=voice_key)
return tts.synthesize(
text,
voice=body.get("voice"),
language=body.get("language", "en-GB"),
gender=body.get("gender", "female"),
)
# ── POST /api/exam/listening/media — generate listening audio ──
@http.route("/api/exam/listening/media", type="http", auth="public", methods=["POST"], csrf=False)
def listening_media(self, **kw):
body = _get_json()
text = body.get("text", "")
if not text:
return _json_response({"error": "No text provided"}, 400)
try:
result = self._synthesize(text, body)
audio_b64 = base64.b64encode(result["audio"]).decode()
return _json_response({
"audio_base64": audio_b64,
"content_type": result["content_type"],
"voice": result.get("voice") or result.get("voice_id"),
"characters": result["characters"],
})
except Exception as e:
_logger.exception("Listening media generation failed")
return _json_response({"error": str(e)}, 500)
# ── POST /api/exam/speaking/media — generate speaking prompt audio ──
@http.route("/api/exam/speaking/media", type="http", auth="public", methods=["POST"], csrf=False)
def speaking_media(self, **kw):
body = _get_json()
text = body.get("text", "")
if not text:
return _json_response({"error": "No text provided"}, 400)
try:
result = self._synthesize(text, body)
audio_b64 = base64.b64encode(result["audio"]).decode()
return _json_response({
"audio_base64": audio_b64,
"content_type": result["content_type"],
})
except Exception as e:
return _json_response({"error": str(e)}, 500)
# ── GET /api/exam/avatars — list ELAI avatars ──
@http.route("/api/exam/avatars", type="http", auth="public", methods=["GET"], csrf=False)
def list_avatars(self, **kw):
try:
from odoo.addons.encoach_ai.services.elai_service import ElaiService
elai = ElaiService(request.env)
avatars = elai.list_avatars()
return _json_response({"avatars": avatars})
except Exception as e:
return _json_response({"avatars": [], "note": str(e)})
# ── POST /api/exam/avatar/video — create avatar video ──
@http.route("/api/exam/avatar/video", type="http", auth="public", methods=["POST"], csrf=False)
def create_avatar_video(self, **kw):
body = _get_json()
try:
from odoo.addons.encoach_ai.services.elai_service import ElaiService
elai = ElaiService(request.env)
result = elai.create_video(
body.get("script", ""),
avatar_id=body.get("avatar_id"),
title=body.get("title", "EnCoach Video"),
)
return _json_response(result)
except Exception as e:
return _json_response({"error": str(e)}, 500)
# ── GET /api/exam/avatar/video/:id — check video status ──
@http.route("/api/exam/avatar/video/<string:video_id>", type="http", auth="public", methods=["GET"], csrf=False)
def video_status(self, video_id, **kw):
try:
from odoo.addons.encoach_ai.services.elai_service import ElaiService
elai = ElaiService(request.env)
return _json_response(elai.get_video_status(video_id))
except Exception as e:
return _json_response({"video_id": video_id, "status": "error", "error": str(e)})
# ── POST /api/courses/ai-generate — AiCreationAssistant.tsx ──
@http.route("/api/courses/ai-generate", type="http", auth="public", methods=["POST"], csrf=False)
def ai_generate_course(self, **kw):
body = _get_json()
try:
from odoo.addons.encoach_ai.services.openai_service import OpenAIService
ai = OpenAIService(request.env)
messages = [
{"role": "system", "content": (
"Generate a complete course structure. Return JSON: "
"{\"title\": string, \"description\": string, \"modules\": "
"[{\"title\": string, \"skill\": string, \"estimated_hours\": number, "
"\"topics\": [string], \"resources\": [{\"title\": string, \"type\": string}]}], "
"\"duration_weeks\": number}"
)},
{"role": "user", "content": json.dumps(body)},
]
result = ai.chat_json(messages, action="generate_course", max_tokens=4096)
return _json_response(result)
except Exception as e:
return _json_response({"error": str(e)}, 500)