diff options
Diffstat (limited to 'addons/web/controllers/main.py')
| -rw-r--r-- | addons/web/controllers/main.py | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index cdc2fa3b950..3041058826b 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1367,7 +1367,8 @@ class Binary(http.Controller): @staticmethod def placeholder(image='placeholder.png'): - with tools.file_open(get_resource_path('web', 'static/src/img', image), 'rb') as fd: + image_path = image.lstrip('/').split('/') if '/' in image else ['web', 'static', 'src', 'img', image] + with tools.file_open(get_resource_path(*image_path), 'rb') as fd: return fd.read() @http.route(['/web/content', @@ -1437,23 +1438,35 @@ class Binary(http.Controller): def _content_image(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename_field='name', unique=None, filename=None, mimetype=None, download=None, width=0, height=0, crop=False, quality=0, access_token=None, - placeholder='placeholder.png', **kwargs): + placeholder=None, **kwargs): status, headers, image_base64 = request.env['ir.http'].binary_content( xmlid=xmlid, model=model, id=id, field=field, unique=unique, filename=filename, filename_field=filename_field, download=download, mimetype=mimetype, default_mimetype='image/png', access_token=access_token) - return Binary._content_image_get_response(status, headers, image_base64, field=field, download=download, - width=width, height=height, crop=crop, quality=quality, - placeholder=placeholder) + return Binary._content_image_get_response( + status, headers, image_base64, model=model, id=id, field=field, download=download, + width=width, height=height, crop=crop, quality=quality, + placeholder=placeholder) @staticmethod - def _content_image_get_response(status, headers, image_base64, field='datas', download=None, - width=0, height=0, crop=False, quality=0, placeholder='placeholder.png'): + def _content_image_get_response( + status, headers, image_base64, model='ir.attachment', id=None, + field='datas', download=None, width=0, height=0, crop=False, + quality=0, placeholder='placeholder.png'): if status in [301, 304] or (status != 200 and download): return request.env['ir.http']._response_by_status(status, headers, image_base64) if not image_base64: - image_base64 = base64.b64encode(Binary.placeholder(image=placeholder)) + if placeholder is None and model in request.env: + # Try to browse the record in case a specific placeholder + # is supposed to be used. (eg: Unassigned users on a task) + record = request.env[model].browse(int(id)) if id else request.env[model] + placeholder_filename = record._get_placeholder_filename(field=field) + placeholder_content = Binary.placeholder(image=placeholder_filename) + else: + placeholder_content = Binary.placeholder() + image_base64 = base64.b64encode(placeholder_content) + if not (width or height): width, height = odoo.tools.image_guess_size_from_field_name(field) |
