Files
Talal Sharabi fa538a1f12 Fix remaining controller serialization and model mismatches
- Replace all _serialize_group/exam/assignment/stat/session calls with _serialize
- Fix sessions controller to use exam_data JSON field instead of nonexistent columns
- Fix stats controller to use correct model fields (score_correct/total/missing, solutions)

Made-with: Cursor
2026-03-15 01:12:17 +04:00

64 lines
2.2 KiB
Python

import json
import logging
from odoo import http
from odoo.http import request
from .base import EncoachMixin
_logger = logging.getLogger(__name__)
class SessionsController(EncoachMixin, http.Controller):
# -------------------------------------------------------------- save
@http.route('/api/sessions', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*')
def save_session(self, **kwargs):
try:
user = self._authenticate()
if not user:
return self._error_response('Unauthorized', 401)
body = self._get_json_body()
session_id = body.get('_id') or body.get('id')
Session = request.env['encoach.session'].sudo()
exam_data = {k: v for k, v in body.items() if k not in ('_id', 'id')}
if session_id:
record = Session.browse(int(session_id))
if record.exists():
record.write({'exam_data': exam_data})
return self._json_response(self._serialize(record))
record = Session.create({
'user_id': user.id,
'exam_data': exam_data,
})
return self._json_response(self._serialize(record), status=201)
except Exception:
_logger.exception("Save session error")
return self._error_response('Internal server error', 500)
# ------------------------------------------------------------- delete
@http.route(
'/api/sessions/<int:sid>', type='http', auth='public',
methods=['DELETE', 'OPTIONS'], csrf=False, cors='*',
)
def delete_session(self, sid, **kwargs):
try:
user = self._authenticate()
if not user:
return self._error_response('Unauthorized', 401)
session = request.env['encoach.session'].sudo().browse(sid)
if not session.exists():
return self._error_response('Session not found', 404)
session.unlink()
return self._json_response({'ok': True})
except Exception:
_logger.exception("Delete session error")
return self._error_response('Internal server error', 500)