diff options
| author | Sébastien Theys <seb@odoo.com> | 2019-06-04 15:11:16 +0200 |
|---|---|---|
| committer | Sébastien Theys <seb@odoo.com> | 2019-06-04 22:14:13 +0200 |
| commit | 87a35d42636e31642c8d1d0170c03c2689867a1e (patch) | |
| tree | 2d5b540d1c12c312d5132460a14c1b6f162485d4 /addons/web_editor/controllers | |
| parent | 76cf2ae82a45b9ffcf63b524944a42b7f5192b0c (diff) | |
[IMP] web, web_editor, *: add media image optimize dialog
* = website, website_blog
The goal is to give the user an opportunity to optimize his images before using
them.
For this we introduce a preview/configuration dialog after every image upload,
where appropriate default values are filled for the quality and resolution,
based on where the image is going to be used. Since this is not going to be
perfect all the time, we still allow the user to configure them, and we display
a preview to ease this process.
Indeed it is important for SEO and for usability in general that the images are
as light as possible in size.
Technically the original image is uploaded and saved first, and then it can be
optimized. This way the upload only happens once, and the preview can be
computed from the already saved image.
task-1930726
PR: #31208
Diffstat (limited to 'addons/web_editor/controllers')
| -rw-r--r-- | addons/web_editor/controllers/main.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index 85797ba2e23..bf169fac138 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -153,12 +153,11 @@ class Web_Editor(http.Controller): return True @http.route('/web_editor/attachment/add_data', type='json', auth='user', methods=['POST'], website=True) - def add_data(self, name, data, disable_optimization=False, res_id=False, res_model='ir.ui.view', filters=False, **kwargs): - if not disable_optimization: - try: - data = tools.image_process(data, verify_resolution=True) - except OSError: - pass # not an image + def add_data(self, name, data, quality=0, width=0, height=0, res_id=False, res_model='ir.ui.view', filters=False, **kwargs): + try: + data = tools.image_process(data, size=(width, height), quality=quality, verify_resolution=True) + except OSError: + pass # not an image attachment = self._attachment_create(name=name, data=data, res_id=res_id, res_model=res_model, filters=filters) return attachment._get_media_info() @@ -167,6 +166,22 @@ class Web_Editor(http.Controller): attachment = self._attachment_create(url=url, res_id=res_id, res_model=res_model, filters=filters) return attachment._get_media_info() + @http.route('/web_editor/attachment/<model("ir.attachment"):attachment>/update', type='json', auth='user', methods=['POST'], website=True) + def attachment_update(self, attachment, name=None, width=0, height=0, quality=0, copy=False, **kwargs): + if attachment.type == 'url': + raise UserError(_("You cannot change the quality, the width or the name of an URL attachment.")) + if copy: + attachment = attachment.copy() + data = {} + if name: + data['name'] = name + try: + data['datas'] = tools.image_process(attachment.datas, size=(width, height), quality=quality) + except OSError: + pass # not an image + attachment.write(data) + return attachment._get_media_info() + @http.route('/web_editor/attachment/remove', type='json', auth='user', website=True) def remove(self, ids, **kwargs): """ Removes a web-based image attachment if it is used by no view (template) |
