[IMP] Brought back /website/image route w/filename hinted in headers
authorFabien Meghazi <fme@openerp.com>
Wed, 17 Sep 2014 14:54:08 +0000 (16:54 +0200)
committerFabien Meghazi <fme@openerp.com>
Wed, 17 Sep 2014 14:54:08 +0000 (16:54 +0200)
Also added support for this route in widget Image#from_html()

addons/website/controllers/main.py
addons/website/models/ir_qweb.py
addons/website/models/website.py
addons/website_sale/static/src/js/website_sale.js

index 4fc90e4..c808141 100644 (file)
@@ -399,8 +399,8 @@ class Website(openerp.addons.web.controllers.main.Home):
 
     @http.route([
         '/website/image',
-        '/website/image/<model>-<id>-<field>',
-        '/website/image/<model>-<id>-<field>-<int:max_width>x<int:max_height>'
+        '/website/image/<model>/<id>/<field>',
+        '/website/image/<model>/<id>/<field>/<int:max_width>x<int:max_height>'
         ], auth="public", website=True)
     def website_image(self, model, id, field, max_width=None, max_height=None):
         """ Fetches the requested field and ensures it does not go above
index a4d94cb..40b85fd 100644 (file)
@@ -303,11 +303,15 @@ class Image(orm.AbstractModel):
         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)
index 691fc3e..b9db8bb 100644 (file)
@@ -573,14 +573,16 @@ class website(osv.osv):
             return response
 
         data = record[field].decode('base64')
+        image = Image.open(cStringIO.StringIO(data))
+        response.mimetype = Image.MIME[image.format]
+
+        filename = '%s_%s.%s' % (model.replace('.', '_'), id, str(image.format).lower())
+        response.headers['Content-Disposition'] = 'inline; filename="%s"' % filename
 
         if (not max_width) and (not max_height):
             response.data = data
             return response
 
-        image = Image.open(cStringIO.StringIO(data))
-        response.mimetype = Image.MIME[image.format]
-
         w, h = image.size
         max_w = int(max_width) if max_width else maxint
         max_h = int(max_height) if max_height else maxint
@@ -601,8 +603,8 @@ class website(osv.osv):
         """Returns a local url that points to the image field of a given browse record."""
         model = record._name
         id = '%s_%s' % (record.id, hashlib.sha1(record.sudo().write_date).hexdigest()[0:7])
-        size = '' if size is None else '-%s' % size
-        return '/website/image/%s-%s-%s%s' % (model, id, field, size)
+        size = '' if size is None else '/%s' % size
+        return '/website/image/%s/%s/%s%s' % (model, id, field, size)
 
 
 class website_menu(osv.osv):
index ae61e02..4336ca2 100644 (file)
@@ -121,7 +121,7 @@ $('.oe_website_sale').each(function () {
 
         if (product_id) {
             var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img');
-            $img.attr("src", "/website/image/product.product-" + product_id + "-image");
+            $img.attr("src", "/website/image/product.product/" + product_id + "/image");
             $img.parent().attr('data-oe-model', 'product.product').attr('data-oe-id', product_id)
                 .data('oe-model', 'product.product').data('oe-id', product_id);
         }