import json import logging from odoo import http from odoo.http import request from .base import EncoachMixin _logger = logging.getLogger(__name__) class AssignmentsController(EncoachMixin, http.Controller): # --------------------------------------------------------------- list @http.route('/api/assignments', type='http', auth='public', methods=['GET', 'OPTIONS'], csrf=False, cors='*') def list_assignments(self, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) domain = [] group_id = kwargs.get('groupId') if group_id: domain.append(('group_id', '=', int(group_id))) creator = kwargs.get('createdBy') if creator: domain.append(('create_uid', '=', int(creator))) status = kwargs.get('status') if status: domain.append(('status', '=', status)) participant = kwargs.get('participant') if participant: domain.append(('group_id.participant_ids', 'in', [int(participant)])) is_archived = kwargs.get('isArchived') if is_archived is not None: domain.append(('is_archived', '=', is_archived in ('true', '1', True))) offset, limit, page = self._paginate_params(kwargs) Assignment = request.env['encoach.assignment'].sudo() total = Assignment.search_count(domain) records = Assignment.search(domain, offset=offset, limit=limit, order='create_date desc') return self._json_response({ 'assignments': [self._serialize(a) for a in records], 'total': total, 'page': page, 'limit': limit, }) except Exception: _logger.exception("List assignments error") return self._error_response('Internal server error', 500) # -------------------------------------------------------------- create @http.route('/api/assignments', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*') def create_assignment(self, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) body = self._get_json_body() if not body.get('examId'): return self._error_response('examId is required', 400) if not body.get('groupId'): return self._error_response('groupId is required', 400) vals = { 'name': body.get('name', ''), 'exam_id': int(body['examId']), 'group_id': int(body['groupId']), 'status': 'draft', } if body.get('startDate'): vals['start_date'] = body['startDate'] if body.get('endDate'): vals['end_date'] = body['endDate'] if body.get('duration'): vals['duration'] = int(body['duration']) assignment = request.env['encoach.assignment'].sudo().with_user(user).create(vals) return self._json_response(self._serialize(assignment), status=201) except Exception: _logger.exception("Create assignment error") return self._error_response('Internal server error', 500) # -------------------------------------------------------------- update @http.route( '/api/assignments/', type='http', auth='public', methods=['PATCH', 'OPTIONS'], csrf=False, cors='*', ) def update_assignment(self, aid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) assignment = request.env['encoach.assignment'].sudo().browse(aid) if not assignment.exists(): return self._error_response('Assignment not found', 404) body = self._get_json_body() vals = {} if 'name' in body: vals['name'] = body['name'] if 'startDate' in body: vals['start_date'] = body['startDate'] if 'endDate' in body: vals['end_date'] = body['endDate'] if 'duration' in body: vals['duration'] = int(body['duration']) if 'examId' in body: vals['exam_id'] = int(body['examId']) if 'groupId' in body: vals['group_id'] = int(body['groupId']) if 'status' in body: vals['status'] = body['status'] if vals: assignment.write(vals) return self._json_response(self._serialize(assignment)) except Exception: _logger.exception("Update assignment error") return self._error_response('Internal server error', 500) # -------------------------------------------------------------- delete @http.route( '/api/assignments/', type='http', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False, cors='*', ) def delete_assignment(self, aid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) assignment = request.env['encoach.assignment'].sudo().browse(aid) if not assignment.exists(): return self._error_response('Assignment not found', 404) assignment.unlink() return self._json_response({'ok': True}) except Exception: _logger.exception("Delete assignment error") return self._error_response('Internal server error', 500) # --------------------------------------------------------------- start @http.route( '/api/assignments//start', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*', ) def start_assignment(self, aid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) assignment = request.env['encoach.assignment'].sudo().browse(aid) if not assignment.exists(): return self._error_response('Assignment not found', 404) assignment.action_start() return self._json_response(self._serialize(assignment)) except Exception as exc: _logger.exception("Start assignment error") return self._error_response(str(exc) if str(exc) != '' else 'Internal server error', 500) # ------------------------------------------------------------- release @http.route( '/api/assignments//release', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*', ) def release_assignment(self, aid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) assignment = request.env['encoach.assignment'].sudo().browse(aid) if not assignment.exists(): return self._error_response('Assignment not found', 404) assignment.action_release() return self._json_response(self._serialize(assignment)) except Exception as exc: _logger.exception("Release assignment error") return self._error_response(str(exc) if str(exc) != '' else 'Internal server error', 500) # ------------------------------------------------------------- archive @http.route( '/api/assignments//archive', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*', ) def archive_assignment(self, aid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) assignment = request.env['encoach.assignment'].sudo().browse(aid) if not assignment.exists(): return self._error_response('Assignment not found', 404) assignment.write({'is_archived': True}) return self._json_response(self._serialize(assignment)) except Exception: _logger.exception("Archive assignment error") return self._error_response('Internal server error', 500) # ----------------------------------------------------------- unarchive @http.route( '/api/assignments//unarchive', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*', ) def unarchive_assignment(self, aid, **kwargs): try: user = self._authenticate() if not user: return self._error_response('Unauthorized', 401) assignment = request.env['encoach.assignment'].sudo().browse(aid) if not assignment.exists(): return self._error_response('Assignment not found', 404) assignment.write({'is_archived': False}) return self._json_response(self._serialize(assignment)) except Exception: _logger.exception("Unarchive assignment error") return self._error_response('Internal server error', 500)