Browse Source

Add a reference field to get to the owner with 1 click.

pull/505/head
Jairo Llopis 9 years ago
committed by Pedro M. Baeza
parent
commit
74dd72b01f
  1. 8
      base_multi_image/hooks.py
  2. 19
      base_multi_image/models/image.py
  3. 1
      base_multi_image/views/image_view.xml

8
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
)

19
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):

1
base_multi_image/views/image_view.xml

@ -19,6 +19,7 @@
<field name="show_technical" invisible="True"/>
<field name="owner_model"/>
<field name="owner_id"/>
<field name="owner_ref_id"/>
<field name="sequence"/>
</group>
<group string="Name">

Loading…
Cancel
Save