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 797cb05
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions partner_firstname/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ 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)
if self.name_fields_in_vals(vals):
vals["name"] = self._get_computed_name(
vals.get("lastname", ""), vals.get("firstname", "")
)
else:
name = vals.get("name", partner_context.get("default_name"))
name = vals.get("name", self.env.context.get("default_name"))
if name is not None:
# Calculate the split fields
inverted = self._get_inverse_name(
Expand All @@ -65,17 +61,12 @@ def create(self, vals_list):
),
)
for key, value in inverted.items():
if not vals.get(key) or partner_context.get("copy"):
if not vals.get(key) or self.env.context.get("copy"):
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 @@ -237,6 +228,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 +261,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.")]

0 comments on commit 797cb05

Please sign in to comment.