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.

255 lines
9.0 KiB

7 years ago
7 years ago
  1. # Copyright 2011 Raphaël Valyi, Renato Lima, Guewen Baconnier, Sodexis
  2. # Copyright 2017 Akretion (http://www.akretion.com)
  3. # Mourad EL HADJ MIMOUNE <mourad.elhadj.mimoune@akretion.com>
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. import time
  6. from functools import wraps
  7. from odoo import api, fields, models, _
  8. import logging
  9. from odoo.exceptions import UserError, ValidationError
  10. from odoo.tools.safe_eval import safe_eval
  11. _logger = logging.getLogger(__name__)
  12. def implemented_by_base_exception(func):
  13. """Call a prefixed function based on 'namespace'."""
  14. @wraps(func)
  15. def wrapper(cls, *args, **kwargs):
  16. fun_name = func.__name__
  17. fun = '_%s%s' % (cls.rule_group, fun_name)
  18. if not hasattr(cls, fun):
  19. fun = '_default%s' % (fun_name)
  20. return getattr(cls, fun)(*args, **kwargs)
  21. return wrapper
  22. class ExceptionRule(models.Model):
  23. _name = 'exception.rule'
  24. _description = "Exception Rules"
  25. _order = 'active desc, sequence asc'
  26. name = fields.Char('Exception Name', required=True, translate=True)
  27. description = fields.Text('Description', translate=True)
  28. sequence = fields.Integer(
  29. string='Sequence',
  30. help="Gives the sequence order when applying the test")
  31. rule_group = fields.Selection(
  32. selection=[],
  33. help="Rule group is used to group the rules that must validated "
  34. "at same time for a target object. Ex: "
  35. "validate sale.order.line rules with sale order rules.",
  36. required=True)
  37. model = fields.Selection(
  38. selection=[],
  39. string='Apply on', required=True)
  40. active = fields.Boolean('Active')
  41. next_state = fields.Char(
  42. 'Next state',
  43. help="If we detect exception we set the state of object (ex purchase) "
  44. "to the next_state (ex 'to approve'). If there are more than one "
  45. "exception detected and all have a value for next_state, we use"
  46. "the exception having the smallest sequence value")
  47. code = fields.Text(
  48. 'Python Code',
  49. help="Python code executed to check if the exception apply or "
  50. "not. Use failed = True to block the exception",
  51. default="""
  52. # Python code. Use failed = True to block the base.exception.
  53. # You can use the following variables :
  54. # - self: ORM model of the record which is checked
  55. # - "rule_group" or "rule_group_"line:
  56. # browse_record of the base.exception or
  57. # base.exception line (ex rule_group = sale for sale order)
  58. # - object: same as order or line, browse_record of the base.exception or
  59. # base.exception line
  60. # - obj: same as object
  61. # - env: Odoo Environment (i.e. self.env)
  62. # - time: Python time module
  63. # - cr: database cursor
  64. # - uid: current user id
  65. # - context: current context
  66. """)
  67. @api.constrains('next_state')
  68. def _check_next_state_value(self):
  69. """ Ensure that the next_state value is in the state values of
  70. destination model """
  71. for rule in self:
  72. if rule.next_state:
  73. select_vals = self.env[
  74. rule.model].fields_get()[
  75. 'state']['selection']
  76. select_vals_code = [s[0] for s in select_vals]
  77. if rule.next_state not in select_vals_code:
  78. raise ValidationError(
  79. _('The value "%s" you choose for the "next state" '
  80. 'field state of "%s" is wrong.'
  81. ' Value must be in this list %s')
  82. % (rule.next_state,
  83. rule.model,
  84. select_vals)
  85. )
  86. class BaseException(models.AbstractModel):
  87. _name = 'base.exception'
  88. _order = 'main_exception_id asc'
  89. main_exception_id = fields.Many2one(
  90. 'exception.rule',
  91. compute='_compute_main_error',
  92. string='Main Exception',
  93. store=True)
  94. rule_group = fields.Selection(
  95. [],
  96. readonly=True,
  97. )
  98. exception_ids = fields.Many2many(
  99. 'exception.rule',
  100. string='Exceptions')
  101. ignore_exception = fields.Boolean('Ignore Exceptions', copy=False)
  102. @api.depends('exception_ids', 'ignore_exception')
  103. def _compute_main_error(self):
  104. for obj in self:
  105. if not obj.ignore_exception and obj.exception_ids:
  106. obj.main_exception_id = obj.exception_ids[0]
  107. else:
  108. obj.main_exception_id = False
  109. @api.multi
  110. def _popup_exceptions(self):
  111. action = self._get_popup_action()
  112. action_data = action.read()[0]
  113. action_data.update({
  114. 'context': {
  115. 'active_id': self.ids[0],
  116. 'active_ids': self.ids,
  117. 'active_model': self._name,
  118. }
  119. })
  120. return action_data
  121. @api.model
  122. def _get_popup_action(self):
  123. action = self.env.ref('base_exception.action_exception_rule_confirm')
  124. return action
  125. @api.multi
  126. def _check_exception(self):
  127. """
  128. This method must be used in a constraint that must be created in the
  129. object that inherits for base.exception.
  130. for sale :
  131. @api.constrains('ignore_exception',)
  132. def sale_check_exception(self):
  133. ...
  134. ...
  135. self._check_exception
  136. """
  137. exception_ids = self.detect_exceptions()
  138. if exception_ids:
  139. exceptions = self.env['exception.rule'].browse(exception_ids)
  140. raise ValidationError('\n'.join(exceptions.mapped('name')))
  141. @api.multi
  142. def test_exceptions(self):
  143. """
  144. Condition method for the workflow from draft to confirm
  145. """
  146. if self.detect_exceptions():
  147. return False
  148. return True
  149. @api.multi
  150. def detect_exceptions(self):
  151. """returns the list of exception_ids for all the considered base.exceptions
  152. """
  153. if not self:
  154. return []
  155. exception_obj = self.env['exception.rule']
  156. all_exceptions = exception_obj.sudo().search(
  157. [('rule_group', '=', self[0].rule_group)])
  158. model_exceptions = all_exceptions.filtered(
  159. lambda ex: ex.model == self._name)
  160. sub_exceptions = all_exceptions.filtered(
  161. lambda ex: ex.model != self._name)
  162. all_exception_ids = []
  163. for obj in self:
  164. if obj.ignore_exception:
  165. continue
  166. exception_ids = obj._detect_exceptions(
  167. model_exceptions, sub_exceptions)
  168. obj.exception_ids = [(6, 0, exception_ids)]
  169. all_exception_ids += exception_ids
  170. return all_exception_ids
  171. @api.model
  172. def _exception_rule_eval_context(self, obj_name, rec):
  173. return {obj_name: rec,
  174. 'self': self.env[rec._name],
  175. 'object': rec,
  176. 'obj': rec,
  177. 'env': self.env,
  178. 'cr': self.env.cr,
  179. 'uid': self.env.uid,
  180. 'user': self.env.user,
  181. 'time': time,
  182. # copy context to prevent side-effects of eval
  183. 'context': self.env.context.copy()}
  184. @api.model
  185. def _rule_eval(self, rule, obj_name, rec):
  186. expr = rule.code
  187. space = self._exception_rule_eval_context(obj_name, rec)
  188. try:
  189. safe_eval(expr,
  190. space,
  191. mode='exec',
  192. nocopy=True) # nocopy allows to return 'result'
  193. except Exception as e:
  194. raise UserError(
  195. _('Error when evaluating the exception.rule '
  196. 'rule:\n %s \n(%s)') % (rule.name, e))
  197. return space.get('failed', False)
  198. @api.multi
  199. def _detect_exceptions(self, model_exceptions,
  200. sub_exceptions):
  201. self.ensure_one()
  202. exception_ids = []
  203. next_state_rule = False
  204. for rule in model_exceptions:
  205. if self._rule_eval(rule, self.rule_group, self):
  206. exception_ids.append(rule.id)
  207. if rule.next_state:
  208. if not next_state_rule or\
  209. rule.sequence < next_state_rule.sequence:
  210. next_state_rule = rule
  211. if sub_exceptions:
  212. for obj_line in self._get_lines():
  213. for rule in sub_exceptions:
  214. if rule.id in exception_ids:
  215. # we do not matter if the exception as already been
  216. # found for an line of this object
  217. # (ex sale order line if obj is sale order)
  218. continue
  219. group_line = self.rule_group + '_line'
  220. if self._rule_eval(rule, group_line, obj_line):
  221. exception_ids.append(rule.id)
  222. # set object to next state
  223. if next_state_rule:
  224. self.state = next_state_rule.next_state
  225. return exception_ids
  226. @implemented_by_base_exception
  227. def _get_lines(self):
  228. pass
  229. def _default_get_lines(self):
  230. return []