Skip to content
Snippets Groups Projects
Commit c04b9c01 authored by Aurélien Warnon's avatar Aurélien Warnon Committed by jem-odoo
Browse files

[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
parent 92e9e84b
Branches
Tags
No related merge requests found
......@@ -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):
......
......@@ -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
......
......@@ -187,10 +187,6 @@
<!-- the js code to initialize the LiveSupport object -->
<template id="loader" name="Livechat : Javascript appending the livechat button">
<t t-translation="off">
<t t-if="not info">
<t t-set="info" t-value="request.env['im_livechat.channel'].get_livechat_info(channel)"/>
</t>
document.addEventListener("DOMContentLoaded", function(event) {
<t t-if="web_session_required">
odoo.define('web.session', function (require) {
......
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
from odoo import api, fields, models
class Website(models.Model):
......@@ -9,3 +9,13 @@ class Website(models.Model):
_inherit = "website"
channel_id = fields.Many2one('im_livechat.channel', string='Website Live Chat Channel')
@api.multi
def get_livechat_channel_info(self):
""" Get the livechat info dict (button text, channel name, ...) for the livechat channel of
the current website.
"""
self.ensure_one()
if self.channel_id:
return self.channel_id.sudo().get_livechat_info()
return {}
......@@ -49,7 +49,7 @@
<t t-if="website and website.channel_id">
<script>
<t t-call="im_livechat.loader">
<t t-set="channel" t-value="website.channel_id.id"/>
<t t-set="info" t-value="website.get_livechat_channel_info()"/>
</t>
</script>
</t>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment