[IMP] base, mail, sms: directly limit available models depending on usage

Add a computed m2m field giving allowed models for the action. It allows to
avoid choosing the wrong models when designing server actions.

Remove a test that has no use: it checks a model_id field is available on
server action view while we don't want to display it as it is imposed by
the rule itself.

Task-3527758 (Base Automation Refactor Fiximp)
Part of Task-3527752 (Mail: The Pre-Major Freeze FixImpLint)

Part-of: odoo/odoo#137133
This commit is contained in:
Thibault Delavallée
2023-10-02 06:54:38 +00:00
parent 84b8ca626e
commit 42958e2f5e
7 changed files with 42 additions and 42 deletions
@@ -22,3 +22,12 @@ class ServerAction(models.Model):
rule_name=action.base_automation_id.name
)
)
@api.depends('usage')
def _compute_available_model_ids(self):
""" Stricter model limit: based on automation rule """
super()._compute_available_model_ids()
rule_based = self.filtered(lambda action: action.usage == 'base_automation')
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 []
+11
View File
@@ -106,6 +106,17 @@ class ServerActions(models.Model):
)
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 in {'mail_post', 'followers', 'remove_followers', 'next_activity'}
)
if mail_thread_based:
mail_models = self.env['ir.model'].search([('is_mail_thread', '=', True), ('transient', '=', False)])
for action in mail_thread_based:
action.available_model_ids = mail_models.ids
super(ServerActions, self - mail_thread_based)._compute_available_model_ids()
@api.depends('model_id', 'state')
def _compute_template_id(self):
to_reset = self.filtered(
+9
View File
@@ -36,6 +36,15 @@ class ServerActions(models.Model):
)
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')
if mail_thread_based:
mail_models = self.env['ir.model'].search([('is_mail_thread', '=', True), ('transient', '=', False)])
for action in mail_thread_based:
action.available_model_ids = mail_models.ids
super(ServerActions, self - mail_thread_based)._compute_available_model_ids()
@api.depends('model_id', 'state')
def _compute_sms_template_id(self):
to_reset = self.filtered(
@@ -431,28 +431,6 @@ registry.category("web_tour.tours").add("test_form_view_resequence_actions", {
],
});
registry.category("web_tour.tours").add("test_form_view_debug", {
test: true,
steps: () => [
{
trigger:
".o_form_renderer .o_field_widget[name='action_server_ids'] .o_kanban_view .o_cp_buttons button",
},
{
trigger: ".modal-content .o_form_renderer",
run() {
const allFields = this.$anchor[0].querySelectorAll(".o_field_widget[name]");
assertEqual(
Array.from(allFields)
.map((el) => el.getAttribute("name"))
.includes("model_id"),
true
);
},
},
],
});
let waitOrmCalls;
registry.category("web_tour.tours").add("test_form_view_model_id", {
test: true,
@@ -265,23 +265,6 @@ class BaseAutomationTestUi(HttpCase):
["Set Active To False 2", "Set Active To False 0", "Set Active To False 1"],
)
def test_form_view_debug(self):
model = self.env.ref("base.model_res_partner")
automation = self.env["base.automation"].create(
{
"name": "Test",
"trigger": "on_create_or_write",
"model_id": model.id,
}
)
self.start_tour(
(
f"/web?debug=1#{_urlencode_kwargs(action=self.env.ref('base_automation.base_automation_act').id, id=automation.id, view_type='form')}"
),
"test_form_view_debug",
login="admin",
)
def test_form_view_model_id(self):
self.start_tour(
(
+8
View File
@@ -485,6 +485,7 @@ class IrActionsServer(models.Model):
"based on the sequence. Low number means high priority.")
model_id = fields.Many2one('ir.model', string='Model', required=True, ondelete='cascade', index=True,
help="Model on which the server action runs.")
available_model_ids = fields.Many2many('ir.model', string='Available Models', compute='_compute_available_model_ids', store=False)
model_name = fields.Char(related='model_id.model', string='Model Name', readonly=True, store=True)
# Python code
code = fields.Text(string='Python Code', groups='base.group_system',
@@ -548,6 +549,13 @@ class IrActionsServer(models.Model):
else:
action.name = dict(action._fields['state']._description_selection(self.env))[action.state]
@api.depends('state')
def _compute_available_model_ids(self):
allowed_models = self.env['ir.model'].search(
[('model', 'in', list(self.env['ir.model.access']._get_allowed_models()))]
)
self.available_model_ids = allowed_models.ids
@api.onchange('model_id')
def _compute_crud_model_id(self):
invalid = self.filtered(lambda act: act.crud_model_id != act.model_id)
+5 -3
View File
@@ -332,9 +332,11 @@
<group name="action_wrapper">
<group name="action_content">
<field name="model_name" invisible="1"/>
<field name="model_id" invisible="1"/>
<field name="model_id" options="{'no_create': True}" groups="base.group_no_one"/>
<field name="model_id" options="{'no_create': True}" groups="!base.group_no_one" invisible="context.get('default_model_id')"/>
<field name="available_model_ids" invisible="1"/>
<field name="model_id" options="{'no_create': True}" groups="base.group_no_one"
domain="[('id', 'in', available_model_ids)]"/>
<field name="model_id" options="{'no_create': True}" groups="!base.group_no_one" invisible="context.get('default_model_id')"
domain="[('id', 'in', available_model_ids)]"/>
<field name="groups_id" widget="many2many_tags" groups="base.group_no_one" />
<field name="state" string="Action Type"/>
<field name="update_field_id" invisible="state != 'object_write'"