/
ir_attachment.py
91 lines (77 loc) · 3.75 KB
/
ir_attachment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from werkzeug.urls import url_quote
from odoo import api, models, fields, tools
from odoo.addons.web_editor.tools import find_in_html_field
SUPPORTED_IMAGE_MIMETYPES = ['image/gif', 'image/jpe', 'image/jpeg', 'image/jpg', 'image/png', 'image/svg+xml']
SUPPORTED_IMAGE_EXTENSIONS = ['.gif', '.jpe', '.jpeg', '.jpg', '.png', '.svg']
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')
original_id = fields.Many2one('ir.attachment', string="Original (unoptimized, unresized) attachment", index=True)
def _compute_local_url(self):
for attachment in self:
if attachment.url:
attachment.local_url = attachment.url
else:
attachment.local_url = '/web/image/%s?unique=%s' % (attachment.id, attachment.checksum)
@api.depends('mimetype', 'url', 'name')
def _compute_image_src(self):
for attachment in self:
# Only add a src for supported images
if attachment.mimetype not in SUPPORTED_IMAGE_MIMETYPES:
attachment.image_src = False
continue
if attachment.type == 'url':
attachment.image_src = attachment.url
else:
# Adding unique in URLs for cache-control
unique = attachment.checksum[:8]
if attachment.url:
# For attachments-by-url, unique is used as a cachebuster. They
# currently do not leverage max-age headers.
separator = '&' if '?' in attachment.url else '?'
attachment.image_src = '%s%sunique=%s' % (attachment.url, separator, unique)
else:
name = url_quote(attachment.name)
attachment.image_src = '/web/image/%s-%s/%s' % (attachment.id, unique, name)
@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
def _get_media_info(self):
"""Return a dict with the values that we need on the media dialog."""
self.ensure_one()
return self._read_format(['id', 'name', 'description', 'mimetype', 'checksum', 'url', 'type', 'res_id', 'res_model', 'public', 'access_token', 'image_src', 'image_width', 'image_height', 'original_id'])[0]
def _is_used_in_html_field(self):
"""Returns a model where this attachment is used, or a dict with model names or False."""
self.ensure_one()
# Keep significant part of attachment url
url = self.local_url
is_default_local_url = url.startswith('/web/image/%s?' % self.id)
if is_default_local_url:
url = ('/web/image/%s' if self.image_src else '/web/content/%s') % self.id
# in-document URLs are html-escaped, a straight search will not
# find them
url = tools.html_escape(url)
likes = [
'"%s"' % url,
"'%s'" % url,
]
if is_default_local_url:
likes.extend([
'"%s-' % url,
"'%s-" % url,
'"%s?' % url,
"'%s?" % url,
])
return find_in_html_field(self.env, likes)