From ad7e61b7bacf9a84703361fd7caa174e828b3c8f Mon Sep 17 00:00:00 2001 From: "laurent.corron" Date: Tue, 26 Nov 2019 11:47:59 +0100 Subject: [PATCH] [MIG] partner_identification: Migration to 13.0 --- partner_identification/__manifest__.py | 4 +- partner_identification/models/res_partner.py | 27 +++----- .../models/res_partner_id_category.py | 18 ++---- .../models/res_partner_id_number.py | 10 +-- partner_identification/tests/fake_models.py | 4 +- .../tests/test_partner_identification.py | 64 ++++--------------- .../tests/test_res_partner.py | 20 +++--- .../views/res_partner_id_category_view.xml | 1 - .../views/res_partner_id_number_view.xml | 1 - 9 files changed, 41 insertions(+), 108 deletions(-) diff --git a/partner_identification/__manifest__.py b/partner_identification/__manifest__.py index e69b444b3..051538a7d 100644 --- a/partner_identification/__manifest__.py +++ b/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", diff --git a/partner_identification/models/res_partner.py b/partner_identification/models/res_partner.py index c1fb4f8f1..6c32cd46f 100644 --- a/partner_identification/models/res_partner.py +++ b/partner_identification/models/res_partner.py @@ -6,7 +6,7 @@ # Antonio Espinosa # 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)] diff --git a/partner_identification/models/res_partner_id_category.py b/partner_identification/models/res_partner_id_category.py index 2a224c72e..6a04a8b86 100644 --- a/partner_identification/models/res_partner_id_category.py +++ b/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) ) diff --git a/partner_identification/models/res_partner_id_number.py b/partner_identification/models/res_partner_id_number.py index 82d534ddd..b3d74e3b8 100644 --- a/partner_identification/models/res_partner_id_number.py +++ b/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", diff --git a/partner_identification/tests/fake_models.py b/partner_identification/tests/fake_models.py index b5e30e326..261daf845 100644 --- a/partner_identification/tests/fake_models.py +++ b/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), ) diff --git a/partner_identification/tests/test_partner_identification.py b/partner_identification/tests/test_partner_identification.py index c14097cd2..3a9f638bb 100644 --- a/partner_identification/tests/test_partner_identification.py +++ b/partner_identification/tests/test_partner_identification.py @@ -1,15 +1,16 @@ # Copyright 2016 ACSONE SA/NV () # 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}) ] } ) diff --git a/partner_identification/tests/test_res_partner.py b/partner_identification/tests/test_res_partner.py index e502bb062..68a1fd658 100644 --- a/partner_identification/tests/test_res_partner.py +++ b/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) diff --git a/partner_identification/views/res_partner_id_category_view.xml b/partner_identification/views/res_partner_id_category_view.xml index 885b0ae23..f7061731a 100644 --- a/partner_identification/views/res_partner_id_category_view.xml +++ b/partner_identification/views/res_partner_id_category_view.xml @@ -31,7 +31,6 @@ Partner ID Categories res.partner.id_category - form tree,form diff --git a/partner_identification/views/res_partner_id_number_view.xml b/partner_identification/views/res_partner_id_number_view.xml index 0f07f79f8..80624376d 100644 --- a/partner_identification/views/res_partner_id_number_view.xml +++ b/partner_identification/views/res_partner_id_number_view.xml @@ -43,7 +43,6 @@ Partner ID Numbers res.partner.id_number - form tree,form