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.

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