From 74dd72b01fc519e5686c35a6e7fc2ae9de136f17 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Wed, 20 Jul 2016 17:51:43 +0200 Subject: [PATCH] Add a reference field to get to the owner with 1 click. --- base_multi_image/hooks.py | 8 +++++++- base_multi_image/models/image.py | 19 +++++++++++++++++++ base_multi_image/views/image_view.xml | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/base_multi_image/hooks.py b/base_multi_image/hooks.py index 55496df40..2846af771 100644 --- a/base_multi_image/hooks.py +++ b/base_multi_image/hooks.py @@ -35,7 +35,12 @@ def pre_init_hook_for_submodules(cr, model, field): # fields.Binary(attachment=True), get the ir_attachment record ID else: extract_query = """ - SELECT res_id, res_model, 'filestore', id + SELECT + res_id, + res_model, + CONCAT_WS(',', res_model, res_id), + 'filestore', + id FROM ir_attachment WHERE res_field='%(field)s' AND res_model='%(model)s' """ % {"model": model, "field": field} @@ -45,6 +50,7 @@ def pre_init_hook_for_submodules(cr, model, field): INSERT INTO base_multi_image_image ( owner_id, owner_model, + owner_ref_id, storage, %s ) diff --git a/base_multi_image/models/image.py b/base_multi_image/models/image.py index 2c9055de5..eda04946e 100644 --- a/base_multi_image/models/image.py +++ b/base_multi_image/models/image.py @@ -27,6 +27,12 @@ class Image(models.Model): required=True) owner_model = fields.Char( required=True) + owner_ref_id = fields.Reference( + selection="_selection_owner_ref_id", + string="Referenced Owner", + compute="_compute_owner_ref_id", + store=True, + ) storage = fields.Selection( [('url', 'URL'), ('file', 'OS file'), ('db', 'Database'), ('filestore', 'Filestore')], @@ -74,6 +80,19 @@ class Image(models.Model): show_technical = fields.Boolean( compute="_show_technical") + @api.model + @tools.ormcache("self") + def _selection_owner_ref_id(self): + """Allow any model; after all, this field is readonly.""" + return [(r.model, r.name) for r in self.env["ir.model"].search([])] + + @api.multi + @api.depends("owner_model", "owner_id") + def _compute_owner_ref_id(self): + """Get a reference field based on the split model and id fields.""" + for s in self: + s.owner_ref_id = "{0.owner_model},{0.owner_id}".format(s) + @api.multi @api.depends('storage', 'path', 'file_db_store', 'url') def _get_image(self): diff --git a/base_multi_image/views/image_view.xml b/base_multi_image/views/image_view.xml index e7bd342d2..8b41025ef 100644 --- a/base_multi_image/views/image_view.xml +++ b/base_multi_image/views/image_view.xml @@ -19,6 +19,7 @@ +