[MERGE] forward port of branch 8.0 up to e883193
[odoo/odoo.git] / addons / website / models / ir_qweb.py
index 1e328e5..c755e61 100644 (file)
@@ -307,32 +307,32 @@ class Image(orm.AbstractModel):
 
     def record_to_html(self, cr, uid, field_name, record, column, options=None, context=None):
         if options is None: options = {}
-        classes = ['img', 'img-responsive'] + options.get('class', '').split()
-
-        url_frags = {
-            'classes': ' '.join(itertools.imap(escape, classes)),
-            'model': record._model._name,
-            'id': record.id,
-            'field': field_name,
-            'max_size': '',
-        }
+        aclasses = ['img', 'img-responsive'] + options.get('class', '').split()
+        classes = ' '.join(itertools.imap(escape, aclasses))
+
+        max_size = None
         max_width, max_height = options.get('max_width', 0), options.get('max_height', 0)
         if max_width or max_height:
-            url_frags['max_size'] = '/%sx%s' % (max_width, max_height)
+            max_size = '%sx%s' % (max_width, max_height)
 
-        img = '<img class="%(classes)s" src="/website/image/%(model)s/%(id)s/%(field)s%(max_size)s"/>'
-        return ir_qweb.HTMLSafe(img % url_frags)
+        src = self.pool['website'].image_url(cr, uid, record, field_name, max_size)
+        img = '<img class="%s" src="%s"/>' % (classes, src)
+        return ir_qweb.HTMLSafe(img)
 
     local_url_re = re.compile(r'^/(?P<module>[^]]+)/static/(?P<rest>.+)$')
     def from_html(self, cr, uid, model, column, element, context=None):
         url = element.find('img').get('src')
 
         url_object = urlparse.urlsplit(url)
-        query = dict(urlparse.parse_qsl(url_object.query))
-        if url_object.path == '/website/image':
-            item = self.pool[query['model']].browse(
-                cr, uid, int(query['id']), context=context)
-            return item[query['field']]
+        if url_object.path.startswith('/website/image'):
+            # url might be /website/image/<model>/<id>[_<checksum>]/<field>[/<width>x<height>]
+            fragments = url_object.path.split('/')
+            query = dict(urlparse.parse_qsl(url_object.query))
+            model = query.get('model', fragments[3])
+            oid = query.get('id', fragments[4].split('_')[0])
+            field = query.get('field', fragments[5])
+            item = self.pool[model].browse(cr, uid, int(oid), context=context)
+            return item[field]
 
         if self.local_url_re.match(url_object.path):
             return self.load_local_url(url)