Browse Source

[MIG] partner_identification: Migration to 13.0

14.0
laurent.corron 5 years ago
committed by Tran Thanh Phuc
parent
commit
ad7e61b7ba
  1. 4
      partner_identification/__manifest__.py
  2. 27
      partner_identification/models/res_partner.py
  3. 18
      partner_identification/models/res_partner_id_category.py
  4. 10
      partner_identification/models/res_partner_id_number.py
  5. 4
      partner_identification/tests/fake_models.py
  6. 64
      partner_identification/tests/test_partner_identification.py
  7. 20
      partner_identification/tests/test_res_partner.py
  8. 1
      partner_identification/views/res_partner_id_category_view.xml
  9. 1
      partner_identification/views/res_partner_id_number_view.xml

4
partner_identification/__manifest__.py

@ -9,9 +9,9 @@
{
"name": "Partner Identification Numbers",
"category": "Customer Relationship Management",
"version": "12.0.1.0.0",
"version": "13.0.1.0.0",
"license": "AGPL-3",
"depends": ["contacts",],
"depends": ["contacts"],
"data": [
"security/ir.model.access.csv",
"views/res_partner_id_category_view.xml",

27
partner_identification/models/res_partner.py

@ -6,7 +6,7 @@
# Antonio Espinosa <antonioea@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models, fields, _
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
@ -19,7 +19,6 @@ class ResPartner(models.Model):
string="Identification Numbers",
)
@api.multi
@api.depends("id_numbers")
def _compute_identification(self, field_name, category_code):
""" Compute a field that indicates a certain ID type.
@ -59,7 +58,6 @@ class ResPartner(models.Model):
value = id_numbers[0].name
record[field_name] = value
@api.multi
def _inverse_identification(self, field_name, category_code):
""" Inverse for an identification field.
@ -104,18 +102,14 @@ class ResPartner(models.Model):
# No value to set
continue
category = self.env["res.partner.id_category"].search(
[("code", "=", category_code),]
[("code", "=", category_code)]
)
if not category:
category = self.env["res.partner.id_category"].create(
{"code": category_code, "name": category_code,}
{"code": category_code, "name": category_code}
)
self.env["res.partner.id_number"].create(
{
"partner_id": record.id,
"category_id": category.id,
"name": name,
}
{"partner_id": record.id, "category_id": category.id, "name": name}
)
# There was an identification record singleton found.
elif record_len == 1:
@ -130,9 +124,9 @@ class ResPartner(models.Model):
_(
"This %s has multiple IDs of this type (%s), so a write "
"via the %s field is not possible. In order to fix this, "
"please use the IDs tab.",
"please use the IDs tab."
)
% (record._name, category_code, field_name,)
% (record._name, category_code, field_name)
)
@api.model
@ -164,11 +158,6 @@ class ResPartner(models.Model):
list: Domain to search with.
"""
id_numbers = self.env["res.partner.id_number"].search(
[
("name", operator, value),
("category_id.code", "=", category_code),
]
[("name", operator, value), ("category_id.code", "=", category_code)]
)
return [
("id_numbers.id", "in", id_numbers.ids),
]
return [("id_numbers.id", "in", id_numbers.ids)]

18
partner_identification/models/res_partner_id_category.py

@ -8,8 +8,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models, fields, _
from odoo.exceptions import ValidationError, UserError
from odoo import _, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools.safe_eval import safe_eval
@ -48,15 +48,10 @@ class ResPartnerIdCategory(models.Model):
"# - id_number: browse_record of ID number to validate"
)
@api.multi
def _validation_eval_context(self, id_number):
self.ensure_one()
return {
"self": self,
"id_number": id_number,
}
return {"self": self, "id_number": id_number}
@api.multi
def validate_id_number(self, id_number):
"""Validate the given ID number
The method raises an odoo.exceptions.ValidationError if the eval of
@ -67,9 +62,7 @@ class ResPartnerIdCategory(models.Model):
return
eval_context = self._validation_eval_context(id_number)
try:
safe_eval(
self.validation_code, eval_context, mode="exec", nocopy=True
)
safe_eval(self.validation_code, eval_context, mode="exec", nocopy=True)
except Exception as e:
raise UserError(
_(
@ -80,6 +73,5 @@ class ResPartnerIdCategory(models.Model):
)
if eval_context.get("failed", False):
raise ValidationError(
_("%s is not a valid %s identifier")
% (id_number.name, self.name)
_("%s is not a valid %s identifier") % (id_number.name, self.name)
)

10
partner_identification/models/res_partner_id_number.py

@ -7,7 +7,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models, fields
from odoo import api, fields, models
class ResPartnerIdNumber(models.Model):
@ -22,8 +22,7 @@ class ResPartnerIdNumber(models.Model):
name = fields.Char(
string="ID Number",
required=True,
help="The ID itself. For example, Driver License number of this "
"person",
help="The ID itself. For example, Driver License number of this person",
)
category_id = fields.Many2one(
string="Category",
@ -32,10 +31,7 @@ class ResPartnerIdNumber(models.Model):
help="ID type defined in configuration. For example, Driver License",
)
partner_id = fields.Many2one(
string="Partner",
required=True,
comodel_name="res.partner",
ondelete="cascade",
string="Partner", required=True, comodel_name="res.partner", ondelete="cascade"
)
partner_issued_id = fields.Many2one(
string="Issued by",

4
partner_identification/tests/fake_models.py

@ -34,7 +34,7 @@ class ResPartner(models.Model):
_teardown_no_delete = True
social_security = fields.Char(
compute=lambda s: s._compute_identification("social_security", "SSN",),
inverse=lambda s: s._inverse_identification("social_security", "SSN",),
compute=lambda s: s._compute_identification("social_security", "SSN"),
inverse=lambda s: s._inverse_identification("social_security", "SSN"),
search=lambda s, *a: s._search_identification("SSN", *a),
)

64
partner_identification/tests/test_partner_identification.py

@ -1,15 +1,16 @@
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from psycopg2._psycopg import IntegrityError
from odoo.exceptions import UserError, ValidationError
from odoo.tests import common
from odoo.exceptions import ValidationError
from odoo.tools import mute_logger
class TestPartnerIdentificationBase(common.TransactionCase):
def test_create_id_category(self):
partner_id_category = self.env["res.partner.id_category"].create(
{"code": "id_code", "name": "id_name",}
{"code": "id_code", "name": "id_name"}
)
self.assertEqual(partner_id_category.name, "id_name")
self.assertEqual(partner_id_category.code, "id_code")
@ -20,25 +21,18 @@ class TestPartnerIdentificationBase(common.TransactionCase):
self.assertEqual(len(partner_1.id_numbers), 0)
# create without required category
with self.assertRaises(IntegrityError):
partner_1.write({"id_numbers": [(0, 0, {"name": "1234",})]})
partner_1.write({"id_numbers": [(0, 0, {"name": "1234"})]})
def test_update_partner_with_category(self):
partner_1 = self.env.ref("base.res_partner_1")
partner_id_category = self.env["res.partner.id_category"].create(
{"code": "new_code", "name": "new_name",}
{"code": "new_code", "name": "new_name"}
)
# successful creation
partner_1.write(
{
"id_numbers": [
(
0,
0,
{
"name": "1234",
"category_id": partner_id_category.id,
},
)
(0, 0, {"name": "1234", "category_id": partner_id_category.id})
]
}
)
@ -66,28 +60,14 @@ if id_number.name != '1234':
partner_1.write(
{
"id_numbers": [
(
0,
0,
{
"name": "01234",
"category_id": partner_id_category.id,
},
)
(0, 0, {"name": "01234", "category_id": partner_id_category.id})
]
}
)
partner_1.write(
{
"id_numbers": [
(
0,
0,
{
"name": "1234",
"category_id": partner_id_category.id,
},
)
(0, 0, {"name": "1234", "category_id": partner_id_category.id})
]
}
)
@ -107,9 +87,7 @@ if id_number.name != '1235':
# check that the constrains is also checked when we change the
# associated category
with self.assertRaises(ValidationError), self.cr.savepoint():
partner_1.id_numbers.write(
{"category_id": partner_id_category2.id}
)
partner_1.id_numbers.write({"category_id": partner_id_category2.id})
def test_bad_validation_code(self):
partner_id_category = self.env["res.partner.id_category"].create(
@ -123,18 +101,11 @@ if id_number.name != '1234' # missing :
}
)
partner_1 = self.env.ref("base.res_partner_1")
with self.assertRaises(ValidationError):
with self.assertRaises(UserError):
partner_1.write(
{
"id_numbers": [
(
0,
0,
{
"name": "1234",
"category_id": partner_id_category.id,
},
)
(0, 0, {"name": "1234", "category_id": partner_id_category.id})
]
}
)
@ -151,20 +122,11 @@ if id_number.name != '1234' # missing :
""",
}
)
partner_1 = self.env.ref("base.res_partner_1").with_context(
id_no_validate=True,
)
partner_1 = self.env.ref("base.res_partner_1").with_context(id_no_validate=True)
partner_1.write(
{
"id_numbers": [
(
0,
0,
{
"name": "1234",
"category_id": partner_id_category.id,
},
)
(0, 0, {"name": "1234", "category_id": partner_id_category.id})
]
}
)

20
partner_identification/tests/test_res_partner.py

@ -1,8 +1,9 @@
# Copyright 2017 LasLabs Inc.
# 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
from .fake_models import ResPartner, setup_test_model, teardown_test_model
@ -12,7 +13,7 @@ class TestResPartner(common.SavepointCase):
super().setUpClass()
setup_test_model(cls.env, ResPartner)
bad_cat = cls.env["res.partner.id_category"].create(
{"code": "another_code", "name": "another_name",}
{"code": "another_code", "name": "another_name"}
)
cls.env["res.partner.id_number"].create(
{
@ -22,7 +23,7 @@ class TestResPartner(common.SavepointCase):
}
)
cls.partner_id_category = cls.env["res.partner.id_category"].create(
{"code": "id_code", "name": "id_name",}
{"code": "id_code", "name": "id_name"}
)
cls.partner = cls.env.ref("base.main_partner")
cls.partner_id = cls.env["res.partner.id_number"].create(
@ -52,21 +53,18 @@ class TestResPartner(common.SavepointCase):
""" It should create a new category of the type if non-existent. """
self.partner._inverse_identification("name", "new_code_type")
category = self.env["res.partner.id_category"].search(
[("code", "=", "new_code_type"),]
[("code", "=", "new_code_type")]
)
self.assertTrue(category)
def test_inverse_identification_creates_new_id(self):
""" It should create a new ID of the type if non-existent. """
category = self.env["res.partner.id_category"].create(
{"code": "new_code_type", "name": "new_code_type",}
{"code": "new_code_type", "name": "new_code_type"}
)
self.partner._inverse_identification("name", "new_code_type")
identification = self.env["res.partner.id_number"].search(
[
("category_id", "=", category.id),
("partner_id", "=", self.partner.id),
]
[("category_id", "=", category.id), ("partner_id", "=", self.partner.id)]
)
self.assertEqual(identification.name, self.partner.name)
@ -85,7 +83,5 @@ class TestResPartner(common.SavepointCase):
def test_search_identification(self):
""" It should return the right record when searched by ID. """
self.partner.social_security = "Test"
partner = self.env["res.partner"].search(
[("social_security", "=", "Test"),]
)
partner = self.env["res.partner"].search([("social_security", "=", "Test")])
self.assertEqual(partner, self.partner)

1
partner_identification/views/res_partner_id_category_view.xml

@ -31,7 +31,6 @@
<record id="action_partner_id_category" model="ir.actions.act_window">
<field name="name">Partner ID Categories</field>
<field name="res_model">res.partner.id_category</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>

1
partner_identification/views/res_partner_id_number_view.xml

@ -43,7 +43,6 @@
<record id="action_partner_id_numbers_form" model="ir.actions.act_window">
<field name="name">Partner ID Numbers</field>
<field name="res_model">res.partner.id_number</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>

Loading…
Cancel
Save