[FIX] base: Traceback with address layout in reports

What are the steps to reproduce your issue ?

    1. Go to Contacts > Configuration > Localization > Countries > Select a country
    2. Edit the 'Layout in reports' (enable debug mode) and add an invalid field
    3. Create a Quotation and search for a customer from the country you edited.

What is currently happening ?

    Traceback is raised and you can't select the customer anymore

Why is this happening ?

    Because the key is not defined.

How to fix the bug ?

    Handle KeyError to detect if the user put an undefined key
    Handle ValueError to detect if the user put an invalid format key

opw-2447078

closes odoo/odoo#69014

X-original-commit: 675feb254ed3448571818a77c920012cbd066c8b
Signed-off-by: Nicolas Lempereur (nle) <nle@odoo.com>
Signed-off-by: Achraf <abz-odoo@users.noreply.github.com>
This commit is contained in:
Achraf (abz)
2021-04-09 07:19:15 +00:00
parent 207c4dda5c
commit 7c9afd09b8
2 changed files with 16 additions and 0 deletions
+6
View File
@@ -23383,6 +23383,12 @@ msgstr ""
msgid "The internal user in charge of this contact."
msgstr ""
#. module: base
#: code:addons/base/models/res_country.py:106
#, python-format
msgid "The layout contains an invalid format key"
msgstr ""
#. module: base
#: model:ir.model.fields,help:base.field_ir_model__inherited_model_ids
msgid "The list of models that extends the current model."
+10
View File
@@ -5,6 +5,7 @@ import re
import logging
from odoo import api, fields, models
from odoo.osv import expression
from odoo.exceptions import UserError
from psycopg2 import IntegrityError
from odoo.tools.translate import _
_logger = logging.getLogger(__name__)
@@ -120,6 +121,15 @@ class Country(models.Model):
code = FLAG_MAPPING.get(country.code, country.code.lower())
country.image_url = "/base/static/img/country_flags/%s.png" % code
@api.constrains('address_format')
def _check_address_format(self):
for record in self:
if record.address_format:
address_fields = self.env['res.partner']._formatting_address_fields() + ['state_code', 'state_name', 'country_code', 'country_name', 'company_name']
try:
record.address_format % {i: 1 for i in address_fields}
except (ValueError, KeyError):
raise UserError(_('The layout contains an invalid format key'))
class CountryGroup(models.Model):
_description = "Country Group"