Skip to content

Commit 65f084c

Browse files
committed
[IMP] base: server actions: add sequence support
In order to update a record using a sequence: - before this commit one would have to use a python expression like `env['ir.sequence'].next_by_code('the_sequence_code')` which was not very discoverable. - after this commit there is a standard way to select a sequence through the server action's form view. Task id: opw-4689501 closes odoo#208376 Signed-off-by: Lucas Perais (lpe) <[email protected]>
1 parent 6761801 commit 65f084c

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

addons/test_base_automation/tests/test_flow.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,35 @@ def test_01_form_object_write_o2m_field(self):
16181618
self.assertEqual(aks_partner.child_ids.ids, [])
16191619
self.assertEqual(bs_partner.parent_id.id, False)
16201620

1621+
def test_02_form_object_write_with_sequence(self):
1622+
test_partner = self.env["res.partner"].create({"name": "Test Partner"})
1623+
test_sequence = self.env["ir.sequence"].create({
1624+
"name": "Test Sequence",
1625+
"padding": 4,
1626+
"prefix": "PARTNER/",
1627+
"suffix": "/TEST",
1628+
})
1629+
1630+
f = Form(self.env['ir.actions.server'], view="base.view_server_action_form")
1631+
f.model_id = self.env["ir.model"]._get("res.partner")
1632+
f.state = "object_write"
1633+
f.evaluation_type = "sequence"
1634+
self.assertEqual(f.warning, False)
1635+
f.update_path = "active"
1636+
self.assertEqual(f.warning, "A sequence must only be used with character fields.")
1637+
f.update_path = "ref"
1638+
self.assertEqual(f.warning, False)
1639+
f.sequence_id = test_sequence
1640+
1641+
action = f.save()
1642+
self.assertEqual(test_partner.ref, False)
1643+
action.with_context(
1644+
active_model="res.partner",
1645+
active_id=test_partner.id,
1646+
).run()
1647+
self.assertEqual(test_partner.ref, "PARTNER/0001/TEST")
1648+
1649+
16211650
@common.tagged("post_install", "-at_install")
16221651
class TestHttp(common.HttpCase):
16231652
def test_webhook_trigger(self):

