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.

101 lines
3.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2014 Serv. Tecnol. Avanzados (http://www.serviciosbaeza.com)
  3. # Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
  4. # © 2015 Antiun Ingeniería S.L. - Jairo Llopis
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from openerp import _, api, fields, models, tools
  7. class Owner(models.AbstractModel):
  8. _name = "base_multi_image.owner"
  9. image_ids = fields.One2many(
  10. comodel_name='base_multi_image.image',
  11. inverse_name='owner_id',
  12. string='Images',
  13. domain=lambda self: [("owner_model", "=", self._name)],
  14. copy=True)
  15. image_main = fields.Binary(
  16. string="Main image",
  17. store=False,
  18. compute="_get_multi_image",
  19. inverse="_set_multi_image_main")
  20. image_main_medium = fields.Binary(
  21. string="Medium image",
  22. compute="_get_multi_image",
  23. inverse="_set_multi_image_main_medium",
  24. store=False)
  25. image_main_small = fields.Binary(
  26. string="Small image",
  27. compute="_get_multi_image",
  28. inverse="_set_multi_image_main_small",
  29. store=False)
  30. @api.multi
  31. @api.depends('image_ids')
  32. def _get_multi_image(self):
  33. """Get the main image for this object.
  34. This is provided as a compatibility layer for submodels that already
  35. had one image per record.
  36. """
  37. for s in self:
  38. s.image_main = False
  39. s.image_main_medium = False
  40. s.image_main_small = False
  41. if s.image_ids:
  42. s.image_main = s.image_ids[0].image_main
  43. s.image_main_medium = s.image_ids[0].image_medium
  44. s.image_main_small = s.image_ids[0].image_small
  45. @api.multi
  46. def _set_multi_image(self, image=False, name=False):
  47. """Save or delete the main image for this record.
  48. This is provided as a compatibility layer for submodels that already
  49. had one image per record.
  50. """
  51. # Values to save
  52. values = {
  53. "storage": "db",
  54. "file_db_store": tools.image_resize_image_big(image),
  55. "owner_model": self._name,
  56. }
  57. if name:
  58. values["name"] = name
  59. for s in self:
  60. if image:
  61. values["owner_id"] = s.id
  62. # Editing
  63. if s.image_ids:
  64. s.image_ids[0].write(values)
  65. # Adding
  66. else:
  67. values.setdefault("name", name or _("Main image"))
  68. s.image_ids = [(0, 0, values)]
  69. # Deleting
  70. elif s.image_ids:
  71. s.image_ids[0].unlink()
  72. @api.multi
  73. def _set_multi_image_main(self):
  74. self._set_multi_image(self.image_main)
  75. @api.multi
  76. def _set_multi_image_main_medium(self):
  77. self._set_multi_image(self.image_main_medium)
  78. @api.multi
  79. def _set_multi_image_main_small(self):
  80. self._set_multi_image(self.image_main_small)
  81. @api.multi
  82. def unlink(self):
  83. """Mimic `ondelete="cascade"` for multi images."""
  84. images = self.mapped("image_ids")
  85. result = super(Owner, self).unlink()
  86. if result:
  87. images.unlink()
  88. return result