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.

47 lines
1.8 KiB

  1. # Copyright 2019 Eficent Business and IT Consulting Services S.L.
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import UserError
  5. from odoo.tools.safe_eval import safe_eval
  6. class TierReview(models.Model):
  7. _inherit = "tier.review"
  8. python_reviewer_ids = fields.Many2many(
  9. string="Reviewers from Python expression", comodel_name="res.users",
  10. compute="_compute_python_reviewer_ids", store=True
  11. )
  12. @api.model
  13. def _get_reviewer_fields(self):
  14. res = super()._get_reviewer_fields()
  15. return res + ['python_reviewer_ids']
  16. def _get_reviewers(self):
  17. return super()._get_reviewers() + self.python_reviewer_ids
  18. @api.multi
  19. @api.depends('definition_id.reviewer_expression',
  20. 'review_type', 'model', 'res_id')
  21. def _compute_python_reviewer_ids(self):
  22. for rec in self:
  23. if rec.review_type == 'expression':
  24. record = rec.env[rec.model].browse(rec.res_id).exists()
  25. try:
  26. reviewer_ids = safe_eval(
  27. rec.definition_id.reviewer_expression,
  28. globals_dict={'rec': record})
  29. except Exception as error:
  30. raise UserError(_(
  31. "Error evaluating tier validation "
  32. "conditions.\n %s") % error)
  33. # Check if python expression returns 'res.users' recordset
  34. if not isinstance(reviewer_ids, models.Model) or \
  35. reviewer_ids._name != 'res.users':
  36. raise UserError(_(
  37. "Reviewer python expression must return a "
  38. "res.users recordset."))
  39. else:
  40. rec.python_reviewer_ids = reviewer_ids