From 307f51ab8597ca14e374f809401f0cf84dcde954 Mon Sep 17 00:00:00 2001 From: David Beguin <dbe@odoo.com> Date: Wed, 15 Jan 2020 09:20:14 +0000 Subject: [PATCH] [IMP] web: split _content_image with get response sub method This commit splits the _content_image method to allow to call the get response part individually. This is needed because _content_image call binary_content using current user access. But in some cases, we need to render a binary content even if the user does not have access to the target model (typically for public users). The binary_content is gotten in sudo mode where needed and the result can be given to the _content_image_get_response. This will avoid code duplication where the sudo use case is met. Usage : This commit prepare the redesign of survey. This _content_image_get_response method will be called to grant access of background image even for public users. Other modules will use this new method like elearning (website_slides) to display karma ranking, etc. Task ID: '2150291' PR #43237 --- addons/purchase/controllers/portal.py | 2 +- addons/web/controllers/main.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/addons/purchase/controllers/portal.py b/addons/purchase/controllers/portal.py index bccc25331d0a..ed4672bed5c8 100644 --- a/addons/purchase/controllers/portal.py +++ b/addons/purchase/controllers/portal.py @@ -27,7 +27,7 @@ class CustomerPortal(CustomerPortal): # def resize_to_48(b64source): if not b64source: - b64source = base64.b64encode(Binary().placeholder()) + b64source = base64.b64encode(Binary.placeholder()) return image_process(b64source, size=(48, 48)) values = { diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 90657b2e7647..cdc2fa3b9501 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -1365,7 +1365,8 @@ class View(http.Controller): class Binary(http.Controller): - def placeholder(self, image='placeholder.png'): + @staticmethod + def placeholder(image='placeholder.png'): with tools.file_open(get_resource_path('web', 'static/src/img', image), 'rb') as fd: return fd.read() @@ -1442,10 +1443,17 @@ class Binary(http.Controller): 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) + + @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'): 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(self.placeholder(image=placeholder)) + image_base64 = base64.b64encode(Binary.placeholder(image=placeholder)) if not (width or height): width, height = odoo.tools.image_guess_size_from_field_name(field) -- GitLab