From c04b9c019eb4c04d62d1d90b810c5407d8a9b5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= Date: Thu, 3 Jan 2019 08:19:21 +0000 Subject: [PATCH 1/4] [REF] im_livechat: rework some 'im_livechat.channel' model methods Purpose ======= Several methods of the 'im_livechat.channel' model were passed a 'channel_id' to work on. This has been changed so that the caller can use those methods on an instance of this model instead. Some methods have also been switched to private because they had no apparent reasons to be public. This is a preliminary cleaning for task #1919871 Specicial note for the "loader" template: To load the livechat assets in a website page, the 'loader' template of livechat is directly called (instead of being returned through a controller) in order to avoid a new call to server. As this commit moves 'sudo' to make method callable on the record directly, it still needs to be sudo. First solution was to add the 'sudo' in the template, which is a bad practise. This commit creates a proxy method on website model returning the livechat info with 'sudo'. This avoid having the 'sudo' done in template. Like always, explicit is better than implicit. Task-1919871 --- addons/im_livechat/controllers/main.py | 7 ++-- .../im_livechat/models/im_livechat_channel.py | 40 +++++++++---------- .../views/im_livechat_channel_templates.xml | 4 -- addons/website_livechat/models/website.py | 12 +++++- .../views/website_livechat.xml | 2 +- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/addons/im_livechat/controllers/main.py b/addons/im_livechat/controllers/main.py index b3783d2f59d..d87cf2329cb 100644 --- a/addons/im_livechat/controllers/main.py +++ b/addons/im_livechat/controllers/main.py @@ -49,13 +49,12 @@ class LivechatController(http.Controller): def loader(self, channel_id, **kwargs): username = kwargs.get("username", _("Visitor")) channel = request.env['im_livechat.channel'].sudo().browse(channel_id) - info = request.env['im_livechat.channel'].get_livechat_info(channel.id, username=username) + info = channel.get_livechat_info(username=username) return request.render('im_livechat.loader', {'info': info, 'web_session_required': True}, headers=[('Content-Type', 'application/javascript')]) @http.route('/im_livechat/init', type='json', auth="public", cors="*") def livechat_init(self, channel_id): - LivechatChannel = request.env['im_livechat.channel'] - available = len(LivechatChannel.browse(channel_id).get_available_users()) + available = len(request.env['im_livechat.channel'].sudo().browse(channel_id)._get_available_users()) rule = {} if available: # find the country from the request @@ -95,7 +94,7 @@ class LivechatController(http.Controller): country = request.env['res.country'].sudo().search([('code', '=', country_code)], limit=1) if country_code else None if country: anonymous_name, country_id = _("%s (%s)") % (anonymous_name, country.name), country.id - return request.env["im_livechat.channel"].with_context(lang=False).get_mail_channel(channel_id, anonymous_name, user_id, country_id) + return request.env["im_livechat.channel"].with_context(lang=False).sudo().browse(channel_id)._get_mail_channel(anonymous_name, user_id, country_id) @http.route('/im_livechat/feedback', type='json', auth='public', cors="*") def feedback(self, uuid, rate, reason=None, **kwargs): diff --git a/addons/im_livechat/models/im_livechat_channel.py b/addons/im_livechat/models/im_livechat_channel.py index df57a6c770e..acba917036c 100644 --- a/addons/im_livechat/models/im_livechat_channel.py +++ b/addons/im_livechat/models/im_livechat_channel.py @@ -124,27 +124,26 @@ class ImLivechatChannel(models.Model): # Channel Methods # -------------------------- @api.multi - def get_available_users(self): + def _get_available_users(self): """ get available user of a given channel :retuns : return the res.users having their im_status online """ self.ensure_one() - return self.sudo().user_ids.filtered(lambda user: user.im_status == 'online') + return self.user_ids.filtered(lambda user: user.im_status == 'online') - @api.model - def get_mail_channel(self, livechat_channel_id, anonymous_name, user_id=None, country_id=None): + @api.multi + def _get_mail_channel(self, anonymous_name, user_id=None, country_id=None): """ Return a mail.channel given a livechat channel. It creates one with a connected operator, or return false otherwise - :param livechat_channel_id : the identifier if the im_livechat.channel :param anonymous_name : the name of the anonymous person of the channel :param user_id : the id of the logged in visitor, if any :param country_code : the country of the anonymous person of the channel - :type livechat_channel_id : int :type anonymous_name : str :return : channel header :rtype : dict """ + self.ensure_one() # get the avalable user of the channel - operators = self.sudo().browse(livechat_channel_id).get_available_users() + operators = self._get_available_users() if len(operators) == 0: return False # choose the res.users operator and get its partner id @@ -158,7 +157,7 @@ class ImLivechatChannel(models.Model): mail_channel = self.env["mail.channel"].with_context(mail_create_nosubscribe=False).sudo().create({ 'channel_partner_ids': channel_partner_to_add, 'livechat_operator_id': operator_partner_id, - 'livechat_channel_id': livechat_channel_id, + 'livechat_channel_id': self.id, 'anonymous_name': False if user_id else anonymous_name, 'country_id': country_id, 'channel_type': 'livechat', @@ -169,24 +168,25 @@ class ImLivechatChannel(models.Model): mail_channel._broadcast([operator_partner_id]) return mail_channel.sudo().channel_info()[0] - @api.model - def get_channel_infos(self, channel_id): - channel = self.browse(channel_id) + def _get_channel_infos(self): + self.ensure_one() + return { - 'button_text': channel.button_text, - 'input_placeholder': channel.input_placeholder, - 'default_message': channel.default_message, - "channel_name": channel.name, - "channel_id": channel.id, + 'button_text': self.button_text, + 'input_placeholder': self.input_placeholder, + 'default_message': self.default_message, + "channel_name": self.name, + "channel_id": self.id, } - @api.model - def get_livechat_info(self, channel_id, username='Visitor'): + def get_livechat_info(self, username='Visitor'): + self.ensure_one() + info = {} - info['available'] = len(self.browse(channel_id).get_available_users()) > 0 + info['available'] = len(self._get_available_users()) > 0 info['server_url'] = self.env['ir.config_parameter'].sudo().get_param('web.base.url') if info['available']: - info['options'] = self.sudo().get_channel_infos(channel_id) + info['options'] = self._get_channel_infos() info['options']["default_username"] = username return info diff --git a/addons/im_livechat/views/im_livechat_channel_templates.xml b/addons/im_livechat/views/im_livechat_channel_templates.xml index 6cc5f093969..0d3a7e0c8da 100644 --- a/addons/im_livechat/views/im_livechat_channel_templates.xml +++ b/addons/im_livechat/views/im_livechat_channel_templates.xml @@ -187,10 +187,6 @@