[IMP] add method exception
parent
bebea98896
commit
0992d09738
|
@ -13,6 +13,7 @@
|
||||||
"author": "Akretion, Sodexis, Camptocamp, Odoo Community Association (OCA)",
|
"author": "Akretion, Sodexis, Camptocamp, Odoo Community Association (OCA)",
|
||||||
"website": "https://github.com/OCA/server-tools",
|
"website": "https://github.com/OCA/server-tools",
|
||||||
"depends": ["base_setup"],
|
"depends": ["base_setup"],
|
||||||
|
"maintainers": ["hparfr", "sebastienbeau"],
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"data": [
|
"data": [
|
||||||
"security/base_exception_security.xml",
|
"security/base_exception_security.xml",
|
||||||
|
|
|
@ -25,17 +25,22 @@ class ExceptionRule(models.Model):
|
||||||
model = fields.Selection(selection=[], string="Apply on", required=True)
|
model = fields.Selection(selection=[], string="Apply on", required=True)
|
||||||
|
|
||||||
exception_type = fields.Selection(
|
exception_type = fields.Selection(
|
||||||
selection=[("by_domain", "By domain"), ("by_py_code", "By python code")],
|
selection=[
|
||||||
|
("by_domain", "By domain"),
|
||||||
|
("by_py_code", "By python code"),
|
||||||
|
("by_method", "By method"),
|
||||||
|
],
|
||||||
string="Exception Type",
|
string="Exception Type",
|
||||||
required=True,
|
required=True,
|
||||||
default="by_py_code",
|
default="by_py_code",
|
||||||
help="By python code: allow to define any arbitrary check\n"
|
help="By python code: allow to define any arbitrary check\n"
|
||||||
"By domain: limited to a selection by an odoo domain:\n"
|
"By domain: limited to a selection by an odoo domain:\n"
|
||||||
" performance can be better when exceptions "
|
" performance can be better when exceptions"
|
||||||
" are evaluated with several records",
|
" are evaluated with several records\n"
|
||||||
|
"By method: allow to select an existing check method",
|
||||||
)
|
)
|
||||||
domain = fields.Char("Domain")
|
domain = fields.Char("Domain")
|
||||||
|
method = fields.Selection(selection=[], string="Method", readonly=True)
|
||||||
active = fields.Boolean("Active", default=True)
|
active = fields.Boolean("Active", default=True)
|
||||||
code = fields.Text(
|
code = fields.Text(
|
||||||
"Python Code",
|
"Python Code",
|
||||||
|
@ -47,16 +52,19 @@ class ExceptionRule(models.Model):
|
||||||
help="When checked the exception can not be ignored",
|
help="When checked the exception can not be ignored",
|
||||||
)
|
)
|
||||||
|
|
||||||
@api.constrains("exception_type", "domain", "code")
|
@api.constrains("exception_type", "domain", "code", "model")
|
||||||
def check_exception_type_consistency(self):
|
def check_exception_type_consistency(self):
|
||||||
for rule in self:
|
for rule in self:
|
||||||
if (rule.exception_type == "by_py_code" and not rule.code) or (
|
if (
|
||||||
rule.exception_type == "by_domain" and not rule.domain
|
(rule.exception_type == "by_py_code" and not rule.code)
|
||||||
|
or (rule.exception_type == "by_domain" and not rule.domain)
|
||||||
|
or (rule.exception_type == "by_method" and not rule.method)
|
||||||
):
|
):
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_(
|
_(
|
||||||
"There is a problem of configuration, python code or "
|
"There is a problem of configuration, python code, "
|
||||||
"domain is missing to match the exception type."
|
"domain or method is missing to match the exception "
|
||||||
|
"type."
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -160,6 +168,8 @@ class BaseExceptionMethod(models.AbstractModel):
|
||||||
return self._detect_exceptions_by_py_code(rule)
|
return self._detect_exceptions_by_py_code(rule)
|
||||||
elif rule.exception_type == "by_domain":
|
elif rule.exception_type == "by_domain":
|
||||||
return self._detect_exceptions_by_domain(rule)
|
return self._detect_exceptions_by_domain(rule)
|
||||||
|
elif rule.exception_type == "by_method":
|
||||||
|
return self._detect_exceptions_by_method(rule)
|
||||||
|
|
||||||
def _get_base_domain(self):
|
def _get_base_domain(self):
|
||||||
return [("ignore_exception", "=", False), ("id", "in", self.ids)]
|
return [("ignore_exception", "=", False), ("id", "in", self.ids)]
|
||||||
|
@ -185,6 +195,14 @@ class BaseExceptionMethod(models.AbstractModel):
|
||||||
domain = expression.AND([base_domain, rule_domain])
|
domain = expression.AND([base_domain, rule_domain])
|
||||||
return self.search(domain)
|
return self.search(domain)
|
||||||
|
|
||||||
|
def _detect_exceptions_by_method(self, rule):
|
||||||
|
"""
|
||||||
|
Find exceptions found on self.
|
||||||
|
"""
|
||||||
|
base_domain = self._get_base_domain()
|
||||||
|
records = self.search(base_domain)
|
||||||
|
return getattr(records, rule.method)()
|
||||||
|
|
||||||
|
|
||||||
class BaseExceptionModel(models.AbstractModel):
|
class BaseExceptionModel(models.AbstractModel):
|
||||||
_inherit = "base.exception.method"
|
_inherit = "base.exception.method"
|
||||||
|
|
|
@ -4,6 +4,15 @@
|
||||||
from odoo import api, fields, models
|
from odoo import api, fields, models
|
||||||
|
|
||||||
|
|
||||||
|
class ExceptionRule(models.Model):
|
||||||
|
_inherit = "exception.rule"
|
||||||
|
_name = "exception.rule"
|
||||||
|
|
||||||
|
method = fields.Selection(
|
||||||
|
selection_add=[("exception_method_no_zip", "Purchase exception no zip")]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class PurchaseTest(models.Model):
|
class PurchaseTest(models.Model):
|
||||||
_inherit = "base.exception"
|
_inherit = "base.exception"
|
||||||
_name = "base.exception.test.purchase"
|
_name = "base.exception.test.purchase"
|
||||||
|
@ -58,6 +67,13 @@ class PurchaseTest(models.Model):
|
||||||
def _reverse_field(self):
|
def _reverse_field(self):
|
||||||
return "test_purchase_ids"
|
return "test_purchase_ids"
|
||||||
|
|
||||||
|
def exception_method_no_zip(self):
|
||||||
|
records_fail = self.env["base.exception.test.purchase"]
|
||||||
|
for rec in self:
|
||||||
|
if not rec.partner_id.zip:
|
||||||
|
records_fail += rec
|
||||||
|
return records_fail
|
||||||
|
|
||||||
|
|
||||||
class LineTest(models.Model):
|
class LineTest(models.Model):
|
||||||
_name = "base.exception.test.purchase.line"
|
_name = "base.exception.test.purchase.line"
|
||||||
|
|
|
@ -19,7 +19,7 @@ class TestBaseException(common.SavepointCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
super(TestBaseException, cls).setUpClass()
|
super(TestBaseException, cls).setUpClass()
|
||||||
setup_test_model(cls.env, [PurchaseTest, LineTest])
|
setup_test_model(cls.env, [PurchaseTest, LineTest, ExceptionRule])
|
||||||
|
|
||||||
cls.base_exception = cls.env["base.exception"]
|
cls.base_exception = cls.env["base.exception"]
|
||||||
cls.exception_rule = cls.env["exception.rule"]
|
cls.exception_rule = cls.env["exception.rule"]
|
||||||
|
|
Loading…
Reference in New Issue