server-ux/base_tier_validation_formula/models/tier_review.py
2021-03-23 20:38:33 +01:00

48 lines
1.8 KiB
Python

# Copyright 2019 Eficent Business and IT Consulting Services S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from flectra import api, fields, models, _
from flectra.exceptions import UserError
from flectra.tools.safe_eval import safe_eval
class TierReview(models.Model):
_inherit = "tier.review"
python_reviewer_ids = fields.Many2many(
string="Reviewers from Python expression", comodel_name="res.users",
compute="_compute_python_reviewer_ids", store=True
)
@api.model
def _get_reviewer_fields(self):
res = super()._get_reviewer_fields()
return res + ['python_reviewer_ids']
def _get_reviewers(self):
return super()._get_reviewers() + self.python_reviewer_ids
@api.multi
@api.depends('definition_id.reviewer_expression',
'review_type', 'model', 'res_id')
def _compute_python_reviewer_ids(self):
for rec in self:
if rec.review_type == 'expression':
record = rec.env[rec.model].browse(rec.res_id).exists()
try:
reviewer_ids = safe_eval(
rec.definition_id.reviewer_expression,
globals_dict={'rec': record})
except Exception as error:
raise UserError(_(
"Error evaluating tier validation "
"conditions.\n %s") % error)
# Check if python expression returns 'res.users' recordset
if not isinstance(reviewer_ids, models.Model) or \
reviewer_ids._name != 'res.users':
raise UserError(_(
"Reviewer python expression must return a "
"res.users recordset."))
else:
rec.python_reviewer_ids = reviewer_ids