Skip to content

Commit

Permalink
[FIX] give template_id to report created through wizard, enable creat…
Browse files Browse the repository at this point in the history
…ing reports from report tree view, handle resource_ref correctly
  • Loading branch information
ntsirintanis committed Jun 2, 2022
1 parent b88619c commit fa28dbc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
23 changes: 18 additions & 5 deletions report_dynamic/models/report_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,28 @@ def _selection_target_model(self):
models = self.env["ir.model"].search([])
return [(model.model, model.name) for model in models]

@api.depends("model_id", "res_id")
@api.depends("model_id", "res_id", "template_id")
def _compute_resource_ref(self):
for this in self:
if this.model_id:
model = (
this.model_id.model
if this.is_template
else this.template_id.model_id.model
)
if model:
# Tackle the problem of non-existing sample record
sample_record = self.env[model].search([], limit=1)
if not sample_record:
raise UserError(
_(
"No sample record exists for Model {}. "
"Please create one before proceeding"
).format(model)
)
# we need to give a default to id part of resource_ref
# otherwise it is not editable
this.resource_ref = "{},{}".format(
this.model_id.model,
this.res_id or self.env[this.model_id.model].search([], limit=1).id,
model, this.res_id or sample_record.id,
)
else:
this.resource_ref = False
Expand Down Expand Up @@ -141,7 +154,7 @@ def action_toggle_active(self):
def create(self, values):
records = super().create(values)
for this in records:
if this.template_id.resource_ref:
if this.template_id.resource_ref and not this.res_id:
this.resource_ref = this.template_id.resource_ref
# Give a default to wrapper_report_id when
# user sets template_id
Expand Down
7 changes: 6 additions & 1 deletion report_dynamic/wizards/wizard_report_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def action_generate_reports(self):
else:
record_name = _("Model {}, id {}").format(record._name, record.id)
report = self.template_id.copy(
{"is_template": False, "res_id": record.id, "name": record_name}
{
"is_template": False,
"name": record_name,
"res_id": record.id,
"template_id": self.template_id.id,
}
)
reports += report
return {
Expand Down

0 comments on commit fa28dbc

Please sign in to comment.