[IMP] Brought back /website/image route w/filename hinted in headers

Also added support for this route in widget Image#from_html()
This commit is contained in:
Fabien Meghazi 2014-09-17 16:54:08 +02:00
parent e5bff82aff
commit 51477fb4f6
4 changed files with 19 additions and 13 deletions

View 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

View 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)

View 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):

View 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);
}