Skip to content

Commit

Permalink
[IMP] partner_firstname: compatible with standard sql constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
CRogos committed Nov 8, 2024
1 parent d9ec5d0 commit 63db919
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
62 changes: 34 additions & 28 deletions partner_firstname/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,46 @@ def create(self, vals_list):
it's not appropriate, we must call `create` for each partner individually with
the correct context.
"""
created_partners = self.browse()

for vals in vals_list:
partner_context = dict(self.env.context)
if (
not vals.get("is_company")
and self.name_fields_in_vals(vals)
and "name" in vals
):
del vals["name"]
partner_context.pop("default_name", None)
is_company = vals.get(
"is_company", self.default_get(["is_company"])["is_company"]
)

compute_name = False
if self.name_fields_in_vals(vals):
compute_name = self._get_computed_name(
vals.get("lastname", ""), vals.get("firstname", "")
)

if is_company:
if "name" in vals:
# name is leading
vals["lastname"] = vals["name"]
vals["firstname"] = False
elif compute_name:
# fallback to lastname when name missing
vals["name"] = compute_name
vals["lastname"] = compute_name
vals["firstname"] = False
else:
name = vals.get("name", partner_context.get("default_name"))
if name is not None:
name = vals.get("name", self.env.context.get("default_name"))
if compute_name:
vals["name"] = compute_name
# overwrite firstname/lastname with name
elif name is not None:
# Calculate the split fields
inverted = self._get_inverse_name(
self._get_whitespace_cleaned_name(name),
vals.get(
"is_company", self.default_get(["is_company"])["is_company"]
),
is_company,
)
for key, value in inverted.items():
if not vals.get(key) or partner_context.get("copy"):
if not vals.get(key):
vals[key] = value

# Remove the combined fields
vals.pop("name", None)
partner_context.pop("default_name", None)
# pylint: disable=W8121
created_partners |= super(
ResPartner, self.with_context(partner_context)
).create([vals])
return created_partners
return super(ResPartner, self.with_context(no_inverse_name=True)).create(
vals_list
)

def get_extra_default_copy_values(self, order):
"""Method to add '(copy)' suffix to lastname or firstname, depending on name
Expand Down Expand Up @@ -105,7 +113,7 @@ def copy(self, default=None):
order = self._get_names_order()
extra_default_values = self.get_extra_default_copy_values(order)
default.update(extra_default_values)
return super(ResPartner, self.with_context(copy=True)).copy(default)
return super().copy(default)

@api.model
def default_get(self, fields_list):
Expand Down Expand Up @@ -237,6 +245,8 @@ def _get_inverse_name(self, name, is_company=False):

def _inverse_name(self):
"""Try to revert the effect of :meth:`._compute_name`."""
if self.env.context.get("no_inverse_name"):
return
for record in self:
parts = record._get_inverse_name(record.name, record.is_company)
record.lastname = parts["lastname"]
Expand Down Expand Up @@ -268,7 +278,3 @@ def _install_partner_firstname(self):
# Force calculations there
records._inverse_name()
_logger.info("%d partners updated installing module.", len(records))

# Disabling SQL constraint givint a more explicit error using a Python
# contstraint
_sql_constraints = [("check_name", "CHECK( 1=1 )", "Contacts require a name.")]
2 changes: 1 addition & 1 deletion partner_firstname/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def tearDown(self):
def test_copy(self):
"""Copy the partner and compare the result."""
self.expect("%s (copy)" % self.lastname, self.firstname)
self.changed = self.original.with_context(copy=True, lang="en_US").copy()
self.changed = self.original.with_context(lang="en_US").copy()

def test_one_name(self):
"""Test what happens when only one name is given."""
Expand Down
6 changes: 6 additions & 0 deletions partner_firstname/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def setUp(self):
self.good_values.update(lastname=self.values["name"], firstname=False)
self.values = self.good_values.copy()

def test_wrong_name_value(self):
"""Name for company is taken."""

def test_wrong_name_value_and_context(self):
"""Name for company is taken, defaults are ignored"""


class UserCase(PersonCase, MailInstalled):
"""Test ``res.users``."""
Expand Down

0 comments on commit 63db919

Please sign in to comment.