Browse Source

[IMP] partner_ref_unique: black, isort, prettier

14.0
João Marques 4 years ago
committed by Pedro M. Baeza
parent
commit
2091ed0fa8
  1. 11
      partner_ref_unique/__manifest__.py
  2. 7
      partner_ref_unique/hooks.py
  3. 11
      partner_ref_unique/models/res_company.py
  4. 23
      partner_ref_unique/models/res_partner.py
  5. 64
      partner_ref_unique/tests/test_res_partner_ref.py
  6. 26
      partner_ref_unique/views/res_company_view.xml

11
partner_ref_unique/__manifest__.py

@ -8,16 +8,11 @@
"version": "12.0.1.0.2",
"category": "Customer Relationship Management",
"website": "https://github.com/OCA/partner-contact/",
"author": "Tecnativa, "
"Odoo Community Association (OCA)",
"author": "Tecnativa, " "Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"pre_init_hook": "pre_init_hook",
"depends": [
"base",
],
"data": [
"views/res_company_view.xml",
],
"depends": ["base",],
"data": ["views/res_company_view.xml",],
}

7
partner_ref_unique/hooks.py

@ -16,12 +16,9 @@ def pre_init_hook(cr):
"COMMENT ON COLUMN public.res_company.partner_ref_unique "
"IS 'Unique partner reference for';"
)
cr.execute(
"UPDATE res_company SET partner_ref_unique = 'none';"
)
cr.execute("UPDATE res_company SET partner_ref_unique = 'none';")
_logger.info(
"Creating res.partner.partner_ref_unique column with value from "
"res.company"
"Creating res.partner.partner_ref_unique column with value from " "res.company"
)
cr.execute("ALTER TABLE res_partner ADD partner_ref_unique varchar NULL;")
cr.execute(

11
partner_ref_unique/models/res_company.py

@ -9,7 +9,10 @@ class ResCompany(models.Model):
partner_ref_unique = fields.Selection(
selection=[
('none', 'None'),
('companies', 'Only companies'),
('all', 'All partners'),
], string="Unique partner reference for", default="none")
("none", "None"),
("companies", "Only companies"),
("all", "All partners"),
],
string="Unique partner reference for",
default="none",
)

23
partner_ref_unique/models/res_partner.py

@ -11,28 +11,29 @@ class ResPartner(models.Model):
# This related is needed in order to trigger the check when changing the
# value on res.company
partner_ref_unique = fields.Selection(
related='company_id.partner_ref_unique', store=True,
related="company_id.partner_ref_unique", store=True,
)
@api.multi
@api.constrains('ref', 'is_company', 'company_id', 'partner_ref_unique')
@api.constrains("ref", "is_company", "company_id", "partner_ref_unique")
def _check_ref(self):
for partner in self:
mode = partner.partner_ref_unique
if (partner.ref and (
mode == 'all' or
(mode == 'companies' and partner.is_company))):
if partner.ref and (
mode == "all" or (mode == "companies" and partner.is_company)
):
domain = [
('id', '!=', partner.id),
('ref', '=', partner.ref),
("id", "!=", partner.id),
("ref", "=", partner.ref),
]
if mode == 'companies':
domain.append(('is_company', '=', True))
if mode == "companies":
domain.append(("is_company", "=", True))
other = self.search(domain)
# active_test is False when called from
# base.partner.merge.automatic.wizard
if other and self.env.context.get("active_test", True):
raise ValidationError(
_("This reference is equal to partner '%s'") %
other[0].display_name)
_("This reference is equal to partner '%s'")
% other[0].display_name
)

64
partner_ref_unique/tests/test_res_partner_ref.py

@ -1,67 +1,57 @@
# Copyright 2017 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests import common
from odoo.exceptions import ValidationError
from odoo.tests import common
class TestResPartnerRefUnique(common.SavepointCase):
@classmethod
def setUpClass(cls):
super(TestResPartnerRefUnique, cls).setUpClass()
cls.company = cls.env.ref('base.main_company')
cls.partner_obj = cls.env['res.partner']
cls.partner1 = cls.partner_obj.create({
'name': 'Partner1',
})
cls.partner2 = cls.partner_obj.create({
'name': 'Partner2',
})
cls.company = cls.env.ref("base.main_company")
cls.partner_obj = cls.env["res.partner"]
cls.partner1 = cls.partner_obj.create({"name": "Partner1",})
cls.partner2 = cls.partner_obj.create({"name": "Partner2",})
def test_check_ref_company(self):
# Test can create/modify partners with same ref
self.company.partner_ref_unique = 'none'
self.partner1.ref = 'same_ref'
self.partner2.ref = 'same_ref'
self.company.partner_ref_unique = "none"
self.partner1.ref = "same_ref"
self.partner2.ref = "same_ref"
self.assertEqual(self.partner1.ref, self.partner2.ref)
# Here there shouldn't be any problem
self.partner_obj.create({
'name': 'other',
'ref': 'same_ref',
})
self.partner_obj.create(
{"name": "other", "ref": "same_ref",}
)
self.partner2.ref = False
with self.assertRaises(ValidationError):
self.company.partner_ref_unique = 'all'
self.company.partner_ref_unique = "all"
def test_check_ref(self):
self.partner1.ref = 'same_ref'
self.partner1.ref = "same_ref"
# Test can't create/modify partner with same ref
self.company.partner_ref_unique = 'all'
self.company.partner_ref_unique = "all"
with self.assertRaises(ValidationError):
self.partner2.ref = 'same_ref'
self.partner2.ref = "same_ref"
with self.assertRaises(ValidationError):
self.partner_obj.create({
'name': 'other',
'ref': 'same_ref',
})
self.partner_obj.create(
{"name": "other", "ref": "same_ref",}
)
# Test can't create/modify companies with same ref
self.company.partner_ref_unique = 'companies'
self.partner2.ref = 'same_ref'
self.company.partner_ref_unique = "companies"
self.partner2.ref = "same_ref"
self.assertEqual(self.partner1.ref, self.partner2.ref)
self.partner2.ref = False
self.partner1.is_company = True
self.partner2.is_company = True
with self.assertRaises(ValidationError):
self.partner2.ref = 'same_ref'
self.partner2.ref = "same_ref"
with self.assertRaises(ValidationError):
self.partner_obj.create({
'is_company': True,
'name': 'other',
'ref': 'same_ref',
})
self.partner_obj.create(
{"is_company": True, "name": "other", "ref": "same_ref",}
)
# Here there shouldn't be any problem
self.partner_obj.create({
'is_company': False,
'name': 'other',
'ref': 'same_ref',
})
self.partner_obj.create(
{"is_company": False, "name": "other", "ref": "same_ref",}
)

26
partner_ref_unique/views/res_company_view.xml

@ -1,20 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2016 Antonio Espinosa
Copyright 2017 Tecnativa - Vicent Cubells
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -->
<odoo>
<record id="view_company_form" model="ir.ui.view">
<field name="name">Add partner_ref_unique field</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form"/>
<field name="arch" type="xml">
<group name="social_media" position="before">
<group name="partner" string="Partners">
<field name="partner_ref_unique"/>
<record id="view_company_form" model="ir.ui.view">
<field name="name">Add partner_ref_unique field</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form" />
<field name="arch" type="xml">
<group name="social_media" position="before">
<group name="partner" string="Partners">
<field name="partner_ref_unique" />
</group>
</group>
</group>
</field>
</record>
</field>
</record>
</odoo>
Loading…
Cancel
Save