Skip to content

Commit

Permalink
[MIG] chained_swapper: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kaynnan committed Jul 11, 2023
1 parent c5e78c1 commit f03dc16
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion chained_swapper/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Chained Swapper",
"summary": "Chained Swapper",
"version": "15.0.1.0.2",
"version": "16.0.1.0.0",
"development_status": "Mature",
"author": "Tecnativa, Odoo Community Association (OCA)",
"category": "Tools",
Expand Down
60 changes: 32 additions & 28 deletions chained_swapper/wizard/chained_swapper_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,41 @@ def fields_view_get(
res.update(arch=etree.tostring(doc, encoding="unicode"), fields=all_fields)
return res

@api.model
def create(self, vals):
"""As we don't have any field in this model, the key-value pair
@api.model_create_multi
def create(self, vals_list):
"""
As we don't have any field in this model, the key-value pair
received in vals dict are only used to change the value in the active
models.
"""
model_obj = self.env[self.env.context.get("active_model")]
context = self.env.context
field_name, new_value = list(vals.items())[0]
# write the active model
model = model_obj.browse(self.env.context.get("active_ids"))
original_values = {m.id: m[field_name] for m in model}
model.write(vals)
if hasattr(model, "message_post"):
self.post_chained_swap(model, field_name, original_values, new_value)
# write chained models
chained_swapper_obj = self.env["chained.swapper"]
chained_swapper = chained_swapper_obj.browse(context.get("chained_swapper_id"))
for sub_field in chained_swapper.sub_field_ids:
chain_fields = sub_field.sub_field_chain.split(".")
field_name = chain_fields.pop()
chain_model = model
for chain_field in chain_fields:
chain_model = chain_model.mapped(chain_field)
original_values = {cm.id: cm[field_name] for cm in chain_model}
chain_model.write({field_name: new_value})
# post swap
if hasattr(chain_model, "message_post"):
self.post_chained_swap(
chain_model, field_name, original_values, new_value
)
for vals in vals_list:
model_obj = self.env[self.env.context.get("active_model")]
context = self.env.context
field_name, new_value = list(vals.items())[0]
# write the active model
model = model_obj.browse(self.env.context.get("active_ids"))
original_values = {m.id: m[field_name] for m in model}
model.write(vals)
if hasattr(model, "message_post"):
self.post_chained_swap(model, field_name, original_values, new_value)
# write chained models
chained_swapper_obj = self.env["chained.swapper"]
chained_swapper = chained_swapper_obj.browse(
context.get("chained_swapper_id")
)
for sub_field in chained_swapper.sub_field_ids:
chain_fields = sub_field.sub_field_chain.split(".")
field_name = chain_fields.pop()
chain_model = model
for chain_field in chain_fields:
chain_model = chain_model.mapped(chain_field)
original_values = {cm.id: cm[field_name] for cm in chain_model}
chain_model.write({field_name: new_value})
# post swap
if hasattr(chain_model, "message_post"):
self.post_chained_swap(
chain_model, field_name, original_values, new_value
)
return super().create({})

def change_action(self):
Expand Down

0 comments on commit f03dc16

Please sign in to comment.