Browse Source

[REF] partner_tier_validation: do not archive, depend on partner_stage

14.0
Daniel Reis 3 years ago
parent
commit
4f93a0666b
  1. 2
      partner_tier_validation/__init__.py
  2. 4
      partner_tier_validation/__manifest__.py
  3. 6
      partner_tier_validation/data/tier_definition.xml
  4. 18
      partner_tier_validation/hooks.py
  5. 41
      partner_tier_validation/models/res_partner.py
  6. 47
      partner_tier_validation/tests/test_tier_validation.py
  7. 16
      partner_tier_validation/views/res_partner_view.xml

2
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

4
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"],
}

6
partner_tier_validation/data/tier_definition.xml

@ -1,13 +1,11 @@
<odoo noupdate="1">
<record id="partner_tier_definition_company_only" model="tier.definition">
<field name="name">Partner Validation (Company)</field>
<field name="name">Validate New Company</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="review_type">group</field>
<field name="reviewer_group_id" ref="base.group_user" />
<field name="definition_type">domain</field>
<field name="active" eval="False" />
<field
name="definition_domain"
>["&amp;",["is_company","=",True],"|",["active","=",True],["active","=",False]]</field>
<field name="definition_domain">[["is_company","=",True]]</field>
</record>
</odoo>

18
partner_tier_validation/hooks.py

@ -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})

41
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)

47
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")

16
partner_tier_validation/views/res_partner_view.xml

@ -8,16 +8,10 @@
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<xpath expr="/form/sheet" position="before">
<header>
<field
name="state"
widget="statusbar"
options="{'clickable': '1'}"
/>
<button name="dummy" invisible="True" />
</header>
</xpath>
<!-- Tier Validation UI needs the state field to be in the form -->
<field name="stage_id" position="before">
<field name="state" invisible="1" />
</field>
</field>
</record>
@ -31,7 +25,7 @@
<filter
name="needs_review"
string="Needs my Approval"
domain="[('reviewer_ids','in',uid), ('state', '=', 'draft'), '|', ('active','=', True), ('active', '=', False)]"
domain="[('reviewer_ids', 'in', uid), ('state', '=', 'draft')]"
help="Partner(s) to Approve"
/>
</filter>

Loading…
Cancel
Save