[FIX] mail: check unicity inside alias names given for unicity check
In addition to checking conflicts with existing aliases, we have to check that given name list also contains only unique names. Otherwise creating in batch with duplicates raises the SQL unicity constraint instead of the expected UserError. Task-3453343 (Mail: Cleanup Alias Usage) Prepares Task-36879 (Mail: Support MultiCompany Aliases) Part-of: odoo/odoo#130632
This commit is contained in:
@@ -194,11 +194,21 @@ class Alias(models.Model):
|
||||
:param list sanitized_names: a list of names (considered as sanitized
|
||||
and ready to be sent to DB);
|
||||
"""
|
||||
valid_names = list(filter(None, sanitized_names))
|
||||
|
||||
# list itself should be unique obviously
|
||||
seen = set()
|
||||
dupes = [name for name in valid_names if name in seen or seen.add(name)]
|
||||
if dupes:
|
||||
raise UserError(
|
||||
_('Email aliases %(alias_name)s cannot be used on several records at the same time. Please update records one by one.',
|
||||
alias_name=', '.join(dupes))
|
||||
)
|
||||
|
||||
catchall_alias = self.env['ir.config_parameter'].sudo().get_param('mail.catchall.alias')
|
||||
bounce_alias = self.env['ir.config_parameter'].sudo().get_param('mail.bounce.alias')
|
||||
alias_domain = self.env["ir.config_parameter"].sudo().get_param("mail.catchall.domain")
|
||||
valid_domain_aliases = filter(None, (bounce_alias, catchall_alias))
|
||||
valid_names = list(filter(None, sanitized_names))
|
||||
|
||||
# matches catchall or bounce alias
|
||||
for sanitized_name in sanitized_names:
|
||||
|
||||
@@ -117,6 +117,14 @@ class TestMailAlias(TestMailAliasCommon):
|
||||
'alias_model_id': alias_model_id,
|
||||
'alias_name': 'unused.test.alias',
|
||||
})
|
||||
with self.assertRaises(exceptions.UserError), self.cr.savepoint():
|
||||
self.env['mail.alias'].create([
|
||||
{
|
||||
'alias_model_id': alias_model_id,
|
||||
'alias_name': alias_name,
|
||||
}
|
||||
for alias_name in ('new.alias.1', 'new.alias.2', 'new.alias.1')
|
||||
])
|
||||
with self.assertRaises(psycopg2.errors.UniqueViolation), self.cr.savepoint(), mute_logger('odoo.sql_db'):
|
||||
new_mail_alias.copy({'alias_name': 'unused.test.alias'})
|
||||
with self.assertRaises(exceptions.UserError), self.cr.savepoint():
|
||||
|
||||
Reference in New Issue
Block a user