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.

98 lines
3.0 KiB

9 years ago
  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 odoo 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. first = s.image_ids[:1]
  39. s.image_main = first.image_main
  40. s.image_main_medium = first.image_medium
  41. s.image_main_small = first.image_small
  42. @api.multi
  43. def _set_multi_image(self, image=False, name=False):
  44. """Save or delete the main image for this record.
  45. This is provided as a compatibility layer for submodels that already
  46. had one image per record.
  47. """
  48. # Values to save
  49. values = {
  50. "storage": "db",
  51. "file_db_store": tools.image_resize_image_big(image),
  52. "owner_model": self._name,
  53. }
  54. if name:
  55. values["name"] = name
  56. for s in self:
  57. if image:
  58. values["owner_id"] = s.id
  59. # Editing
  60. if s.image_ids:
  61. s.image_ids[0].write(values)
  62. # Adding
  63. else:
  64. values.setdefault("name", name or _("Main image"))
  65. s.image_ids = [(0, 0, values)]
  66. # Deleting
  67. elif s.image_ids:
  68. s.image_ids[0].unlink()
  69. @api.multi
  70. def _set_multi_image_main(self):
  71. self._set_multi_image(self.image_main)
  72. @api.multi
  73. def _set_multi_image_main_medium(self):
  74. self._set_multi_image(self.image_main_medium)
  75. @api.multi
  76. def _set_multi_image_main_small(self):
  77. self._set_multi_image(self.image_main_small)
  78. @api.multi
  79. def unlink(self):
  80. """Mimic `ondelete="cascade"` for multi images."""
  81. images = self.mapped("image_ids")
  82. result = super(Owner, self).unlink()
  83. if result:
  84. images.unlink()
  85. return result