[IMP] mail, mass_mailing, crm : stringify _primary_email
_primary_email was an array to be able to use it directly in the depends. Typically a 'depends' with a computed value (lambda) needs an array. Otherwise, it will only take the first character of the field name string. By setting _primary_email as a string, the lambda should always get an array. But it simplifies a lot the usage of the _primary_email. also, having an array was confusing as we could think that more than one field can be considered as the primary email. This is not the goal hare. So having the _primary_email as a single string is more 'self describing'. Only one field should be the _primary_email.
This commit is contained in:
@@ -51,7 +51,7 @@ class Lead(models.Model):
|
||||
_description = "Lead/Opportunity"
|
||||
_order = "priority desc,activity_date_deadline,id desc"
|
||||
_inherit = ['mail.thread', 'mail.activity.mixin', 'utm.mixin', 'format.address.mixin', 'mail.blacklist.mixin']
|
||||
_primary_email = ['email_from']
|
||||
_primary_email = 'email_from'
|
||||
|
||||
def _default_probability(self):
|
||||
stage_id = self._default_stage_id()
|
||||
|
||||
@@ -20,24 +20,20 @@ class MailAddressMixin(models.AbstractModel):
|
||||
"""
|
||||
_name = 'mail.address.mixin'
|
||||
_description = 'Email address mixin'
|
||||
_primary_email = ['email']
|
||||
_primary_email = 'email'
|
||||
|
||||
email_normalized = fields.Char(string='Normalized email address', compute="_compute_email_normalized", invisible=True,
|
||||
compute_sudo=True, store=True, help="""This field is used to search on email address,
|
||||
as the primary email field can contain more than strictly an email address.""")
|
||||
|
||||
@api.depends(lambda self: self._primary_email)
|
||||
@api.depends(lambda self: [self._primary_email])
|
||||
def _compute_email_normalized(self):
|
||||
self._assert_primary_email()
|
||||
[email_field] = self._primary_email
|
||||
for record in self:
|
||||
record.email_normalized = tools.email_normalize(record[email_field])
|
||||
record.email_normalized = tools.email_normalize(record[self._primary_email])
|
||||
|
||||
def _assert_primary_email(self):
|
||||
if not hasattr(self, "_primary_email") or \
|
||||
not isinstance(self._primary_email, (list, tuple)) or \
|
||||
not len(self._primary_email) == 1:
|
||||
if not hasattr(self, "_primary_email") or not isinstance(self._primary_email, str):
|
||||
raise UserError(_('Invalid primary email field on model %s') % self._name)
|
||||
field_name = self._primary_email[0]
|
||||
if field_name not in self._fields or self._fields[field_name].type != 'char':
|
||||
if self._primary_email not in self._fields or self._fields[self._primary_email].type != 'char':
|
||||
raise UserError(_('Invalid primary email field on model %s') % self._name)
|
||||
|
||||
@@ -24,7 +24,7 @@ class ChannelPartner(models.Model):
|
||||
_description = 'Listeners of a Channel'
|
||||
_table = 'mail_channel_partner'
|
||||
_rec_name = 'partner_id'
|
||||
_primary_email = ['partner_email']
|
||||
_primary_email = 'partner_email'
|
||||
|
||||
partner_id = fields.Many2one('res.partner', string='Recipient', ondelete='cascade')
|
||||
partner_email = fields.Char('Email', related='partner_id.email', readonly=False)
|
||||
|
||||
@@ -10,7 +10,7 @@ class MassMailTest(models.Model):
|
||||
_description = 'Simple Mass Mailing Model'
|
||||
_name = 'mass.mail.test'
|
||||
_inherit = ['mail.thread', 'mail.address.mixin']
|
||||
_primary_email = ['email_from']
|
||||
_primary_email = 'email_from'
|
||||
|
||||
name = fields.Char()
|
||||
email_from = fields.Char()
|
||||
@@ -22,7 +22,7 @@ class MassMailTestBlacklist(models.Model):
|
||||
_name = 'mass.mail.test.bl'
|
||||
_inherit = ['mail.thread', 'mail.blacklist.mixin']
|
||||
|
||||
_primary_email = ['email_from'] # blacklist field to check
|
||||
_primary_email = 'email_from' # blacklist field to check
|
||||
|
||||
name = fields.Char()
|
||||
email_from = fields.Char()
|
||||
|
||||
@@ -25,23 +25,15 @@ class TestBLMixin(common.MassMailingCase):
|
||||
|
||||
@users('emp')
|
||||
def test_bl_mixin_primary_field_consistency(self):
|
||||
MassMailTestBlacklist._primary_email = ['not_a_field']
|
||||
MassMailTestBlacklist._primary_email = 'not_a_field'
|
||||
with self.assertRaises(UserError):
|
||||
self.env['mass.mail.test.bl'].search([('is_blacklisted', '=', False)])
|
||||
|
||||
MassMailTestBlacklist._primary_email = 'not_a_list'
|
||||
MassMailTestBlacklist._primary_email = ['not_a_str']
|
||||
with self.assertRaises(UserError):
|
||||
self.env['mass.mail.test.bl'].search([('is_blacklisted', '=', False)])
|
||||
|
||||
MassMailTestBlacklist._primary_email = 'email_from'
|
||||
with self.assertRaises(UserError):
|
||||
self.env['mass.mail.test.bl'].search([('is_blacklisted', '=', False)])
|
||||
|
||||
MassMailTestBlacklist._primary_email = ['email_from', 'name']
|
||||
with self.assertRaises(UserError):
|
||||
self.env['mass.mail.test.bl'].search([('is_blacklisted', '=', False)])
|
||||
|
||||
MassMailTestBlacklist._primary_email = ['email_from']
|
||||
self.env['mass.mail.test.bl'].search([('is_blacklisted', '=', False)])
|
||||
|
||||
@users('emp')
|
||||
|
||||
Reference in New Issue
Block a user