From f0f13e6c54bb27743a6e7b51df6b6966ab456365 Mon Sep 17 00:00:00 2001 From: emagdalena Date: Fri, 10 Jul 2020 02:21:05 +0200 Subject: [PATCH] [IMP] partner_second_lastname: black, isort, prettier --- partner_second_lastname/__manifest__.py | 12 +-- .../models/res_config_settings.py | 38 ++++---- partner_second_lastname/models/res_partner.py | 37 ++++---- partner_second_lastname/tests/test_config.py | 16 ++-- partner_second_lastname/tests/test_name.py | 90 ++++++++----------- .../tests/test_onchange.py | 21 ++--- partner_second_lastname/views/res_partner.xml | 68 ++++++++++---- partner_second_lastname/views/res_user.xml | 21 +++-- 8 files changed, 160 insertions(+), 143 deletions(-) diff --git a/partner_second_lastname/__manifest__.py b/partner_second_lastname/__manifest__.py index 3549ae627..a90fedd0d 100644 --- a/partner_second_lastname/__manifest__.py +++ b/partner_second_lastname/__manifest__.py @@ -9,15 +9,9 @@ "version": "12.0.1.0.0", "license": "AGPL-3", "website": "https://github.com/OCA/partner-contact", - "author": "Tecnativa, " - "Odoo Community Association (OCA)", + "author": "Tecnativa, " "Odoo Community Association (OCA)", "category": "Partner Management", - "depends": [ - "partner_firstname", - ], - "data": [ - "views/res_partner.xml", - "views/res_user.xml", - ], + "depends": ["partner_firstname",], + "data": ["views/res_partner.xml", "views/res_user.xml",], "installable": True, } diff --git a/partner_second_lastname/models/res_config_settings.py b/partner_second_lastname/models/res_config_settings.py index 4fbf58000..84be74d7c 100644 --- a/partner_second_lastname/models/res_config_settings.py +++ b/partner_second_lastname/models/res_config_settings.py @@ -1,28 +1,36 @@ # Copyright 2015 Antiun Ingenieria S.L. - Antonio Espinosa # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import models, api +from odoo import api, models class ResConfigSettings(models.TransientModel): - _inherit = 'res.config.settings' + _inherit = "res.config.settings" def _partner_names_order_selection(self): - options = super( - ResConfigSettings, self)._partner_names_order_selection() + options = super(ResConfigSettings, self)._partner_names_order_selection() new_labels = { - 'last_first': 'Lastname SecondLastname Firstname', - 'last_first_comma': 'Lastname SecondLastname, Firstname', - 'first_last': 'Firstname Lastname SecondLastname', + "last_first": "Lastname SecondLastname Firstname", + "last_first_comma": "Lastname SecondLastname, Firstname", + "first_last": "Firstname Lastname SecondLastname", } - return [(k, new_labels[k]) if k in new_labels else (k, v) - for k, v in options] + return [(k, new_labels[k]) if k in new_labels else (k, v) for k, v in options] @api.multi def _partners_for_recalculating(self): - return self.env['res.partner'].search([ - ('is_company', '=', False), - '|', '&', ('firstname', '!=', False), ('lastname', '!=', False), - '|', '&', ('firstname', '!=', False), ('lastname2', '!=', False), - '&', ('lastname', '!=', False), ('lastname2', '!=', False), - ]) + return self.env["res.partner"].search( + [ + ("is_company", "=", False), + "|", + "&", + ("firstname", "!=", False), + ("lastname", "!=", False), + "|", + "&", + ("firstname", "!=", False), + ("lastname2", "!=", False), + "&", + ("lastname", "!=", False), + ("lastname2", "!=", False), + ] + ) diff --git a/partner_second_lastname/models/res_partner.py b/partner_second_lastname/models/res_partner.py index 1f70d9e40..32adb3ee5 100644 --- a/partner_second_lastname/models/res_partner.py +++ b/partner_second_lastname/models/res_partner.py @@ -4,16 +4,16 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import api, fields, models + from odoo.addons.partner_firstname import exceptions class ResPartner(models.Model): """Adds a second last name.""" + _inherit = "res.partner" - lastname2 = fields.Char( - "Second last name", - ) + lastname2 = fields.Char("Second last name",) @api.model def _get_computed_name(self, lastname, firstname, lastname2=None): @@ -24,7 +24,7 @@ class ResPartner(models.Model): """ order = self._get_names_order() names = list() - if order == 'first_last': + if order == "first_last": if firstname: names.append(firstname) if lastname: @@ -36,7 +36,7 @@ class ResPartner(models.Model): names.append(lastname) if lastname2: names.append(lastname2) - if names and firstname and order == 'last_first_comma': + if names and firstname and order == "last_first_comma": names[-1] = names[-1] + "," if firstname: names.append(firstname) @@ -73,29 +73,28 @@ class ResPartner(models.Model): """ # Company name goes to the lastname result = { - 'firstname': False, - 'lastname': name or False, - 'lastname2': False, + "firstname": False, + "lastname": name or False, + "lastname2": False, } if not is_company and name: order = self._get_names_order() - result = super(ResPartner, self)._get_inverse_name( - name, is_company) + result = super(ResPartner, self)._get_inverse_name(name, is_company) parts = [] - if order == 'last_first': - if result['firstname']: - parts = result['firstname'].split(" ", 1) + if order == "last_first": + if result["firstname"]: + parts = result["firstname"].split(" ", 1) while len(parts) < 2: parts.append(False) - result['lastname2'] = parts[0] - result['firstname'] = parts[1] + result["lastname2"] = parts[0] + result["firstname"] = parts[1] else: - if result['lastname']: - parts = result['lastname'].split(" ", 1) + if result["lastname"]: + parts = result["lastname"].split(" ", 1) while len(parts) < 2: parts.append(False) - result['lastname'] = parts[0] - result['lastname2'] = parts[1] + result["lastname"] = parts[0] + result["lastname2"] = parts[1] return result @api.constrains("firstname", "lastname", "lastname2") diff --git a/partner_second_lastname/tests/test_config.py b/partner_second_lastname/tests/test_config.py index 3b8e00ae1..7f12c09e7 100644 --- a/partner_second_lastname/tests/test_config.py +++ b/partner_second_lastname/tests/test_config.py @@ -8,27 +8,25 @@ class TestConfig(common.SavepointCase): @classmethod def setUpClass(cls): super(TestConfig, cls).setUpClass() - cls.wizard = cls.env['res.config.settings'].create({}) - cls.partner = cls.env['res.partner'].create({ - 'firstname': "First", - 'lastname': "Last", - 'lastname2': "Second", - }) + cls.wizard = cls.env["res.config.settings"].create({}) + cls.partner = cls.env["res.partner"].create( + {"firstname": "First", "lastname": "Last", "lastname2": "Second",} + ) def test_last_first(self): - self.wizard.partner_names_order = 'last_first' + self.wizard.partner_names_order = "last_first" self.wizard.set_values() self.wizard.action_recalculate_partners_name() self.assertEqual(self.partner.name, "Last Second First") def test_last_first_comma(self): - self.wizard.partner_names_order = 'last_first_comma' + self.wizard.partner_names_order = "last_first_comma" self.wizard.set_values() self.wizard.action_recalculate_partners_name() self.assertEqual(self.partner.name, "Last Second, First") def test_first_last(self): - self.wizard.partner_names_order = 'first_last' + self.wizard.partner_names_order = "first_last" self.wizard.set_values() self.wizard.action_recalculate_partners_name() self.assertEqual(self.partner.name, "First Last Second") diff --git a/partner_second_lastname/tests/test_name.py b/partner_second_lastname/tests/test_name.py index 06e0de4e3..d74ee8a04 100644 --- a/partner_second_lastname/tests/test_name.py +++ b/partner_second_lastname/tests/test_name.py @@ -3,47 +3,43 @@ # Copyright 2015 Antiun Ingenieria S.L. - Antonio Espinosa from odoo.tests.common import TransactionCase + from odoo.addons.partner_firstname.tests.base import MailInstalled class CompanyCase(TransactionCase): """Test ``res.partner`` when it is a company.""" + def setUp(self): super(CompanyCase, self).setUp() - self.env['ir.config_parameter'].set_param( - 'partner_names_order', 'first_last') + self.env["ir.config_parameter"].set_param("partner_names_order", "first_last") def tearDown(self): try: - new = self.env["res.partner"].create({ - "is_company": True, - "name": self.name, - }) + new = self.env["res.partner"].create( + {"is_company": True, "name": self.name,} + ) # Name should be cleaned of unneeded whitespace clean_name = " ".join(self.name.split(None)) # Check it's saved OK - self.assertEqual( - new.name, - clean_name, - "Saved company name is wrong.") + self.assertEqual(new.name, clean_name, "Saved company name is wrong.") # Check it's saved in the lastname self.assertEqual( new.lastname, clean_name, - "Company name should be saved in the lastname field.") + "Company name should be saved in the lastname field.", + ) # Check that other fields are empty self.assertEqual( - new.firstname, - False, - "Company first name must always be empty.") + new.firstname, False, "Company first name must always be empty." + ) self.assertEqual( - new.lastname2, - False, - "Company last name 2 must always be empty.") + new.lastname2, False, "Company last name 2 must always be empty." + ) finally: super(CompanyCase, self).tearDown() @@ -75,13 +71,15 @@ class CompanyCase(TransactionCase): class PersonCase(TransactionCase): """Test ``res.partner`` when it is a person.""" + model = "res.partner" context = dict() def setUp(self): super(PersonCase, self).setUp() - self.env['ir.config_parameter'].set_param( - 'partner_names_order', 'last_first_comma') + self.env["ir.config_parameter"].set_param( + "partner_names_order", "last_first_comma" + ) self.firstname = "Fírstname" self.lastname = "Làstname1" @@ -90,59 +88,49 @@ class PersonCase(TransactionCase): def tearDown(self): try: - new = (self.env[self.model].with_context(self.context) - .create(self.params)) + new = self.env[self.model].with_context(self.context).create(self.params) # Check that each individual field matches - self.assertEqual( - self.firstname, - new.firstname, - "First name saved badly.") - self.assertEqual( - self.lastname, - new.lastname, - "Last name 1 saved badly.") - self.assertEqual( - self.lastname2, - new.lastname2, - "Last name 2 saved badly.") + self.assertEqual(self.firstname, new.firstname, "First name saved badly.") + self.assertEqual(self.lastname, new.lastname, "Last name 1 saved badly.") + self.assertEqual(self.lastname2, new.lastname2, "Last name 2 saved badly.") # Check that name gets saved fine self.assertEqual( - self.template % ({"last1": self.lastname, - "last2": self.lastname2, - "first": self.firstname}), + self.template + % ( + { + "last1": self.lastname, + "last2": self.lastname2, + "first": self.firstname, + } + ), new.name, - "Name saved badly.") + "Name saved badly.", + ) finally: super(PersonCase, self).tearDown() def test_firstname_first(self): """Create a person setting his first name first.""" - self.env['ir.config_parameter'].set_param( - 'partner_names_order', 'first_last') + self.env["ir.config_parameter"].set_param("partner_names_order", "first_last") self.template = "%(first)s %(last1)s %(last2)s" self.params = { "is_company": False, - "name": "%s %s %s" % (self.firstname, - self.lastname, - self.lastname2), + "name": "{} {} {}".format(self.firstname, self.lastname, self.lastname2), } def test_firstname_last(self): """Create a person setting his first name last.""" self.params = { "is_company": False, - "name": "%s %s, %s" % (self.lastname, - self.lastname2, - self.firstname), + "name": "{} {}, {}".format(self.lastname, self.lastname2, self.firstname), } def test_firstname_only(self): """Create a person setting his first name only.""" - self.env['ir.config_parameter'].set_param( - 'partner_names_order', 'first_last') + self.env["ir.config_parameter"].set_param("partner_names_order", "first_last") self.firstname = self.lastname2 = False self.template = "%(last1)s" self.params = { @@ -152,13 +140,12 @@ class PersonCase(TransactionCase): def test_firstname_lastname_only(self): """Create a person setting his first name and last name 1 only.""" - self.env['ir.config_parameter'].set_param( - 'partner_names_order', 'first_last') + self.env["ir.config_parameter"].set_param("partner_names_order", "first_last") self.lastname2 = False self.template = "%(first)s %(last1)s" self.params = { "is_company": False, - "name": "%s %s" % (self.firstname, self.lastname), + "name": "{} {}".format(self.firstname, self.lastname), } def test_lastname_firstname_only(self): @@ -167,7 +154,7 @@ class PersonCase(TransactionCase): self.template = "%(last1)s, %(first)s" self.params = { "is_company": False, - "name": "%s, %s" % (self.lastname, self.firstname), + "name": "{}, {}".format(self.lastname, self.firstname), } def test_separately(self): @@ -182,6 +169,7 @@ class PersonCase(TransactionCase): class UserCase(PersonCase, MailInstalled): """Test ``res.users``.""" + model = "res.users" context = {"default_login": "user@example.com"} diff --git a/partner_second_lastname/tests/test_onchange.py b/partner_second_lastname/tests/test_onchange.py index 1e290231c..2e5e63118 100644 --- a/partner_second_lastname/tests/test_onchange.py +++ b/partner_second_lastname/tests/test_onchange.py @@ -15,8 +15,9 @@ class OnChangeCase(TransactionCase): def setUp(self): super(OnChangeCase, self).setUp() - self.env['ir.config_parameter'].set_param( - 'partner_names_order', 'last_first_comma') + self.env["ir.config_parameter"].set_param( + "partner_names_order", "last_first_comma" + ) def new_partner(self): """Create an empty partner. Ensure it is (or not) a company.""" @@ -163,9 +164,7 @@ class PartnerContactCase(OnChangeCase): self.set_field("lastname2", lastname2) self.assertEqual(self.partner.firstname, False) - self.assertEqual( - self.partner.name, - "%s %s" % (lastname, lastname2)) + self.assertEqual(self.partner.name, "{} {}".format(lastname, lastname2)) def test_create_from_without_lastname(self): """A user creates a contact without lastname from the form.""" @@ -180,9 +179,7 @@ class PartnerContactCase(OnChangeCase): self.set_field("lastname2", lastname2) self.assertEqual(self.partner.lastname, False) - self.assertEqual( - self.partner.name, - "%s, %s" % (lastname2, firstname)) + self.assertEqual(self.partner.name, "{}, {}".format(lastname2, firstname)) def test_create_from_without_lastname2(self): """A user creates a contact without lastname2 from the form.""" @@ -197,9 +194,7 @@ class PartnerContactCase(OnChangeCase): self.set_field("lastname", lastname) self.assertEqual(self.partner.lastname2, False) - self.assertEqual( - self.partner.name, - "%s, %s" % (lastname, firstname)) + self.assertEqual(self.partner.name, "{}, {}".format(lastname, firstname)) def test_create_from_form_all(self): """A user creates a contact with all names from the form.""" @@ -216,5 +211,5 @@ class PartnerContactCase(OnChangeCase): self.set_field("lastname2", lastname2) self.assertEqual( - self.partner.name, - "%s %s, %s" % (lastname, lastname2, firstname)) + self.partner.name, "{} {}, {}".format(lastname, lastname2, firstname) + ) diff --git a/partner_second_lastname/views/res_partner.xml b/partner_second_lastname/views/res_partner.xml index a47838afd..c1a6b8164 100644 --- a/partner_second_lastname/views/res_partner.xml +++ b/partner_second_lastname/views/res_partner.xml @@ -1,50 +1,80 @@ - + res.partner - + - {'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} + {'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} - {'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} + {'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} - + - res.partner - + - {'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} + {'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} - - {'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} + {'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} - + - - {'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} + + {'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} - - {'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} + + {'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]} - - + + - + - diff --git a/partner_second_lastname/views/res_user.xml b/partner_second_lastname/views/res_user.xml index fb2b1c5d9..1742b9030 100644 --- a/partner_second_lastname/views/res_user.xml +++ b/partner_second_lastname/views/res_user.xml @@ -1,24 +1,29 @@ - + - Add second last name res.users - + - {'required': [('lastname', '=', False), ('lastname2', '=', False)]} + {'required': [('lastname', '=', False), ('lastname2', '=', False)]} - {'required': [('firstname', '=', False), ('lastname2', '=', False)]} + {'required': [('firstname', '=', False), ('lastname2', '=', False)]} - + - + -