odoo/addons/base/models/ir_actions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,11 @@ def _default_update_path(self):
613613
"`42` or `My custom name` or the selected record.")
614614
evaluation_type = fields.Selection([
615615
('value', 'Update'),
616+
('sequence', 'Sequence'),
616617
('equation', 'Compute')
617618
], 'Value Type', default='value', change_default=True)
618619
html_value = fields.Html()
620+
sequence_id = fields.Many2one('ir.sequence', string='Sequence to use')
619621
resource_ref = fields.Reference(
620622
string='Record', selection='_selection_target_model', inverse='_set_resource_ref')
621623
selection_value = fields.Many2one('ir.model.fields.selection', string="Custom Value", ondelete='cascade',
@@ -624,6 +626,7 @@ def _default_update_path(self):
624626
value_field_to_show = fields.Selection([
625627
('value', 'value'),
626628
('html_value', 'html_value'),
629+
('sequence_id', 'sequence_id'),
627630
('resource_ref', 'reference'),
628631
('update_boolean_value', 'update_boolean_value'),
629632
('selection_value', 'selection_value'),
@@ -657,6 +660,8 @@ def _warning_depends(self):
657660
'child_ids.model_id',
658661
'child_ids.group_ids',
659662
'update_path',
663+
'update_field_type',
664+
'evaluation_type',
660665
'webhook_field_ids'
661666
]
662667

@@ -680,6 +685,9 @@ def _get_warning_messages(self):
680685
if (relation_chain := self._get_relation_chain("update_path")) and relation_chain[0] and isinstance(relation_chain[0][-1], fields.Json):
681686
warnings.append(_("I'm sorry to say that JSON fields (such as '%s') are currently not supported.", relation_chain[0][-1].string))
682687

688+
if self.state == 'object_write' and self.evaluation_type == 'sequence' and self.update_field_type and self.update_field_type not in ('char', 'text'):
689+
warnings.append(_("A sequence must only be used with character fields."))
690+
683691
if self.state == 'webhook' and self.model_id:
684692
restricted_fields = []
685693
Model = self.env[self.model_id.model]
@@ -1108,7 +1116,9 @@ def _can_execute_action_on_records(self, records):
11081116
@api.depends('evaluation_type', 'update_field_id')
11091117
def _compute_value_field_to_show(self): # check if value_field_to_show can be removed and use ttype in xml view instead
11101118
for action in self:
1111-
if action.update_field_id.ttype in ('one2many', 'many2one', 'many2many'):
1119+
if action.evaluation_type == 'sequence':
1120+
action.value_field_to_show = 'sequence_id'
1121+
elif action.update_field_id.ttype in ('one2many', 'many2one', 'many2many'):
11121122
action.value_field_to_show = 'resource_ref'
11131123
elif action.update_field_id.ttype == 'selection':
11141124
action.value_field_to_show = 'selection_value'
@@ -1150,6 +1160,8 @@ def _eval_value(self, eval_context=None):
11501160
expr = action.value
11511161
if action.evaluation_type == 'equation':
11521162
expr = safe_eval(action.value, eval_context)
1163+
elif action.evaluation_type == 'sequence':
1164+
expr = action.sequence_id.next_by_id()
11531165
elif action.update_field_id.ttype in ['one2many', 'many2many']:
11541166
operation = action.update_m2m_operation
11551167
if operation == 'add':

odoo/addons/base/views/ir_actions_views.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,14 @@
382382
<span invisible="evaluation_type != 'value' or update_field_type not in ['one2many', 'many2many']">by</span>
383383
<field name="update_m2m_operation" class="oe_inline" invisible="evaluation_type != 'value' or update_field_type not in ['one2many', 'many2many']" required="update_field_type in ['one2many', 'many2many']"/>
384384
<span invisible="evaluation_type != 'value' or update_field_type in ['one2many', 'many2many']">to</span>
385+
<span invisible="evaluation_type != 'sequence'">with</span>
385386
<field name="value" class="oe_inline" placeholder="Set a value..." invisible="update_field_id == False or value_field_to_show != 'value' or evaluation_type != 'value'" string="Custom Value"/>
386387
<field name="html_value" class="w-100" placeholder="Set a value..." invisible="update_field_id == False or value_field_to_show != 'html_value' or evaluation_type != 'value'" string="Custom Value"/>
388+
<field name="sequence_id" class="oe_inline" placeholder="Select a sequence..." invisible="evaluation_type != 'sequence'" required="evaluation_type == 'sequence'"/>
387389
<field name="resource_ref" class="oe_inline" placeholder="Choose a value..." string="Custom Value" options="{'model_field': 'update_related_model_id', 'no_create': True, 'no_open': True}" invisible="update_field_id == False or value_field_to_show != 'resource_ref' or evaluation_type == 'equation' or update_m2m_operation == 'clear'"/>
388390
<field name="selection_value" class="oe_inline" placeholder="Choose a value..." options="{'no_create': True, 'no_open': True}" invisible="update_field_id == False or value_field_to_show != 'selection_value' or evaluation_type == 'equation'"/>
389391
<field name="update_boolean_value" class="oe_inline" invisible="evaluation_type != 'value' or value_field_to_show != 'update_boolean_value'" required="value_field_to_show == 'update_boolean_value'"/>
390-
<span invisible="update_field_id != False or evaluation_type == 'equation'" class="o_actions_server_set_a_value text-muted">Set a value...</span>
392+
<span invisible="update_field_id != False or evaluation_type != 'value'" class="o_actions_server_set_a_value text-muted">Set a value...</span>
391393
<span invisible="evaluation_type != 'equation'">
392394
to this Python expression:
393395
</span>

0 commit comments

Comments
 (0)