summaryrefslogtreecommitdiff
path: root/addons/web/models
diff options
context:
space:
mode:
authorVictor Feyens <vfe@odoo.com>2020-09-02 08:50:45 +0000
committerVictor Feyens <vfe@odoo.com>2020-09-03 08:40:30 +0000
commit310fb4b7c5e2a6205b59de76356f14c3d562cee9 (patch)
treeb39ca79ee85cc84a8b8b19efe373abb180603819 /addons/web/models
parent4ed7bb50487acb88d0a817015064e917e1a44a1c (diff)
[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.
Diffstat (limited to 'addons/web/models')
-rw-r--r--addons/web/models/ir_qweb.py56
1 files changed, 37 insertions, 19 deletions
diff --git a/addons/web/models/ir_qweb.py b/addons/web/models/ir_qweb.py
index 875899f7d57..51694a3bc42 100644
--- a/addons/web/models/ir_qweb.py
+++ b/addons/web/models/ir_qweb.py
@@ -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)