ir_attachment.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from werkzeug.urls import url_quote
  4. from odoo import api, models, fields, tools
  5. SUPPORTED_IMAGE_MIMETYPES = ['image/gif', 'image/jpe', 'image/jpeg', 'image/jpg', 'image/png', 'image/svg+xml']
  6. SUPPORTED_IMAGE_EXTENSIONS = ['.gif', '.jpe', '.jpeg', '.jpg', '.png', '.svg']
  7. class IrAttachment(models.Model):
  8. _inherit = "ir.attachment"
  9. local_url = fields.Char("Attachment URL", compute='_compute_local_url')
  10. image_src = fields.Char(compute='_compute_image_src')
  11. image_width = fields.Integer(compute='_compute_image_size')
  12. image_height = fields.Integer(compute='_compute_image_size')
  13. original_id = fields.Many2one('ir.attachment', string="Original (unoptimized, unresized) attachment")
  14. def _compute_local_url(self):
  15. for attachment in self:
  16. if attachment.url:
  17. attachment.local_url = attachment.url
  18. else:
  19. attachment.local_url = '/web/image/%s?unique=%s' % (attachment.id, attachment.checksum)
  20. @api.depends('mimetype', 'url', 'name')
  21. def _compute_image_src(self):
  22. for attachment in self:
  23. # Only add a src for supported images
  24. if attachment.mimetype not in SUPPORTED_IMAGE_MIMETYPES:
  25. attachment.image_src = False
  26. continue
  27. if attachment.type == 'url':
  28. attachment.image_src = attachment.url
  29. else:
  30. # Adding unique in URLs for cache-control
  31. unique = attachment.checksum[:8]
  32. if attachment.url:
  33. # For attachments-by-url, unique is used as a cachebuster. They
  34. # currently do not leverage max-age headers.
  35. separator = '&' if '?' in attachment.url else '?'
  36. attachment.image_src = '%s%sunique=%s' % (attachment.url, separator, unique)
  37. else:
  38. name = url_quote(attachment.name)
  39. attachment.image_src = '/web/image/%s-%s/%s' % (attachment.id, unique, name)
  40. @api.depends('datas')
  41. def _compute_image_size(self):
  42. for attachment in self:
  43. try:
  44. image = tools.base64_to_image(attachment.datas)
  45. attachment.image_width = image.width
  46. attachment.image_height = image.height
  47. except Exception:
  48. attachment.image_width = 0
  49. attachment.image_height = 0
  50. def _get_media_info(self):
  51. """Return a dict with the values that we need on the media dialog."""
  52. self.ensure_one()
  53. 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]
  54. def _can_bypass_rights_on_media_dialog(self, **attachment_data):
  55. """ This method is meant to be overridden, for instance to allow to
  56. create image attachment despite the user not allowed to create
  57. attachment, eg:
  58. - Portal user uploading an image on the forum (bypass acl)
  59. - Non admin user uploading an unsplash image (bypass binary/url check)
  60. """
  61. return False