[IMP] base,mail,sms,base_automation: auto-naming of server actions

Commit b05e7e4da0 rightfully removed the _compute_name dependency on
context keys (a bad idea for stored computed fields), but this change
could lead to UX frustrations (Server Action names getting reset all the
time despite what the user may have used as a name before) and UX issues
(e.g. triggering a recompute by accident at module removal that could
reset all 'code' action names to 'Execute Code').

This commit simply removes the automated naming feature from the base
module and moves the logic to the base_automation module - and only
computes automated names based on the link with an automation rule or
not (so 'standalone' server actions need a name given by the user at all
times).

Task-3450200

Part-of: odoo/odoo#138804
This commit is contained in:
Damien Bouvy
2023-10-26 13:41:07 +00:00
parent 3871ae2c6c
commit 37976ec5e1
5 changed files with 51 additions and 50 deletions
@@ -7,6 +7,8 @@ from odoo import api, exceptions, fields, models, _
class ServerAction(models.Model):
_inherit = "ir.actions.server"
name = fields.Char(compute='_compute_name', store=True, readonly=False)
usage = fields.Selection(selection_add=[
('base_automation', 'Automation Rule')
], ondelete={'base_automation': 'cascade'})
@@ -31,3 +33,51 @@ class ServerAction(models.Model):
for action in rule_based:
rule_model = action.base_automation_id.model_id
action.available_model_ids = rule_model.ids if rule_model in action.available_model_ids else []
@api.depends('state', 'update_field_id', 'crud_model_id', 'value', 'evaluation_type', 'template_id', 'partner_ids', 'activity_summary', 'sms_template_id', 'webhook_url')
def _compute_name(self):
''' Only server actions linked to a base_automation get an automatic name. '''
to_update = self.filtered('base_automation_id')
for action in to_update:
match action.state:
case 'object_write':
action_type = _("Update") if action.evaluation_type == 'value' else _("Compute")
action.name = f"{action_type} {action._stringify_path()}"
case 'object_create':
action.name = _(
"Create %(model_name)s with name %(value)s",
model_name=action.crud_model_id.name,
value=action.value
)
case 'webhook':
action.name = _("Send Webhook Notification")
case 'sms':
action.name = _(
'Send SMS: %(template_name)s',
template_name=action.sms_template_id.name
)
case 'mail_post':
action.name = _(
'Send email: %(template_name)s',
template_name=action.template_id.name
)
case 'followers':
action.name = _(
'Add followers: %(partner_names)s',
partner_names=', '.join(action.partner_ids.mapped('name'))
)
case 'remove_followers':
action.name = _(
'Remove followers: %(partner_names)s',
partner_names=', '.join(action.partner_ids.mapped('name'))
)
case 'next_activity':
action.name = _(
'Create activity: %(activity_name)s',
activity_name=action.activity_summary or action.activity_type_id.name
)
case other:
action.name = dict(action._fields['state']._description_selection(self.env))[action.state]
# Not sure, but IIRC assignation is mandatory and I don't want the name to be reset by accident
for action in (self - to_update):
action.name = action.name or ''
-28
View File
@@ -80,34 +80,6 @@ class ServerActions(models.Model):
'User Field',
compute='_compute_activity_info', readonly=False, store=True)
@api.depends('template_id', 'partner_ids', 'activity_summary')
def _compute_name(self):
to_update = self.filtered(
lambda action: action.state in {'mail_post', 'followers', 'remove_followers', 'next_activity'}
)
for action in to_update:
if action.state == 'mail_post':
action.name = _(
'Send email: %(template_name)s',
template_name=action.template_id.name
)
elif action.state == 'followers':
action.name = _(
'Add followers: %(partner_names)s',
partner_names=', '.join(action.partner_ids.mapped('name'))
)
elif action.state == 'remove_followers':
action.name = _(
'Remove followers: %(partner_names)s',
partner_names=', '.join(action.partner_ids.mapped('name'))
)
elif action.state == 'next_activity':
action.name = _(
'Next activity: %(activity_name)s',
activity_name=action.activity_summary or action.activity_type_id.name
)
super(ServerActions, self - to_update)._compute_name()
@api.depends('state')
def _compute_available_model_ids(self):
mail_thread_based = self.filtered(
-10
View File
@@ -26,16 +26,6 @@ class ServerActions(models.Model):
compute='_compute_sms_method',
readonly=False, store=True)
@api.depends('sms_template_id')
def _compute_name(self):
to_update = self.filtered(lambda action: action.state == 'sms')
for action in to_update:
action.name = _(
'Send SMS: %(template_name)s',
template_name=action.sms_template_id.name
)
super(ServerActions, self - to_update)._compute_name()
@api.depends('state')
def _compute_available_model_ids(self):
mail_thread_based = self.filtered(lambda action: action.state == 'sms')
+1 -1
View File
@@ -472,7 +472,7 @@ class IrActionsServer(models.Model):
return field_name
return ''
name = fields.Char(compute='_compute_name', store=True, readonly=False, required=True)
name = fields.Char(required=True)
type = fields.Char(default='ir.actions.server')
usage = fields.Selection([
('ir_actions_server', 'Server Action'),
-11
View File
@@ -309,17 +309,6 @@ ZeroDivisionError: division by zero""" % self.test_server_action.id
})
self.action.flush_recordset(['update_path', 'update_field_id'])
def test_38_field_traversal_name_compute(self):
""" Test the automated computed name of an 'update record' action """
# update the country's name via the partner
action_with_name = self.action.with_context(automatic_action_name=True)
action_with_name.write({
'state': 'object_write',
'update_path': 'country_id.name',
'value': 'TestUpdatedCountry',
})
self.assertEqual(action_with_name.name, 'Update Country > Country Name', 'ir_actions_server: name should have been updated automatically')
@mute_logger('odoo.addons.base.models.ir_model', 'odoo.models')
def test_40_multi(self):
# Data: 2 server actions that will be nested