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.

67 lines
2.4 KiB

  1. # Copyright 2019 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
  3. from odoo import fields, models
  4. from odoo.tools.safe_eval import safe_eval
  5. class AbstractConditionalImage(models.AbstractModel):
  6. _name = 'abstract.conditional.image'
  7. _description = 'Abstract image'
  8. image = fields.Binary(
  9. compute='_compute_image', string="Image",
  10. store=False, readonly=True
  11. )
  12. image_medium = fields.Binary(
  13. compute='_compute_image', string="Medium-sized image",
  14. store=False, readonly=True
  15. )
  16. image_small = fields.Binary(
  17. compute='_compute_image', string="Small-sized image",
  18. store=False, readonly=True
  19. )
  20. @staticmethod
  21. def _compute_selector_test_without_company(image_selector, record):
  22. return bool(
  23. safe_eval(image_selector.selector or "True", {'object': record})
  24. )
  25. @staticmethod
  26. def _compute_selector_test_with_company(image_selector, record):
  27. if (image_selector.company_id == record.company_id or
  28. record.company_id and not image_selector.company_id):
  29. return AbstractConditionalImage\
  30. ._compute_selector_test_without_company(
  31. image_selector, record
  32. )
  33. return False
  34. def _compute_image(self):
  35. if 'company_id' in self._fields:
  36. search_clause = [('model_name', '=', self._name)]
  37. test_method = self._compute_selector_test_with_company
  38. else:
  39. # If inherited object doesn't have a `company_id` field,
  40. # remove the items with a company defined and the related checks
  41. search_clause = [('model_name', '=', self._name),
  42. ('company_id', '=', False)]
  43. test_method = self._compute_selector_test_without_company
  44. image_selectors = self.env['image'].search(
  45. search_clause, order='company_id, selector'
  46. )
  47. for rec in self:
  48. found = None
  49. for image_selector in image_selectors:
  50. if test_method(image_selector, rec):
  51. found = image_selector
  52. break
  53. rec.update({
  54. 'image': found and found.image,
  55. 'image_medium': found and found.image_medium,
  56. 'image_small': found and found.image_small,
  57. })