[IMP]URD Newsletter : better constraint on mailing lists

* Refactoring from tabs to spaces ;
* Add constrains with create/write overwrite ;
* Now allow to not raise on double email on creation (but no real insert) ;
* Raise on write.
This commit is contained in:
Fabien BOURGEOIS 2020-03-25 09:39:12 +01:00
parent 3663753a60
commit f0edff138f
3 changed files with 180 additions and 151 deletions

View File

@ -34,7 +34,7 @@
# Check https://github.com/odoo/odoo/blob/master/openerp/addons/base/module/module_data.xml
# for the full list
'category': 'Marketing',
'version': '0.5',
'version': '8.0.0.5.1',
# any module necessary for this one to work correctly
'depends': ['base','mass_mailing','marketing','website_mail'],

View File

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
def migrate(cr, version):
cr.execute('''ALTER TABLE mail_mass_mailing_contact
DROP CONSTRAINT mail_mass_mailing_contact_email_list_unique''')

View File

@ -2,6 +2,7 @@
import urlparse
import werkzeug.urls
import logging
from openerp import models, fields, api
from openerp.osv import osv
@ -9,7 +10,6 @@ from openerp.osv import osv
from openerp import tools
from openerp.tools.translate import _
import logging
_logger = logging.getLogger(__name__)
# Overrides mass mailing Contact for this module purpose
@ -20,11 +20,11 @@ class contact(models.Model):
unsubscription_date = fields.Date(string="Unsubscription date")
unsubscribed_by_odoo_user = fields.Many2one("res.users", string="Unsubscribed by this Odoo user")
_sql_constraints = [
('email_list_unique',
'unique (email, list_id)',
_('Subscriber already registered for this mailing list.')
)]
# _sql_constraints = [
# ('email_list_unique',
# 'unique (email, list_id)',
# _('Subscriber already registered for this mailing list.')
# )]
@api.onchange("opt_out")
def on_change_opt_out(self):
@ -35,6 +35,30 @@ class contact(models.Model):
self.unsubscription_date = None
self.unsubscribed_by_odoo_user = None
@api.model
@api.returns('self', lambda rec: rec.id)
def create(self, vals):
if 'list_id' in vals and 'email' in vals:
domain = [('list_id', '=', vals['list_id']),
('email', 'ilike', vals['email'])]
res = self.env['mail.mass_mailing.contact'].search(domain)
if len(res.ids) > 0:
return res[0] # Workaround...
return super(contact, self).create(vals)
@api.multi
def write(self, values):
""" Ensures that there is no doubles in email for the same list """
for ctc in self:
domain = [('list_id', '=', ctc.list_id.id),
('email', 'ilike', ctc.email)]
res = self.env['mail.mass_mailing.contact'].search(domain)
if len(res.ids) > 1:
_logger.warning(res)
raise models.ValidationError(
_('Subscriber %s already registered for this mailing list.') % ctc.email
)
return super(contact, self).write(values)