Files
odoo_source/addons/web_editor/models/ir_attachment.py
T
Sébastien Theys 87a35d4263 [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
2019-06-04 22:14:13 +02:00

54 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from werkzeug import url_quote
from odoo import api, models, fields, tools
class IrAttachment(models.Model):
_inherit = "ir.attachment"
local_url = fields.Char("Attachment URL", compute='_compute_local_url')
image_src = fields.Char(compute='_compute_image_src')
image_width = fields.Integer(compute='_compute_image_size')
image_height = fields.Integer(compute='_compute_image_size')
@api.one
def _compute_local_url(self):
if self.url:
self.local_url = self.url
else:
self.local_url = '/web/image/%s?unique=%s' % (self.id, self.checksum)
@api.multi
@api.depends('mimetype', 'url', 'name')
def _compute_image_src(self):
for attachment in self:
if attachment.mimetype not in ['image/gif', 'image/jpe', 'image/jpeg', 'image/jpg', 'image/gif', 'image/png', 'image/svg+xml']:
attachment.image_src = False
else:
attachment.image_src = attachment.url or '/web/image/%s/%s' % (
attachment.id,
url_quote(attachment.name or ''),
)
@api.multi
@api.depends('datas')
def _compute_image_size(self):
for attachment in self:
try:
image = tools.base64_to_image(attachment.datas)
attachment.image_width = image.width
attachment.image_height = image.height
except Exception:
attachment.image_width = 0
attachment.image_height = 0
@api.multi
def _get_media_info(self):
"""Return a dict with the values that we need on the media dialog."""
self.ensure_one()
return self.read(['id', 'name', 'mimetype', 'checksum', 'url', 'type', 'res_id', 'res_model', 'public', 'access_token', 'image_src', 'image_width', 'image_height'])[0]