[IMP] base: Allow to select a record by its name on a server action line

Purpose
=======

When you update a record on a server action, you can modify a field
value (like the partner id). It's boring to be forced to specify
the id on the server action line. It would be easier to search on the
record name.

Specification
=============

A a server action line type 'Reference'. It allows to select the partner
name (Agrolait) instead of being forced to specify its id (3493).
This commit is contained in:
Pierre Paridans
2018-08-08 17:35:34 +02:00
committed by Yannick Tivisse
parent 7309981426
commit 91b62235f8
2 changed files with 40 additions and 2 deletions
+36 -1
View File
@@ -583,7 +583,42 @@ class IrServerObjectLines(models.Model):
"When Formula type is selected, this field may be a Python expression "
" that can use the same values as for the code field on the server action.\n"
"If Value type is selected, the value will be used directly without evaluation.")
type = fields.Selection([('value', 'Value'), ('equation', 'Python expression')], 'Evaluation Type', default='value', required=True, change_default=True)
type = fields.Selection([
('value', 'Value'),
('reference', 'Reference'),
('equation', 'Python expression')
], 'Evaluation Type', default='value', required=True, change_default=True)
resource_ref = fields.Reference(
string='Record', selection='_selection_target_model',
compute='_compute_resource_ref', inverse='_set_resource_ref')
@api.model
def _selection_target_model(self):
models = self.env['ir.model'].search([])
return [(model.model, model.name) for model in models]
@api.depends('col1.relation', 'value', 'type')
def _compute_resource_ref(self):
for line in self:
if line.type in ['reference', 'value'] and line.col1 and line.col1.relation:
value = line.value or ''
try:
value = int(value)
if not self.env[line.col1.relation].browse(value).exists():
record = self.env[line.col1.relation]._search([], limit=1)
value = record[0] if record else 0
except ValueError:
record = self.env[line.col1.relation]._search([], limit=1)
value = record[0] if record else 0
line.resource_ref = '%s,%s' % (line.col1.relation, value)
else:
line.resource_ref = False
@api.onchange('resource_ref')
def _set_resource_ref(self):
for line in self.filtered(lambda line: line.type == 'reference'):
if line.resource_ref:
line.value = str(line.resource_ref.id)
@api.multi
def eval_value(self, eval_context=None):
+4 -1
View File
@@ -276,6 +276,8 @@
<group name="action_content">
<field name="model_id"/>
<field name="model_name" invisible="1"/>
</group>
<group>
<field name="state"/>
<field name="type" invisible="1"/>
<field name="crud_model_id"
@@ -301,7 +303,8 @@
<tree string="Field Mappings" editable="bottom">
<field name="col1" domain="['|', ('model_id', '=', parent.crud_model_id), ('model_id', '=', parent.model_id)]"/>
<field name="type"/>
<field name="value"/>
<field name="resource_ref" attrs="{'readonly': [('type', '!=', 'reference')]}" options="{'hide_model': True}"/>
<field name="value" attrs="{'readonly': [('type', '=', 'reference')]}" force_save="1"/>
</tree>
</field>
</page>