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 14, 2023
1 parent 9b07f99 commit 85a8d6a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 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
68 changes: 32 additions & 36 deletions chained_swapper/wizard/chained_swapper_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,12 @@ def default_get(self, fields):
return super().default_get(fields)

@api.model
def fields_view_get(
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
def get_view(self, view_id=None, view_type="form", **options):
"""As we don't have any field in this model, result['fields']
and result['arch'] are modified to add dynamically the
corresponding field.
"""
res = super().fields_view_get(
view_id=view_id,
view_type=view_type,
toolbar=toolbar,
submenu=submenu,
)
res = super().get_view(view_id, view_type, **options)

Check warning on line 48 in chained_swapper/wizard/chained_swapper_wizard.py

View check run for this annotation

Codecov / codecov/patch

chained_swapper/wizard/chained_swapper_wizard.py#L48

Added line #L48 was not covered by tests
if not self.env.context.get("chained_swapper_id"):
return res
chained_swapper = self.env["chained.swapper"].browse(

Check warning on line 51 in chained_swapper/wizard/chained_swapper_wizard.py

View check run for this annotation

Codecov / codecov/patch

chained_swapper/wizard/chained_swapper_wizard.py#L50-L51

Added lines #L50 - L51 were not covered by tests
Expand Down Expand Up @@ -84,37 +77,40 @@ def fields_view_get(
res.update(arch=etree.tostring(doc, encoding="unicode"), fields=all_fields)
return res

Check warning on line 78 in chained_swapper/wizard/chained_swapper_wizard.py

View check run for this annotation

Codecov / codecov/patch

chained_swapper/wizard/chained_swapper_wizard.py#L75-L78

Added lines #L75 - L78 were not covered by tests

@api.model
def create(self, vals):
@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 85a8d6a

Please sign in to comment.