diff options
| author | ThanhDodeurOdoo <tso@odoo.com> | 2018-11-26 10:08:01 +0000 |
|---|---|---|
| committer | ThanhDodeurOdoo <tso@odoo.com> | 2018-12-06 19:09:33 +0000 |
| commit | 7d85ab1eac6dbf33d56c6a1f8bf7e9e12bb8008e (patch) | |
| tree | f977a392fe9e9fe9a21c06d997c0d5b877cbce76 /addons/web | |
| parent | 89f20d2c297d99d4eb0ded8be78fbf86b8ec45d3 (diff) | |
[REF] base, *: refactor binary_content
*: tools, web, website, website_forum, mail, im_livechat
This commit refactors ir_http to make it more readable
and flexible.
Move the resize function of web/image to odoo.tools
Co-authored-by: XavierDo <xdo@odoo.com>
Co-authored-by: Antony Lesuisse <al@openerp.com>
closes: #28563
task: #1908896
Diffstat (limited to 'addons/web')
| -rw-r--r-- | addons/web/controllers/main.py | 71 |
1 files changed, 18 insertions, 53 deletions
diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 64cf2ca9ac5..57c86a2c6d6 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -36,7 +36,7 @@ import odoo import odoo.modules.registry from odoo.api import call_kw, Environment from odoo.modules import get_resource_path -from odoo.tools import crop_image, topological_sort, html_escape, pycompat +from odoo.tools import limited_image_resize, topological_sort, html_escape, pycompat from odoo.tools.mimetypes import guess_mimetype from odoo.tools.translate import _ from odoo.tools.misc import str2bool, xlwt, file_open @@ -424,16 +424,6 @@ def xml2json_from_elementtree(el, preserve_whitespaces=False): res["children"] = kids return res -def binary_content(xmlid=None, model='ir.attachment', id=None, field='datas', unique=False, - filename=None, filename_field='datas_fname', download=False, mimetype=None, - default_mimetype='application/octet-stream', related_id=None, access_mode=None, access_token=None, - env=None): - return request.registry['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=default_mimetype, related_id=related_id, access_mode=access_mode, access_token=access_token, - env=env) - #---------------------------------------------------------- # Odoo Web web Controllers #---------------------------------------------------------- @@ -1029,18 +1019,14 @@ class Binary(http.Controller): '/web/content/<string:model>/<int:id>/<string:field>/<string:filename>'], type='http', auth="public") def content_common(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename=None, filename_field='datas_fname', unique=None, mimetype=None, - download=None, data=None, token=None, access_token=None, related_id=None, access_mode=None, - **kw): - status, headers, content = binary_content( + download=None, data=None, token=None, access_token=None, **kw): + + status, headers, content = 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, - access_token=access_token, related_id=related_id, access_mode=access_mode) - if status == 304: - response = werkzeug.wrappers.Response(status=status, headers=headers) - elif status == 301: - return werkzeug.utils.redirect(content, code=301) - elif status != 200: - response = request.not_found() + filename_field=filename_field, download=download, mimetype=mimetype, access_token=access_token) + + if status != 200: + return request.env['ir.http']._response_by_status(status, headers, content) else: content_base64 = base64.b64decode(content) headers.append(('Content-Length', len(content_base64))) @@ -1068,39 +1054,18 @@ class Binary(http.Controller): '/web/image/<int:id>-<string:unique>/<int:width>x<int:height>/<string:filename>'], type='http', auth="public") def content_image(self, xmlid=None, model='ir.attachment', id=None, field='datas', filename_field='datas_fname', unique=None, filename=None, mimetype=None, - download=None, width=0, height=0, crop=False, related_id=None, access_mode=None, - access_token=None, avoid_if_small=False, upper_limit=False, signature=False): - status, headers, content = binary_content( + download=None, width=0, height=0, crop=False, access_token=None, avoid_if_small=False, + upper_limit=False, **kw): + status, headers, content = 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', related_id=related_id, access_mode=access_mode, access_token=access_token) - if status == 304: - return werkzeug.wrappers.Response(status=304, headers=headers) - elif status == 301: - return werkzeug.utils.redirect(content, code=301) - elif status != 200 and download: - return request.not_found() - - if headers and dict(headers).get('Content-Type', '') == 'image/svg+xml': # we shan't resize svg images - height = 0 - width = 0 - else: - height = int(height or 0) - width = int(width or 0) - - if crop and (width or height): - content = crop_image(content, type='center', size=(width, height), ratio=(1, 1)) - - elif content and (width or height): - if not upper_limit: - # resize maximum 500*500 - if width > 500: - width = 500 - if height > 500: - height = 500 - content = odoo.tools.image_resize_image(base64_source=content, size=(width or None, height or None), - encoding='base64', upper_limit=upper_limit, - avoid_if_small=avoid_if_small) + default_mimetype='image/png', access_token=access_token) + + if status != 200 and download: + return request.env['ir.http']._response_by_status(status, headers, content) + + content = limited_image_resize( + content, width=width, height=height, crop=crop, upper_limit=upper_limit, avoid_if_small=avoid_if_small) if content: image_base64 = base64.b64decode(content) |
