Skip to content

Commit

Permalink
[FIX] mass_edit: Play onchanges before writing
Browse files Browse the repository at this point in the history
  • Loading branch information
grindtildeath committed Oct 29, 2024
1 parent 33f9425 commit 1a59422
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions server_action_mass_edit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"summary": "Mass Editing",
"depends": [
"base",
"onchange_helper",
],
"data": [
"security/ir.model.access.csv",
Expand Down
12 changes: 12 additions & 0 deletions server_action_mass_edit/models/ir_actions_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class IrActionsServer(models.Model):
string="Apply domain in lines",
compute="_compute_mass_edit_apply_domain_in_lines",
)
mass_edit_play_onchanges = fields.Json(
string="Play onchanges from lines",
compute="_compute_mass_edit_play_onchanges",
)
mass_edit_message = fields.Text(
string="Message",
help="If set, this message will be displayed in the wizard.",
Expand Down Expand Up @@ -46,6 +50,14 @@ def _compute_mass_edit_apply_domain_in_lines(self):
record.mass_edit_line_ids.mapped("apply_domain")
)

@api.depends("mass_edit_line_ids.apply_onchanges")
def _compute_mass_edit_play_onchanges(self):
for record in self:
record.mass_edit_play_onchanges = {
line.field_id.name: line.apply_onchanges
for line in record.mass_edit_line_ids
}

def _run_action_mass_edit_multi(self, eval_context=None):
"""Show report label wizard"""
context = dict(self.env.context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class IrActionsServerMassEditLine(models.Model):
default=False,
help="Apply default domain related to field",
)
apply_onchanges = fields.Boolean(help="Play field onchanges before writing value")

@api.constrains("server_action_id", "field_id")
def _check_field_model(self):
Expand Down
1 change: 1 addition & 0 deletions server_action_mass_edit/views/ir_actions_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/>
<field name="widget_option" />
<field name="apply_domain" />
<field name="apply_onchanges" />
</tree>
</field>
<field name="mass_edit_apply_domain_in_lines" invisible="1" />
Expand Down
38 changes: 34 additions & 4 deletions server_action_mass_edit/wizard/mass_editing_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MassEditingWizard(models.TransientModel):
operation_description_warning = fields.Text(readonly=True)
operation_description_danger = fields.Text(readonly=True)
message = fields.Text(readonly=True)
play_onchanges = fields.Json(readonly=True)

@api.model
def default_get(self, fields, active_ids=None):
Expand Down Expand Up @@ -67,6 +68,7 @@ def default_get(self, fields, active_ids=None):
"operation_description_warning": operation_description_warning,
"operation_description_danger": operation_description_danger,
"message": server_action.mass_edit_message,
"play_onchanges": server_action.mass_edit_play_onchanges,
}
)
server_action_id = self.env.context.get("server_action_id")
Expand Down Expand Up @@ -243,7 +245,7 @@ def _clean_check_company_field_domain(self, TargetModel, field, field_info):
return field_info

@api.model_create_multi
def create(self, vals_list):
def create(self, vals_list): # noqa: C901
server_action_id = self.env.context.get("server_action_id")
server_action = self.env["ir.actions.server"].sudo().browse(server_action_id)
active_ids = self.env.context.get("active_ids", [])
Expand Down Expand Up @@ -281,9 +283,37 @@ def create(self, vals_list):
values.update({split_key: m2m_list})

if values:
self.env[server_action.model_id.model].browse(
active_ids
).with_context(mass_edit=True,).write(values)
model = self.env[server_action.model_id.model].with_context(
mass_edit=True
)
records = model.browse(active_ids)
# Check if a field in values is set to play onchanges, in which case
# each record is to be updated sequentially
onchanges_to_play = [
fname
for fname, val in server_action.mass_edit_play_onchanges.items()
if val
]
if onchanges_to_play:
onchange_values = {
k: v for k, v in values.items() if k in onchanges_to_play
}
not_onchange_values = {
k: v
for k, v in values.items()
if k not in onchanges_to_play
}
for rec in records:
rec_values = onchange_values.copy()
rec_values = rec.play_onchanges(
rec_values, list(rec_values.keys())
)
rec_values.update(not_onchange_values)
rec.write(rec_values)
else:
# If there is not any onchange to play we can write
# all the records at once
records.write(values)
return super().create([{}])

def _prepare_create_values(self, vals_list):
Expand Down

0 comments on commit 1a59422

Please sign in to comment.