123 lines
4.5 KiB
Python
123 lines
4.5 KiB
Python
import logging
|
|
|
|
from odoo import http
|
|
from odoo.http import request
|
|
|
|
from .base import EncoachMixin
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ClassroomsController(EncoachMixin, http.Controller):
|
|
|
|
# --------------------------------------------------------------- list
|
|
@http.route('/api/groups', type='http', auth='public', methods=['GET', 'OPTIONS'], csrf=False, cors='*')
|
|
def list_groups(self, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
|
|
domain = []
|
|
participant = kwargs.get('participant')
|
|
if participant:
|
|
domain.append(('participant_ids', 'in', [int(participant)]))
|
|
|
|
admin = kwargs.get('admin')
|
|
if admin:
|
|
domain.append(('admin_id', '=', int(admin)))
|
|
|
|
entity_id = kwargs.get('entityID')
|
|
if entity_id:
|
|
domain.append(('entity_id', '=', int(entity_id)))
|
|
|
|
groups = request.env['encoach.group'].sudo().search(domain, order='name asc')
|
|
return self._json_response([self._serialize(g) for g in groups])
|
|
except Exception:
|
|
_logger.exception("List groups error")
|
|
return self._error_response('Internal server error', 500)
|
|
|
|
# -------------------------------------------------------------- create
|
|
@http.route('/api/groups', type='http', auth='public', methods=['POST', 'OPTIONS'], csrf=False, cors='*')
|
|
def create_group(self, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
|
|
body = self._get_json_body()
|
|
if not body.get('name'):
|
|
return self._error_response('Group name is required', 400)
|
|
|
|
vals = {
|
|
'name': body['name'],
|
|
'admin_id': user.id,
|
|
}
|
|
if body.get('entityID'):
|
|
vals['entity_id'] = int(body['entityID'])
|
|
|
|
participant_ids = body.get('participants', [])
|
|
if participant_ids:
|
|
vals['participant_ids'] = [(4, int(pid)) for pid in participant_ids]
|
|
|
|
group = request.env['encoach.group'].sudo().create(vals)
|
|
return self._json_response(self._serialize(group), status=201)
|
|
except Exception:
|
|
_logger.exception("Create group error")
|
|
return self._error_response('Internal server error', 500)
|
|
|
|
# -------------------------------------------------------------- update
|
|
@http.route(
|
|
'/api/groups/<int:group_id>', type='http', auth='public',
|
|
methods=['PATCH', 'OPTIONS'], csrf=False, cors='*',
|
|
)
|
|
def update_group(self, group_id, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
|
|
group = request.env['encoach.group'].sudo().browse(group_id)
|
|
if not group.exists():
|
|
return self._error_response('Group not found', 404)
|
|
|
|
body = self._get_json_body()
|
|
vals = {}
|
|
if 'name' in body:
|
|
vals['name'] = body['name']
|
|
|
|
if 'participants' in body:
|
|
vals['participant_ids'] = [(6, 0, [int(pid) for pid in body['participants']])]
|
|
|
|
if 'admins' in body and body['admins']:
|
|
vals['admin_id'] = int(body['admins'][0])
|
|
|
|
if vals:
|
|
group.write(vals)
|
|
|
|
return self._json_response(self._serialize(group))
|
|
except Exception:
|
|
_logger.exception("Update group error")
|
|
return self._error_response('Internal server error', 500)
|
|
|
|
# -------------------------------------------------------------- delete
|
|
@http.route(
|
|
'/api/groups/<int:group_id>', type='http', auth='public',
|
|
methods=['DELETE', 'OPTIONS'], csrf=False, cors='*',
|
|
)
|
|
def delete_group(self, group_id, **kwargs):
|
|
try:
|
|
user = self._authenticate()
|
|
if not user:
|
|
return self._error_response('Unauthorized', 401)
|
|
|
|
group = request.env['encoach.group'].sudo().browse(group_id)
|
|
if not group.exists():
|
|
return self._error_response('Group not found', 404)
|
|
|
|
group.unlink()
|
|
return self._json_response({'ok': True})
|
|
except Exception:
|
|
_logger.exception("Delete group error")
|
|
return self._error_response('Internal server error', 500)
|