[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
This commit is contained in:
David Beguin
2020-01-17 10:25:20 +00:00
parent ded9d60986
commit 307f51ab85
2 changed files with 11 additions and 3 deletions
+1 -1
View File
@@ -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 = {
+10 -2
View File
@@ -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)