[IMP] base,web: add image_url widget support in qweb.
The new "image_url" widget logic provides the ability to provide static image links in Char fields instead of duplicating the image file in database (+ filestore) through an Image field (& ir_attachment). This commits implements the frontend qweb logic for this widget, following the logic of the existing "image" qweb widget.
This commit is contained in:
@@ -21,20 +21,12 @@ class Image(models.AbstractModel):
|
||||
_description = 'Qweb Field Image'
|
||||
_inherit = 'ir.qweb.field.image'
|
||||
|
||||
@api.model
|
||||
def record_to_html(self, record, field_name, options):
|
||||
assert options['tagName'] != 'img',\
|
||||
"Oddly enough, the root tag of an image field can not be img. " \
|
||||
"That is because the image goes into the tag, or it gets the " \
|
||||
"hose again."
|
||||
|
||||
if options.get('qweb_img_raw_data', False):
|
||||
return super(Image, self).record_to_html(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))
|
||||
def _get_src_urls(self, record, field_name, options):
|
||||
"""Considering the rendering options, returns the src and data-zoom-image urls.
|
||||
|
||||
:return: src, src_zoom urls
|
||||
:rtype: tuple
|
||||
"""
|
||||
max_size = None
|
||||
if options.get('resize'):
|
||||
max_size = options.get('resize')
|
||||
@@ -56,6 +48,30 @@ class Image(models.AbstractModel):
|
||||
|
||||
src = '/web/image/%s/%s/%s%s/%s?unique=%s' % (record._name, record.id, options.get('preview_image', field_name), max_size, url_quote(filename), sha)
|
||||
|
||||
src_zoom = None
|
||||
if options.get('zoom') and getattr(record, options['zoom'], None):
|
||||
src_zoom = '/web/image/%s/%s/%s%s/%s?unique=%s' % (record._name, record.id, options['zoom'], max_size, url_quote(filename), sha)
|
||||
elif options.get('zoom'):
|
||||
src_zoom = options['zoom']
|
||||
|
||||
return src, src_zoom
|
||||
|
||||
@api.model
|
||||
def record_to_html(self, record, field_name, options):
|
||||
assert options['tagName'] != 'img',\
|
||||
"Oddly enough, the root tag of an image field can not be img. " \
|
||||
"That is because the image goes into the tag, or it gets the " \
|
||||
"hose again."
|
||||
|
||||
if options.get('qweb_img_raw_data', False):
|
||||
return super(Image, self).record_to_html(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 getattr(record, options['alt-field'], None):
|
||||
alt = escape(record[options['alt-field']])
|
||||
elif options.get('alt'):
|
||||
@@ -63,12 +79,6 @@ class Image(models.AbstractModel):
|
||||
else:
|
||||
alt = escape(record.display_name)
|
||||
|
||||
src_zoom = None
|
||||
if options.get('zoom') and getattr(record, options['zoom'], None):
|
||||
src_zoom = '/web/image/%s/%s/%s%s/%s?unique=%s' % (record._name, record.id, options['zoom'], max_size, url_quote(filename), sha)
|
||||
elif options.get('zoom'):
|
||||
src_zoom = options['zoom']
|
||||
|
||||
itemprop = None
|
||||
if options.get('itemprop'):
|
||||
itemprop = options['itemprop']
|
||||
@@ -96,3 +106,11 @@ class Image(models.AbstractModel):
|
||||
img.append('/>')
|
||||
|
||||
return u''.join(img)
|
||||
|
||||
class ImageUrlConverter(models.AbstractModel):
|
||||
_description = 'Qweb Field Image'
|
||||
_inherit = 'ir.qweb.field.image_url'
|
||||
|
||||
def _get_src_urls(self, record, field_name, options):
|
||||
image_url = record[options.get('preview_image', field_name)]
|
||||
return image_url, options.get("zoom", None)
|
||||
|
||||
@@ -385,6 +385,17 @@ class ImageConverter(models.AbstractModel):
|
||||
|
||||
return u'<img src="data:%s;base64,%s">' % (Image.MIME[image.format], value.decode('ascii'))
|
||||
|
||||
class ImageUrlConverter(models.AbstractModel):
|
||||
""" ``image_url`` widget rendering, inserts an image tag in the
|
||||
document.
|
||||
"""
|
||||
_name = 'ir.qweb.field.image_url'
|
||||
_description = 'Qweb Field Image'
|
||||
_inherit = 'ir.qweb.field.image'
|
||||
|
||||
@api.model
|
||||
def value_to_html(self, value, options):
|
||||
return u'<img src="%s">' % (value)
|
||||
|
||||
class MonetaryConverter(models.AbstractModel):
|
||||
""" ``monetary`` converter, has a mandatory option
|
||||
|
||||
Reference in New Issue
Block a user