Button with selected records from xxx2Many

Rational

A frequent need is to run a button on a form based on selected records from a xxx2Many.

For example the wizards to handle exception on sale and purchase are indeed a way to implement this missing feature. A better design would be to launch the actions from a list of lines in exception.

Proposal

We add a new attribute to button tag on form. The attribute selected contains a comma separated list of names of the xxx2Many fields for which the selected record ids are passed as argument to the button method as a list of dictionaries (following the same order as the records).
The button is deactivated if no record is selected for the xxx2Many field (this shows to the user that the button is related to the selection). But also if the xxx2Many contains any record without a positive id (this is because after save we could not ensure to select the right one).

A decorator ModelView.button_selected is added to complete the existing button decorator. It is in charge of instantiate and testing the selected dictionary:

  • it instantiates the ids using the target class of the named xxx2Many
  • it raise an error message if any selected records are not in the corresponding xxx2Many of the record.

The form parser get <button/> defined inside the <field/> and add them inside the frame of the widget (only for one2many and many2many) at the bottom.

This attribute can be used for both kind of buttons.
For the button_action it will be up to the developer to pass the selected ids to the action.

Implementation

1 Like

As the button is defined inside the XML of the field is this new attribute necessary as they should be the same (if they are not then what happens ?).

Maybe people might want to put the button somewhere else than inside the frame of the x2m but it could lead to abuse such as putting the button on another tab.

Then it is a classic button.

We can say the same for many other things like label not next to field or required field in hidden tab.
Developers are responsible.

It will make bugs harder to find. I think that a warning should be logged because I don’t think it would be the expected behaviour.

But it also means that it would allow any kind of button inside the frame of a x2m then, wouldn’t it?

Of course but I wonder if by opting to make every button defined into x2m (and only those buttons) displaying the new kind of behaviour we’re not making something more robust.

What bug?

Yes why not.

I do not think that it is more robust to make implicit definition depending of the position in XML.

I can imagine that someone will change the name of the field but forget to change it also in the button attribute resulting in a bug.

Yes why not. But it’s something new and not in the scope of the rational.

Yet it is.

We will have a generic test for that like we check the name of buttons.

It may be useful to implement button that are not linked to selected records but are somehow related to the xxx2Many. For example like a creation wizard.