Browse Source

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

12.0-mig-module_prototyper_last
Jairo Llopis 8 years ago
committed by Vladislav Shepilov
parent
commit
b60462f79d
  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 # fields.Binary(attachment=True), get the ir_attachment record ID
else: else:
extract_query = """ 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 FROM ir_attachment
WHERE res_field='%(field)s' AND res_model='%(model)s' WHERE res_field='%(field)s' AND res_model='%(model)s'
""" % {"model": model, "field": field} """ % {"model": model, "field": field}
@ -45,6 +50,7 @@ def pre_init_hook_for_submodules(cr, model, field):
INSERT INTO base_multi_image_image ( INSERT INTO base_multi_image_image (
owner_id, owner_id,
owner_model, owner_model,
owner_ref_id,
storage, storage,
%s %s
) )

19
base_multi_image/models/image.py

@ -27,6 +27,12 @@ class Image(models.Model):
required=True) required=True)
owner_model = fields.Char( owner_model = fields.Char(
required=True) 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( storage = fields.Selection(
[('url', 'URL'), ('file', 'OS file'), ('db', 'Database'), [('url', 'URL'), ('file', 'OS file'), ('db', 'Database'),
('filestore', 'Filestore')], ('filestore', 'Filestore')],
@ -74,6 +80,19 @@ class Image(models.Model):
show_technical = fields.Boolean( show_technical = fields.Boolean(
compute="_show_technical") 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.multi
@api.depends('storage', 'path', 'file_db_store', 'url') @api.depends('storage', 'path', 'file_db_store', 'url')
def _get_image(self): def _get_image(self):

1
base_multi_image/views/image_view.xml

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

Loading…
Cancel
Save