partner-contact/partner_display_name_line_break/models/res_partner.py

54 lines
1.9 KiB
Python
Raw Normal View History

2024-10-01 05:51:46 +00:00
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import re
from flectra import api, models
class ResPartner(models.Model):
_inherit = "res.partner"
@api.depends(
"complete_name",
"email",
"vat",
"state_id",
"country_id",
"commercial_company_name",
)
@api.depends_context(
"show_address",
"partner_show_db_id",
"address_inline",
"show_email",
"show_vat",
"lang",
"_two_lines_partner_address",
2024-11-08 07:13:24 +00:00
"_keep_partner_address_type",
2024-10-01 05:51:46 +00:00
)
def _compute_display_name(self): # pylint: disable=W8110
super()._compute_display_name()
if self.env.context.get("_two_lines_partner_address"):
for partner in self:
2024-11-08 07:13:24 +00:00
# Do not split on two lines if name is empty as it would display
# the address type on a new line in the report.
# This happens because Flectra splits the display_name on \n character
# and discards the first element to get the address from the
# display_name. In which case, the address type would appear as
# part of the address.
if not partner.name and not self.env.context.get(
"_keep_partner_address_type"
):
continue
2024-10-01 05:51:46 +00:00
displayed_types = partner._complete_name_displayed_types
type_description = dict(
partner._fields["type"]._description_selection(partner.env)
)
name = partner.name or ""
if not name and partner.type in displayed_types:
name = type_description[partner.type]
if name in partner.display_name:
pattern = r",\s(?=" + re.escape(name) + ")"
partner.display_name = re.sub(pattern, "\n", partner.display_name)