Browse Source

[IMP] partner_second_lastname: black, isort, prettier

14.0
emagdalena 4 years ago
committed by Luis Torres
parent
commit
f0f13e6c54
  1. 12
      partner_second_lastname/__manifest__.py
  2. 38
      partner_second_lastname/models/res_config_settings.py
  3. 37
      partner_second_lastname/models/res_partner.py
  4. 16
      partner_second_lastname/tests/test_config.py
  5. 90
      partner_second_lastname/tests/test_name.py
  6. 21
      partner_second_lastname/tests/test_onchange.py
  7. 68
      partner_second_lastname/views/res_partner.xml
  8. 21
      partner_second_lastname/views/res_user.xml

12
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,
}

38
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),
]
)

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

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

90
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"}

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

68
partner_second_lastname/views/res_partner.xml

@ -1,50 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2015 Tecnativa - Jairo Llopis
Copyright 2017 Tecnativa - Pedro M. Baeza
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="partner_simple_form" model="ir.ui.view">
<field name="model">res.partner</field>
<field name="inherit_id" ref="partner_firstname.view_partner_simple_form_firstname"/>
<field
name="inherit_id"
ref="partner_firstname.view_partner_simple_form_firstname"
/>
<field name="arch" type="xml">
<field name="firstname" position="attributes">
<attribute name="attrs">{'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
<attribute
name="attrs"
>{'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
</field>
<field name="lastname" position="attributes">
<attribute name="attrs">{'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
<attribute
name="attrs"
>{'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
</field>
<field name="lastname" position="after">
<field name="lastname2" attrs="{'required': [('firstname', '=', False), ('lastname', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}"/>
<field
name="lastname2"
attrs="{'required': [('firstname', '=', False), ('lastname', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}"
/>
</field>
</field>
</record>
<record id="partner_form" model="ir.ui.view">
<field name="model">res.partner</field>
<field name="inherit_id" ref="partner_firstname.view_partner_form_firstname"/>
<field name="inherit_id" ref="partner_firstname.view_partner_form_firstname" />
<field name="arch" type="xml">
<!-- Main form -->
<field name="firstname" position="attributes">
<attribute name="attrs">{'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
<attribute
name="attrs"
>{'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
</field>
<field name="lastname" position="attributes">
<attribute name="attrs">{'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
<attribute
name="attrs"
>{'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
</field>
<field name="lastname" position="after">
<field name="lastname2" attrs="{'required': [('firstname', '=', False), ('lastname', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}"/>
<field
name="lastname2"
attrs="{'required': [('firstname', '=', False), ('lastname', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}"
/>
</field>
<!-- Inner contact form of child_ids -->
<xpath expr="//field[@name='child_ids']/form//field[@name='firstname']" position="attributes">
<attribute name="attrs">{'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
<xpath
expr="//field[@name='child_ids']/form//field[@name='firstname']"
position="attributes"
>
<attribute
name="attrs"
>{'required': [('lastname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
</xpath>
<xpath expr="//field[@name='child_ids']/form//field[@name='lastname']" position="attributes">
<attribute name="attrs">{'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
<xpath
expr="//field[@name='child_ids']/form//field[@name='lastname']"
position="attributes"
>
<attribute
name="attrs"
>{'required': [('firstname', '=', False), ('lastname2', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}</attribute>
</xpath>
<xpath expr="//field[@name='child_ids']/form//field[@name='lastname']" position="after">
<field name="lastname2" attrs="{'required': [('firstname', '=', False), ('lastname', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}"/>
<xpath
expr="//field[@name='child_ids']/form//field[@name='lastname']"
position="after"
>
<field
name="lastname2"
attrs="{'required': [('firstname', '=', False), ('lastname', '=', False), ('is_company', '=', False), ('type', '=', 'contact')]}"
/>
</xpath>
</field>
</field>
</record>
</odoo>

21
partner_second_lastname/views/res_user.xml

@ -1,24 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
Copyright 2017 Tecnativa - Pedro M. Baeza
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="users_form" model="ir.ui.view">
<field name="name">Add second last name</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="partner_firstname.view_users_form"/>
<field name="inherit_id" ref="partner_firstname.view_users_form" />
<field name="arch" type="xml">
<field name="firstname" position="attributes">
<attribute name="attrs">{'required': [('lastname', '=', False), ('lastname2', '=', False)]}</attribute>
<attribute
name="attrs"
>{'required': [('lastname', '=', False), ('lastname2', '=', False)]}</attribute>
</field>
<field name="lastname" position="attributes">
<attribute name="attrs">{'required': [('firstname', '=', False), ('lastname2', '=', False)]}</attribute>
<attribute
name="attrs"
>{'required': [('firstname', '=', False), ('lastname2', '=', False)]}</attribute>
</field>
<field name="lastname" position="after">
<field name="lastname2" attrs="{'required': [('firstname', '=', False), ('lastname', '=', False)]}" />
<field
name="lastname2"
attrs="{'required': [('firstname', '=', False), ('lastname', '=', False)]}"
/>
</field>
</field>
</field>
</record>
</odoo>
Loading…
Cancel
Save