Skip to content

Commit

Permalink
[UPD] add resource_ref in tree view, hide it for templates, add searc…
Browse files Browse the repository at this point in the history
…h view [WIP]
  • Loading branch information
ntsirintanis committed Jun 8, 2022
1 parent fa28dbc commit 8bf8845
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
45 changes: 36 additions & 9 deletions report_dynamic/models/report_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ReportDynamic(models.Model):
selection="_selection_target_model",
compute="_compute_resource_ref",
inverse="_inverse_resource_ref",
store=True,
)
wrapper_report_id = fields.Many2one("ir.ui.view", domain="[('type', '=', 'qweb')]")
template_id = fields.Many2one(
Expand All @@ -43,6 +44,11 @@ class ReportDynamic(models.Model):
"ir.model.fields", "contextual_field_rel", "contextual_id", "field_id", "Fields"
)
window_action_exists = fields.Boolean(compute="_compute_window_action_exists")
group_by_record_name = fields.Char(
compute="_compute_group_by_record_name",
store=True,
help="Computed field for grouping by record name in search view",
)

@api.model
def _selection_target_model(self):
Expand All @@ -58,8 +64,16 @@ def _compute_resource_ref(self):
else this.template_id.model_id.model
)
if model:
# Return a meaningful message anytime this breaks
try:
sample_record = self.env[model].search([], limit=1)
except Exception as e:
raise UserError(
_("Model {} is not applicable for report. Reason: {}").format(
model, str(e)
)
)
# Tackle the problem of non-existing sample record
sample_record = self.env[model].search([], limit=1)
if not sample_record:
raise UserError(
_(
Expand All @@ -75,6 +89,16 @@ def _compute_resource_ref(self):
else:
this.resource_ref = False

def _inverse_resource_ref(self):
for this in self:
if this.resource_ref:
this.res_id = this.resource_ref.id
this.model_id = (
self.env["ir.model"]
.search([("model", "=", this.resource_ref._name)], limit=1)
.id
)

def _compute_window_action_exists(self):
for this in self:
this.window_action_exists = bool(
Expand All @@ -86,15 +110,18 @@ def _compute_window_action_exists(self):
)
)

def _inverse_resource_ref(self):
@api.depends("resource_ref")
def _compute_group_by_record_name(self):
for this in self:
if this.resource_ref:
this.res_id = this.resource_ref.id
this.model_id = (
self.env["ir.model"]
.search([("model", "=", this.resource_ref._name)], limit=1)
.id
)
this.group_by_record_name = ""
if this.is_template:
continue
if hasattr(this.resource_ref, "name"):
this.group_by_record_name = this.resource_ref.name
continue
this.group_by_record_name = _("{} - {}").format(
this.resource_ref._name, this.resource_ref.id
)

def get_template_xml_id(self):
self.ensure_one()
Expand Down
39 changes: 38 additions & 1 deletion report_dynamic/views/report_dynamic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,54 @@
</field>
</record>


<record id="report_dynamic_tree" model="ir.ui.view">
<field name="name">report.dynamic.tree</field>
<field name="model">report.dynamic</field>
<field name="arch" type="xml">
<tree>
<field name="name" readonly="True" />
<field name="model_id" readonly="True" />
<field
name="resource_ref"
readonly="True"
invisible="context.get('is_template')==True"
string="For"
/>
</tree>
</field>
</record>

<!-- Search view -->
<record id="report_dynamic_search" model="ir.ui.view">
<field name="name">report.dynamic.search</field>
<field name="model">report.dynamic</field>
<field name="arch" type="xml">
<search>
<field name="name" />
<field name="model_id" />
<field name="group_by_record_name" />
<filter
string="Archived"
name="active"
domain="[('active', '=',False)]"
/>
<group expand="0" string="Group By">
<filter
string="Model"
name="Model"
context="{'group_by': 'model_id'}"
/>
<filter
string="Record"
name="Record"
context="{'group_by': 'group_by_record_name'}"
/>
</group>
</search>
</field>
</record>

<!-- action and menu for reports-->
<record id="report_dynamic_action" model="ir.actions.act_window">
<field name="name">Dynamic Reports</field>
Expand All @@ -224,7 +261,7 @@
<field name="res_model">report.dynamic</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('is_template', '=', True)]</field>
<field name="context">{'default_is_template': True}</field>
<field name="context">{'default_is_template': True, 'is_template':True}</field>
</record>
<menuitem
name="Templates"
Expand Down

0 comments on commit 8bf8845

Please sign in to comment.