Rationale ========= Since v8, the `display_name` field is present on all models. By default, `display_name` uses `name_get` which has pretty much the same purpose (return record name used by the web client). Gradually, many (backend) developers (and the ORM: https://github.com/odoo/odoo/commit/6da1c3ac4c036eac289597602976538e243cb939) started using `display_name` (more convenient than `record.name_get()[0][1]`) but it still had the `name_get` override. It becomes more complex than necessary and poeple start to misunderstand the two (and sometimes override both, leading to inconstiencies between `display_name`/`name_get`). To simplify the ORM and the API, we decided to keep only one of them, the `display_name` field: - It is much more convenient from a backend point of view (`record.name_get()[0][1]` vs `record.display_name`) - It is cached during the same transaction (and invalidated if its dependencies change) - It can be overridden like any other compute field (override `_compute_display_name` with any extra dependencies) - `name_get` is replaced by `read(['display_name'])` (API perceptive), which can actually be more efficient (if `display_name`'s depends are correct, the ORM will only fetch the fields it needs instead of every prefetchable field) Changes ======= - Deprecates `name_get` for the v17 and based the method on `display_name` (the opposite of before) - Converts all usage of `name_get` - Overrides of `name_get` are now overrides of `_compute_display_name` - For `res.partner`, rename the field store `display_name` into `complete_name` because `display_name` context-dependent and it makes no sense to have a compute store that is context-dependent. - Previously, it was possible to return multiple names for the same record with `name_get`, but it was tricky and most of the usage of this `name_get` didn't take this into account. The only example of this is the `name_get` of `product.product` (now use `", ".join(<names>)`). Part-of: odoo/odoo#122085
55 lines
2.4 KiB
Python
55 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, models, fields
|
|
from odoo.osv import expression
|
|
|
|
import textwrap
|
|
|
|
|
|
class ChatbotScriptAnswer(models.Model):
|
|
_name = 'chatbot.script.answer'
|
|
_description = 'Chatbot Script Answer'
|
|
_order = 'script_step_id, sequence, id'
|
|
|
|
name = fields.Char(string='Answer', required=True, translate=True)
|
|
sequence = fields.Integer(string='Sequence', default=1)
|
|
redirect_link = fields.Char('Redirect Link',
|
|
help="The visitor will be redirected to this link upon clicking the option "
|
|
"(note that the script will end if the link is external to the livechat website).")
|
|
script_step_id = fields.Many2one(
|
|
'chatbot.script.step', string='Script Step', required=True, ondelete='cascade')
|
|
chatbot_script_id = fields.Many2one(related='script_step_id.chatbot_script_id')
|
|
|
|
def _compute_display_name(self):
|
|
if self._context.get('chatbot_script_answer_display_short_name'):
|
|
return super()._compute_display_name()
|
|
|
|
for answer in self:
|
|
answer_message = answer.script_step_id.message.replace('\n', ' ')
|
|
shortened_message = textwrap.shorten(answer_message, width=26, placeholder=" [...]")
|
|
|
|
answer.display_name = f"{shortened_message}: {answer.name}"
|
|
|
|
@api.model
|
|
def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None):
|
|
"""
|
|
Search the records whose name or step message are matching the ``name`` pattern.
|
|
The chatbot_script_id is also passed to the context through the custom widget
|
|
('chatbot_triggering_answers_widget') This allows to only see the question_answer
|
|
from the same chatbot you're configuring.
|
|
"""
|
|
domain = domain or []
|
|
|
|
if name and operator == 'ilike':
|
|
# search on both name OR step's message (combined with passed args)
|
|
name_domain = [('name', operator, name)]
|
|
step_domain = [('script_step_id.message', operator, name)]
|
|
domain = expression.AND([domain, expression.OR([name_domain, step_domain])])
|
|
|
|
force_domain_chatbot_script_id = self.env.context.get('force_domain_chatbot_script_id')
|
|
if force_domain_chatbot_script_id:
|
|
domain = expression.AND([domain, [('chatbot_script_id', '=', force_domain_chatbot_script_id)]])
|
|
|
|
return self._search(domain, limit=limit, order=order)
|