[FIX] base, web (qweb): image field in raw data mode handles options
Commit[1] implemented a way to output an image as its raw representation
`<img src="data:image/png;base64......." />`
It is useful for integrating an image of a record not accessible publicly.
However the original commit forgot to allow the img node to handle the options
passed to the field. Classes in particular were absent
After this commit, the options are handled correctly, and the image in raw mode
has the right classes.
opw-3517861
[1]: f8b901d04b
closes odoo/odoo#151410
X-original-commit: dbb22351921629aad845e29ebfa8b4083b4e745a
Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
This commit is contained in:
@@ -64,15 +64,19 @@ class Image(models.AbstractModel):
|
||||
"That is because the image goes into the tag, or it gets the " \
|
||||
"hose again."
|
||||
|
||||
src = src_zoom = None
|
||||
if options.get('qweb_img_raw_data', False):
|
||||
return super(Image, self).record_to_html(record, field_name, options)
|
||||
value = record[field_name]
|
||||
if value is False:
|
||||
return False
|
||||
src = self._get_src_data_b64(value, options)
|
||||
else:
|
||||
src, src_zoom = self._get_src_urls(record, field_name, options)
|
||||
|
||||
aclasses = ['img', 'img-fluid'] if options.get('qweb_img_responsive', True) else ['img']
|
||||
aclasses += options.get('class', '').split()
|
||||
classes = ' '.join(map(escape, aclasses))
|
||||
|
||||
src, src_zoom = self._get_src_urls(record, field_name, options)
|
||||
|
||||
if options.get('alt-field') and options['alt-field'] in record and record[options['alt-field']]:
|
||||
alt = escape(record[options['alt-field']])
|
||||
elif options.get('alt'):
|
||||
|
||||
@@ -22,3 +22,4 @@ from . import test_translate
|
||||
from . import test_web_redirect
|
||||
from . import test_res_users
|
||||
from . import test_webmanifest
|
||||
from . import test_ir_qweb
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
from lxml import etree
|
||||
from odoo.tests.common import TransactionCase
|
||||
|
||||
class TestIrQweb(TransactionCase):
|
||||
def test_image_field(self):
|
||||
view = self.env["ir.ui.view"].create({
|
||||
"key": "web.test_qweb",
|
||||
"type": "qweb",
|
||||
"arch": """<t t-name="test_qweb">
|
||||
<span t-field="record.avatar_128" t-options-widget="'image'" t-options-qweb_img_raw_data="is_raw_image" />
|
||||
</t>"""
|
||||
})
|
||||
partner = self.env["res.partner"].create({
|
||||
"name": "test image partner",
|
||||
"image_128": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAF0lEQVR4nGJxKFrEwMDAxAAGgAAAAP//D+IBWx9K7TUAAAAASUVORK5CYII=",
|
||||
})
|
||||
|
||||
html = view._render_template(view.id, {"is_raw_image": True, "record": partner})
|
||||
tree = etree.fromstring(html)
|
||||
img = tree.find("img")
|
||||
self.assertTrue(img.get("src").startswith("data:image/png;base64"))
|
||||
self.assertEqual(img.get("class"), "img img-fluid")
|
||||
self.assertEqual(img.get("alt"), "test image partner")
|
||||
|
||||
html = view._render_template(view.id, {"is_raw_image": False, "record": partner})
|
||||
tree = etree.fromstring(html)
|
||||
img = tree.find("img")
|
||||
self.assertTrue(img.get("src").startswith("/web/image"))
|
||||
self.assertEqual(img.get("class"), "img img-fluid")
|
||||
self.assertEqual(img.get("alt"), "test image partner")
|
||||
@@ -389,7 +389,7 @@ class ImageConverter(models.AbstractModel):
|
||||
_inherit = 'ir.qweb.field'
|
||||
|
||||
@api.model
|
||||
def value_to_html(self, value, options):
|
||||
def _get_src_data_b64(self, value, options):
|
||||
try: # FIXME: maaaaaybe it could also take raw bytes?
|
||||
image = Image.open(BytesIO(base64.b64decode(value)))
|
||||
image.verify()
|
||||
@@ -398,7 +398,11 @@ class ImageConverter(models.AbstractModel):
|
||||
except: # image.verify() throws "suitable exceptions", I have no idea what they are
|
||||
raise ValueError("Invalid image content")
|
||||
|
||||
return Markup('<img src="data:%s;base64,%s">' % (Image.MIME[image.format], value.decode('ascii')))
|
||||
return "data:%s;base64,%s" % (Image.MIME[image.format], value.decode('ascii'))
|
||||
|
||||
@api.model
|
||||
def value_to_html(self, value, options):
|
||||
return Markup('<img src="%s">') % self._get_src_data_b64(value, options)
|
||||
|
||||
class ImageUrlConverter(models.AbstractModel):
|
||||
""" ``image_url`` widget rendering, inserts an image tag in the
|
||||
|
||||
Reference in New Issue
Block a user