From 4f93a0666b4ad9edc4138c3f4dc5b2ecaacb226f Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Tue, 1 Feb 2022 13:57:32 +0000 Subject: [PATCH] [REF] partner_tier_validation: do not archive, depend on partner_stage --- partner_tier_validation/__init__.py | 2 - partner_tier_validation/__manifest__.py | 4 +- .../data/tier_definition.xml | 6 +-- partner_tier_validation/hooks.py | 18 ------- partner_tier_validation/models/res_partner.py | 41 +++++++++------- .../tests/test_tier_validation.py | 47 +++++++++++-------- .../views/res_partner_view.xml | 16 ++----- 7 files changed, 60 insertions(+), 74 deletions(-) delete mode 100644 partner_tier_validation/hooks.py diff --git a/partner_tier_validation/__init__.py b/partner_tier_validation/__init__.py index c0d9f3d12..3275ac2ad 100644 --- a/partner_tier_validation/__init__.py +++ b/partner_tier_validation/__init__.py @@ -1,4 +1,2 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - from . import models -from .hooks import post_init_hook diff --git a/partner_tier_validation/__manifest__.py b/partner_tier_validation/__manifest__.py index 7b2ca7f1f..5836fb652 100644 --- a/partner_tier_validation/__manifest__.py +++ b/partner_tier_validation/__manifest__.py @@ -9,10 +9,10 @@ "author": "Open Source Integrators, Odoo Community Association (OCA)", "license": "AGPL-3", "installable": True, - "depends": ["contacts", "base_tier_validation"], + "depends": ["contacts", "base_tier_validation", "partner_stage"], "data": [ "data/tier_definition.xml", "views/res_partner_view.xml", ], - "post_init_hook": "post_init_hook", + "maintainers": ["dreispt"], } diff --git a/partner_tier_validation/data/tier_definition.xml b/partner_tier_validation/data/tier_definition.xml index 528842bca..c46c0e54a 100644 --- a/partner_tier_validation/data/tier_definition.xml +++ b/partner_tier_validation/data/tier_definition.xml @@ -1,13 +1,11 @@ - Partner Validation (Company) + Validate New Company group domain - ["&",["is_company","=",True],"|",["active","=",True],["active","=",False]] + [["is_company","=",True]] diff --git a/partner_tier_validation/hooks.py b/partner_tier_validation/hooks.py deleted file mode 100644 index 0bfebf185..000000000 --- a/partner_tier_validation/hooks.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2021 Tecnativa - Víctor Martínez -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). - -from odoo import SUPERUSER_ID -from odoo.api import Environment -from odoo.tools import config - - -def post_init_hook(cr, pool): - """ - We need to activate the rule only if we are not in a test environment. - """ - if not config["test_enable"]: - env = Environment(cr, SUPERUSER_ID, {}) - tier_partner = env.ref( - "partner_tier_validation.partner_tier_definition_company_only" - ) - tier_partner.write({"active": True}) diff --git a/partner_tier_validation/models/res_partner.py b/partner_tier_validation/models/res_partner.py index 34e78d17e..909085098 100644 --- a/partner_tier_validation/models/res_partner.py +++ b/partner_tier_validation/models/res_partner.py @@ -1,47 +1,54 @@ # Copyright 2019 Open Source Integrators # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import api, models class ResPartner(models.Model): _name = "res.partner" _inherit = ["res.partner", "tier.validation"] - _tier_validation_manual_config = False - state = fields.Selection( - [("draft", "Draft"), ("confirmed", "Active"), ("cancel", "Archived")], - string="Status", - default="draft", - ) + _tier_validation_buttons_xpath = "/form/header/field[@name='state']" + _state_from = ["draft"] + _state_to = ["confirmed"] + _cancel_state = ["inactive"] + _tier_validation_manual_config = False @api.model def _tier_revalidation_fields(self, values): """ Changing some Partner fields forces Tier Validation to be reevaluated. Out of the box these are is_company and parent_id. - Other can be added extenting this method. + Other can be added extending this method. """ return ["is_company", "parent_id"] + @api.model + def _get_confirmed_stage(self): + return self.env["res.partner.stage"].search( + [("state", "in", self._state_to)], limit=1 + ) + @api.model def create(self, vals): new = super().create(vals) - if new.need_validation and new.state != "confirmed": - new.active = False - else: - new.active = True - new.state = "confirmed" + # Contact not requiring validation + # are created in confirmed state + if not new.need_validation: + confirmed_stage = self._get_confirmed_stage() + new.stage_id = confirmed_stage return new def write(self, vals): + # Signal state transition by adding to vals + if "stage_id" in vals: + stage_id = vals.get("stage_id") + stage = self.env["res.partner.stage"].browse(stage_id) + vals["state"] = stage.state # Changing certain fields required new validation process revalidate_fields = self._tier_revalidation_fields(vals) if any(x in revalidate_fields for x in vals.keys()): self.mapped("review_ids").unlink() vals["state"] = "draft" - - # Automatically update active flag depending on state - if "state" in vals: - vals["active"] = vals["state"] == "confirmed" + self.request_validation() return super().write(vals) diff --git a/partner_tier_validation/tests/test_tier_validation.py b/partner_tier_validation/tests/test_tier_validation.py index f4fe3d221..56420cfcf 100644 --- a/partner_tier_validation/tests/test_tier_validation.py +++ b/partner_tier_validation/tests/test_tier_validation.py @@ -21,48 +21,55 @@ class TestPartnerTierValidation(common.SavepointCase): "name": "John", "login": "test1", "groups_id": [(6, 0, group_ids)], - "email": "test@examlple.com", + "email": "test@example.com", } ) - # Create tier definitions: - cls.tier_def_obj = cls.env["tier.definition"] - cls.tier_def_obj.create( + # Create tier definition: example where only Company needs validation + cls.TierDefinition = cls.env["tier.definition"] + cls.TierDefinition.create( { "model_id": cls.partner_model.id, "review_type": "individual", "reviewer_id": cls.test_user_1.id, - "definition_domain": "['&',('is_company','=',True),'|', \ - ('active','=',True),('active','=',False)]", + "definition_domain": "[('is_company','=',True)]", } ) def test_tier_validation_model_name(self): self.assertIn( - "res.partner", self.tier_def_obj._get_tier_validation_model_names() + "res.partner", self.TierDefinition._get_tier_validation_model_names() ) def test_validation_res_partner(self): - company = self.env["res.partner"].create( + """ + Case where new Contact requires validation + """ + contact = self.env["res.partner"].create( {"name": "Company for test", "company_type": "company"} ) - # Since company need validation, it should be inactive - self.assertEqual(company.active, False) + # Since contact need validation, it should be inactive + self.assertEqual(contact.state, "draft") # Assert an error shows if trying to make it active with self.assertRaises(ValidationError): - company.write({"state": "confirmed"}) + contact.write({"state": "confirmed"}) # Request and validate partner - company.request_validation() - company.with_user(self.test_user_1).validate_tier() - company.with_user(self.test_user_1).write({"state": "confirmed"}) - self.assertEqual(company.state, "confirmed") + contact.request_validation() + contact.with_user(self.test_user_1).validate_tier() + contact.with_user(self.test_user_1).write({"state": "confirmed"}) + self.assertEqual(contact.state, "confirmed") # Change company type to retrigger validation - company.write({"company_type": "person", "is_company": False}) - self.assertEqual(company.state, "draft") + contact.write({"company_type": "person", "is_company": False}) + self.assertEqual(contact.state, "draft") - # Test partner creation that doesn't need validation - customer = self.env["res.partner"].create({"name": "Partner for test"}) - self.assertEqual(customer.active, True) + def test_no_validation_res_partner(self): + """ + Case where new Contact does not require validation + """ + contact = self.env["res.partner"].create( + {"name": "Person for test", "company_type": "person"} + ) + self.assertEqual(contact.state, "confirmed") diff --git a/partner_tier_validation/views/res_partner_view.xml b/partner_tier_validation/views/res_partner_view.xml index cc74dee53..29cbd0a62 100644 --- a/partner_tier_validation/views/res_partner_view.xml +++ b/partner_tier_validation/views/res_partner_view.xml @@ -8,16 +8,10 @@ - -
- -
-
+ + + + @@ -31,7 +25,7 @@