You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.3 KiB

  1. # Copyright 2019 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
  3. from odoo import api, fields, models, tools, _
  4. from odoo.exceptions import ValidationError
  5. class Image(models.Model):
  6. _name = 'image'
  7. name = fields.Char('Name', required=True)
  8. model_name = fields.Char('Model Name', required=True)
  9. selector = fields.Text(
  10. 'Selector',
  11. help='Python expression used as selector when multiple images are used'
  12. 'for the same object. The variable `object` refers '
  13. 'to the actual record on which the expression will be executed. '
  14. 'An empty expression will always return `True`.'
  15. )
  16. company_id = fields.Many2one(
  17. 'res.company', 'Company',
  18. help='Company related check. If inherited object does not have a '
  19. '`company_id` field, it will be ignored. '
  20. 'The check will first take the records with a company then, '
  21. 'if no match is found, the ones without a company.'
  22. )
  23. # image: all image fields are base64 encoded and PIL-supported
  24. image = fields.Binary(
  25. "Image", attachment=True,
  26. help="This field holds the standard image, limited to 1024x1024px"
  27. )
  28. image_medium = fields.Binary(
  29. "Medium-sized image", attachment=True,
  30. help="Medium-sized image. It is automatically "
  31. "resized as a 128x128px image, with aspect ratio preserved. "
  32. "Use this field in form views or some kanban views."
  33. )
  34. image_small = fields.Binary(
  35. "Small-sized image", attachment=True,
  36. help="Small-sized image. It is automatically "
  37. "resized as a 64x64px image, with aspect ratio preserved. "
  38. "Use this field anywhere a small image is required."
  39. )
  40. @api.model
  41. def _process_images(self, vals, required=False):
  42. if set(['image', 'image_medium', 'image_small']) & set(vals.keys()):
  43. tools.image_resize_images(vals)
  44. elif required:
  45. raise ValidationError(
  46. _('At least one image type must be specified')
  47. )
  48. @api.model
  49. def create(self, vals):
  50. self._process_images(vals, required=True)
  51. return super().create(vals)
  52. @api.multi
  53. def write(self, vals):
  54. self._process_images(vals)
  55. return super().write(vals)