summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorThanhDodeurOdoo <tso@odoo.com>2018-11-26 10:08:01 +0000
committerThanhDodeurOdoo <tso@odoo.com>2018-12-06 19:09:33 +0000
commit7d85ab1eac6dbf33d56c6a1f8bf7e9e12bb8008e (patch)
treef977a392fe9e9fe9a21c06d997c0d5b877cbce76 /addons
parent89f20d2c297d99d4eb0ded8be78fbf86b8ec45d3 (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')
-rw-r--r--addons/im_livechat/controllers/main.py3
-rw-r--r--addons/mail/controllers/main.py4
-rw-r--r--addons/web/controllers/main.py71
-rw-r--r--addons/website/models/ir_http.py21
-rw-r--r--addons/website_forum/controllers/main.py4
5 files changed, 32 insertions, 71 deletions
diff --git a/addons/im_livechat/controllers/main.py b/addons/im_livechat/controllers/main.py
index abe0e17bb0c..86553625f4d 100644
--- a/addons/im_livechat/controllers/main.py
+++ b/addons/im_livechat/controllers/main.py
@@ -6,7 +6,6 @@ import base64
from odoo import http, _
from odoo.http import request
from odoo.addons.base.models.assetsbundle import AssetsBundle
-from odoo.addons.web.controllers.main import binary_content
class LivechatController(http.Controller):
@@ -22,7 +21,7 @@ class LivechatController(http.Controller):
if isinstance(mock_attachment, list): # suppose that CSS asset will not required to be split in pages
mock_attachment = mock_attachment[0]
# can't use /web/content directly because we don't have attachment ids (attachments must be created)
- status, headers, content = binary_content(id=mock_attachment.id, unique=asset.checksum)
+ status, headers, content = request.env['ir.http'].binary_content(id=mock_attachment.id, unique=asset.checksum)
content_base64 = base64.b64decode(content) if content else ''
headers.append(('Content-Length', len(content_base64)))
return request.make_response(content_base64, headers)
diff --git a/addons/mail/controllers/main.py b/addons/mail/controllers/main.py
index 6694fbf72b1..38965dcf79b 100644
--- a/addons/mail/controllers/main.py
+++ b/addons/mail/controllers/main.py
@@ -9,7 +9,6 @@ import werkzeug
from werkzeug import url_encode
from odoo import api, http, registry, SUPERUSER_ID, _
-from odoo.addons.web.controllers.main import binary_content
from odoo.exceptions import AccessError
from odoo.http import request
from odoo.tools import consteq
@@ -216,7 +215,8 @@ class MailController(http.Controller):
# if the current user has access to the document, get the partner avatar as sudo()
request.env[res_model].browse(res_id).check_access_rule('read')
if partner_id in request.env[res_model].browse(res_id).sudo().exists().message_ids.mapped('author_id').ids:
- status, headers, _content = binary_content(model='res.partner', id=partner_id, field='image_medium', default_mimetype='image/png', env=request.env(user=SUPERUSER_ID))
+ status, headers, _content = request.env['ir.http'].sudo().binary_content(
+ model='res.partner', id=partner_id, field='image_medium', default_mimetype='image/png')
# binary content return an empty string and not a placeholder if obj[field] is False
if _content != '':
content = _content
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)
diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py
index 7ca7563159b..25becf9749c 100644
--- a/addons/website/models/ir_http.py
+++ b/addons/website/models/ir_http.py
@@ -237,25 +237,22 @@ class Http(models.AbstractModel):
html = request.env['ir.ui.view'].render_template('website.http_error', values)
return werkzeug.wrappers.Response(html, status=code, content_type='text/html;charset=utf-8')
- @classmethod
- def binary_content(cls, xmlid=None, model='ir.attachment', id=None, field='datas',
+ def binary_content(self, 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',
- access_token=None, related_id=None, access_mode=None, env=None):
- env = env or request.env
+ access_token=None):
obj = None
if xmlid:
- obj = cls._xmlid_to_obj(env, xmlid)
- elif id and model in env:
- obj = env[model].browse(int(id))
+ obj = self._xmlid_to_obj(self.env, xmlid)
+ elif id and model in self.env:
+ obj = self.env[model].browse(int(id))
if obj and 'website_published' in obj._fields:
- if env[obj._name].sudo().search([('id', '=', obj.id), ('website_published', '=', True)]):
- env = env(user=SUPERUSER_ID)
- return super(Http, cls).binary_content(
+ if self.env[obj._name].sudo().search([('id', '=', obj.id), ('website_published', '=', True)]):
+ self.sudo()
+ return super(Http, self).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, access_token=access_token, related_id=related_id,
- access_mode=access_mode, env=env)
+ default_mimetype=default_mimetype, access_token=access_token)
@classmethod
def _xmlid_to_obj(cls, env, xmlid):
diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py
index 05c00e8df93..42b1e892fcc 100644
--- a/addons/website_forum/controllers/main.py
+++ b/addons/website_forum/controllers/main.py
@@ -12,7 +12,6 @@ from datetime import datetime
from odoo import http, modules, SUPERUSER_ID, tools, _
from odoo.addons.http_routing.models.ir_http import slug
-from odoo.addons.web.controllers.main import binary_content
from odoo.addons.website.models.ir_http import sitemap_qs2dom
from odoo.http import request
@@ -591,7 +590,8 @@ class WebsiteForum(http.Controller):
@http.route(['/forum/user/<int:user_id>/avatar'], type='http', auth="public", website=True, sitemap=False)
def user_avatar(self, user_id=0, **post):
- status, headers, content = binary_content(model='res.users', id=user_id, field='image_medium', default_mimetype='image/png', env=request.env(user=SUPERUSER_ID))
+ status, headers, content = request.env['ir.http'].sudo().binary_content(
+ model='res.users', id=user_id, field='image_medium', default_mimetype='image/png')
if not content:
img_path = modules.get_module_resource('web', 'static/src/img', 'placeholder.png')