[FIX] core: make check_company's error message clearer

Make the error message more comprehensible for users, using the record's
display name, the company's name, the field's label and value.

    Incompatible companies on records:
     - 'Foo' belongs to company 'X' and 'Contact' (partner_id: 'Bar')
       belongs to another company.

closes odoo/odoo#54883

X-original-commit: e410c90bbddb581100d170bf8180e790bb7af5fd
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2020-07-24 08:37:45 +00:00
parent e1f514f100
commit 4147f1d313
2 changed files with 39 additions and 33 deletions
+16 -11
View File
@@ -5897,6 +5897,14 @@ msgstr ""
msgid ", required"
msgstr ""
#. module: base
#: code:addons/models.py:0
#, python-format
msgid ""
"- %(record)r belongs to company %(company)r and %(field)r (%(fname)s: "
"%(values)s) belongs to another company."
msgstr ""
#. module: base
#: code:addons/model.py:0
#, python-format
@@ -5905,6 +5913,14 @@ msgid ""
"- Delete: another model requires the record being deleted. If possible, archive it instead."
msgstr ""
#. module: base
#: code:addons/models.py:0
#, python-format
msgid ""
"- Record is company %(company)r and %(field)r (%(fname)s: %(values)s) "
"belongs to another company."
msgstr ""
#. module: base
#: model_terms:ir.ui.view,arch_db:base.report_irmodeloverview
msgid "- domain ="
@@ -21686,17 +21702,6 @@ msgstr ""
msgid "Somalia"
msgstr ""
#. module: base
#: code:addons/models.py:0
#, python-format
msgid ""
"Some records are incompatible with the company of the %(document_descr)s.\n"
"\n"
"Incompatibilities:\n"
"Fields: %(fields)s\n"
"Record ids: %(records)s\n"
msgstr ""
#. module: base
#: model:ir.model.fields,help:base.field_res_bank__bic
#: model:ir.model.fields,help:base.field_res_partner_bank__bank_bic
+23 -22
View File
@@ -3211,8 +3211,7 @@ Fields:
if not (regular_fields or property_fields):
return
inconsistent_fields = set()
inconsistent_recs = self.browse()
inconsistencies = []
for record in self:
company = record.company_id if record._name != 'res.company' else record
# The first part of the check verifies that all records linked via relation fields are compatible
@@ -3222,11 +3221,9 @@ Fields:
# Special case with `res.users` since an user can belong to multiple companies.
if corecord._name == 'res.users' and corecord.company_ids:
if not (company <= corecord.company_ids):
inconsistent_fields.add(name)
inconsistent_recs |= record
inconsistencies.append((record, name, corecord))
elif not (corecord.company_id <= company):
inconsistent_fields.add(name)
inconsistent_recs |= record
inconsistencies.append((record, name, corecord))
# The second part of the check (for property / company-dependent fields) verifies that the records
# linked via those relation fields are compatible with the company that owns the property value, i.e.
# the company for which the value is being assigned, i.e:
@@ -3237,24 +3234,28 @@ Fields:
corecord = record.sudo()[name]
if corecord._name == 'res.users' and corecord.company_ids:
if not (company <= corecord.company_ids):
inconsistent_fields.add(name)
inconsistent_recs |= record
inconsistencies.append((record, name, corecord))
elif not (corecord.company_id <= company):
inconsistent_fields.add(name)
inconsistent_recs |= record
inconsistencies.append((record, name, corecord))
if inconsistent_fields:
message = _("""Some records are incompatible with the company of the %(document_descr)s.
Incompatibilities:
Fields: %(fields)s
Record ids: %(records)s
""")
raise UserError(message % {
'document_descr': self.env['ir.model']._get(self._name).name,
'fields': ', '.join(sorted(inconsistent_fields)),
'records': ', '.join([str(a) for a in inconsistent_recs.ids[:6]]),
})
if inconsistencies:
lines = [_("Incompatible companies on records:")]
company_msg = _("- Record is company %(company)r and %(field)r (%(fname)s: %(values)s) belongs to another company.")
record_msg = _("- %(record)r belongs to company %(company)r and %(field)r (%(fname)s: %(values)s) belongs to another company.")
for record, name, corecords in inconsistencies[:5]:
if record._name == 'res.company':
msg, company = company_msg, record
else:
msg, company = record_msg, record.company_id
field = self.env['ir.model.fields']._get(self._name, name)
lines.append(msg % {
'record': record.display_name,
'company': company.display_name,
'field': field.field_description,
'fname': field.name,
'values': ", ".join(repr(rec.display_name) for rec in corecords),
})
raise UserError("\n".join(lines))
@api.model
def check_access_rights(self, operation, raise_exception=True):