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.

40 lines
1.4 KiB

  1. # Copyright 2016 Tecnativa - Pedro M. Baeza
  2. # Copyright 2018 Tecnativa - Carlos Dauden
  3. # Copyright 2018 ACSONE SA/NV
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import _, api, fields, models, exceptions
  6. from odoo.tools.safe_eval import safe_eval
  7. class ContractLineFormula(models.Model):
  8. _name = 'contract.line.qty.formula'
  9. _description = 'Contract Line Formula'
  10. name = fields.Char(required=True, translate=True)
  11. code = fields.Text(required=True, default="result = 0")
  12. @api.constrains('code')
  13. def _check_code(self):
  14. eval_context = {
  15. 'env': self.env,
  16. 'context': self.env.context,
  17. 'user': self.env.user,
  18. 'line': self.env['contract.line'],
  19. 'contract': self.env['contract.contract'],
  20. 'invoice': self.env['account.invoice'],
  21. 'quantity': 0,
  22. 'period_first_date': False,
  23. 'period_last_date': False,
  24. 'invoice_date': False,
  25. }
  26. try:
  27. safe_eval(
  28. self.code.strip(), eval_context, mode="exec", nocopy=True
  29. )
  30. except Exception as e:
  31. raise exceptions.ValidationError(
  32. _('Error evaluating code.\nDetails: %s') % e
  33. )
  34. if 'result' not in eval_context:
  35. raise exceptions.ValidationError(_('No valid result returned.'))