Files
odoo_source/addons/website_jitsi/models/chat_room_mixin.py
T
Anjali 184c3acc81 [IMP] website_{event_meet, jitsi}: set default value for 'Max Capacity'
Before this commit:
When user tries to create an event meeting room without selecting
'Max Capacity', it throws a Validation Error because 'Max Capacity'
is a required field.

After this commit:
We provide the default value for 'Max Capicity' field on event meeting
room to avoid default ORM Validation Error while saving the record.
Note: Because the 'Max Capacity' is a selection field,  displayed with
radio widget, we can't make it required from the view unlike other fields.
Also, in the 'chat.room.mixin', when we create a chat room on the fly from
create method, instead of popping the ROOM_CONFIG_FIELDS, we propagate them
(same like in write method), so that the room being created respects value
selected by user (instead of taking the default one if we pop it).

Task-id : 2448435

closes odoo/odoo#72756

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2021-06-28 13:56:31 +00:00

78 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import re
from odoo import api, fields, models
from odoo.tools import remove_accents
class ChatRoomMixin(models.AbstractModel):
"""Add the chat room configuration (`chat.room`) on the needed models.
The chat room configuration contains all information about the room. So, we store
all the chat room logic at the same place, for all models.
Embed chat room related fields prefixed with `room_`.
"""
_name = "chat.room.mixin"
_description = "Chat Room Mixin"
ROOM_CONFIG_FIELDS = [
('room_name', 'name'),
('room_lang_id', 'lang_id'),
('room_max_capacity', 'max_capacity'),
('room_participant_count', 'participant_count')
]
chat_room_id = fields.Many2one("chat.room", "Chat Room", readonly=True, copy=False, ondelete="set null")
# chat room related fields
room_name = fields.Char("Room Name", related="chat_room_id.name")
room_is_full = fields.Boolean("Room Is Full", related="chat_room_id.is_full")
room_lang_id = fields.Many2one("res.lang", "Language", related="chat_room_id.lang_id", readonly=False)
room_max_capacity = fields.Selection(string="Max capacity", related="chat_room_id.max_capacity", readonly=False, required=True)
room_participant_count = fields.Integer("Participant count", related="chat_room_id.participant_count", readonly=False)
room_last_activity = fields.Datetime("Last activity", related="chat_room_id.last_activity")
room_max_participant_reached = fields.Integer("Peak participants", related="chat_room_id.max_participant_reached")
@api.model_create_multi
def create(self, values_list):
for values in values_list:
if any(values.get(fmatch[0]) for fmatch in self.ROOM_CONFIG_FIELDS) and not values.get('chat_room_id'):
if values.get('room_name'):
values['room_name'] = self._jitsi_sanitize_name(values['room_name'])
room_values = dict((fmatch[1], values[fmatch[0]]) for fmatch in self.ROOM_CONFIG_FIELDS if values.get(fmatch[0]))
values['chat_room_id'] = self.env['chat.room'].create(room_values).id
return super(ChatRoomMixin, self).create(values_list)
def write(self, values):
if any(values.get(fmatch[0]) for fmatch in self.ROOM_CONFIG_FIELDS):
if values.get('room_name'):
values['room_name'] = self._jitsi_sanitize_name(values['room_name'])
for document in self.filtered(lambda doc: not doc.chat_room_id):
room_values = dict((fmatch[1], values[fmatch[0]]) for fmatch in self.ROOM_CONFIG_FIELDS if values.get(fmatch[0]))
document.chat_room_id = self.env['chat.room'].create(room_values).id
return super(ChatRoomMixin, self).write(values)
def copy_data(self, default=None):
if default is None:
default = {}
if self.chat_room_id:
chat_room_default = {}
if 'room_name' not in default:
chat_room_default['name'] = self._jitsi_sanitize_name(self.chat_room_id.name)
default['chat_room_id'] = self.chat_room_id.copy(default=chat_room_default).id
return super(ChatRoomMixin, self).copy_data(default=default)
def unlink(self):
rooms = self.chat_room_id
res = super(ChatRoomMixin, self).unlink()
rooms.unlink()
return res
def _jitsi_sanitize_name(self, name):
sanitized = re.sub(r'[^\w+.]+', '-', remove_accents(name).lower())
counter, sanitized_suffixed = 1, sanitized
existing = self.env['chat.room'].search([('name', '=like', '%s%%' % sanitized)]).mapped('name')
while sanitized_suffixed in existing:
sanitized_suffixed = '%s-%d' % (sanitized, counter)
counter += 1
return sanitized_suffixed