Skip to content

Commit

Permalink
[UPD] don't create duplicate window actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsirintanis committed Jun 2, 2022
1 parent 22cc75d commit 523d968
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
46 changes: 30 additions & 16 deletions report_dynamic/models/report_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,10 @@ class ReportDynamic(models.Model):
active = fields.Boolean(default=True)
is_template = fields.Boolean(default=False)
lock_date = fields.Date()
ref_ir_act_window_id = fields.Many2one(
"ir.actions.act_window",
"Sidebar action",
readonly=True,
help="Sidebar action to make this "
"template available on "
"records of the related "
"document model.",
)
field_ids = fields.Many2many(
"ir.model.fields", "contextual_field_rel", "contextual_id", "field_id", "Fields"
)
window_action_exists = fields.Boolean(compute="_compute_window_action_exists")

@api.model
def _selection_target_model(self):
Expand All @@ -70,6 +62,17 @@ def _compute_resource_ref(self):
else:
this.resource_ref = False

def _compute_window_action_exists(self):
for this in self:
this.window_action_exists = bool(
self.env["ir.actions.act_window"].search_count(
[
("res_model", "=", "wizard.report.dynamic"),
("binding_model_id", "=", this.model_id.id),
]
)
)

def _inverse_resource_ref(self):
for this in self:
if this.resource_ref:
Expand Down Expand Up @@ -182,16 +185,24 @@ def write(self, values):
self.wrapper_report_id = self._set_wrapper_report_id(template)
return super().write(values)

def unlink(self):
for this in self:
if this.window_action_exists:
this.unlink_action()
return super().unlink()

def _set_wrapper_report_id(self, template):
self.ensure_one()
return template.wrapper_report_id or self.env.company.external_report_layout_id

# Contextual action for dynamic reports
def create_action(self):
self.ensure_one()
vals = {}
action_obj = self.env["ir.actions.act_window"]
vals["ref_ir_act_window_id"] = action_obj.create(
if self.window_action_exists:
return
if not self.model_id:
return
self.env["ir.actions.act_window"].create(
{
"name": "Dynamic Reporting",
"type": "ir.actions.act_window",
Expand All @@ -203,11 +214,14 @@ def create_action(self):
"binding_type": "action",
"binding_model_id": self.model_id.id,
}
).id
self.write(vals)
)

def unlink_action(self):
# We make sudo as any user with rights in this model should be able
# to delete the action, not only admin
self.mapped("ref_ir_act_window_id").sudo().unlink()
return True
self.env["ir.actions.act_window"].search(
[
("res_model", "=", "wizard.report.dynamic"),
("binding_model_id", "=", self.model_id.id),
]
).sudo().unlink()
6 changes: 3 additions & 3 deletions report_dynamic/views/report_dynamic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
attrs="{'invisible': [('active', '=', True)]}"
/>
<div class="oe_button_box" name="button_box">
<field name="ref_ir_act_window_id" invisible="1" />
<field name="window_action_exists" invisible="1" />
<button
name="create_action"
type="object"
string="Add Report Button"
class="oe_inline oe_stat_button"
attrs="{'invisible':['|',('ref_ir_act_window_id','!=',False),('is_template', '=', False)]}"
attrs="{'invisible':['|',('window_action_exists','=',True),('is_template', '=', False)]}"
icon="fa-plus"
help="Display a button in the sidebar of related documents to open a composition wizard"
/>
Expand All @@ -29,7 +29,7 @@
string="Remove Report Button"
class="oe_stat_button"
icon="fa-minus"
attrs="{'invisible':['|',('ref_ir_act_window_id','=',False),('is_template', '=', False)]}"
attrs="{'invisible':['|',('window_action_exists','=',False),('is_template', '=', False)]}"
help="Remove the contextual action to use this template on related documents"
widget="statinfo"
/>
Expand Down

0 comments on commit 523d968

Please sign in to comment.