Skip to content

Commit

Permalink
[FIX][8.0][asynchronous_batch_mailings] attachments were exclude from…
Browse files Browse the repository at this point in the history
… final mails once the mailing is asynchronous
  • Loading branch information
Olivier-LAURENT committed Oct 30, 2015
1 parent 3fb800e commit 782437e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ env:

matrix:
- LINT_CHECK="1"
- TESTS="1" ODOO_REPO="odoo/odoo"
- TESTS="1" ODOO_REPO="OCA/OCB"
- TESTS="1" ODOO_REPO="odoo/odoo" EXCLUDE="asynchronous_batch_mailings"
- TESTS="1" ODOO_REPO="OCA/OCB" EXCLUDE="asynchronous_batch_mailings"
- TESTS="1" ODOO_REPO="odoo/odoo" INCLUDE="asynchronous_batch_mailings"
- TESTS="1" ODOO_REPO="OCA/OCB" INCLUDE="asynchronous_batch_mailings"

virtualenv:
system_site_packages: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<record id="mail_worker_pivot" model="ir.config_parameter">
<field name="key">mail_worker_pivot</field>
<field name="value">80</field>
<field name="value">20</field>
</record>

</data>
Expand Down
16 changes: 10 additions & 6 deletions asynchronous_batch_mailings/tests/test_async_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_async_send_mail(self):
from the passing values of the wizard.
"""
cr, uid = self.cr, self.uid

context = {
'lang': 'en_US',
'tz': False,
Expand All @@ -61,10 +62,6 @@ def test_async_send_mail(self):
'name': 'RedHat_spec.doc',
}
attachment_id = ir_att_obj.create(cr, uid, vals, context=context)
vals = {
'name': 'Mitch',
'email': 'mitch@mi.tch',
}

mail_vals = {
'composition_mode': 'mass_mail',
Expand All @@ -79,13 +76,19 @@ def test_async_send_mail(self):
'record_name': 'a',
'model': 'res.partner',
'partner_ids': [[6, False, []]],
'res_id': 6,
'res_id': self.ref('base.res_partner_1'),
'email_from': 'Administrator <admin@example.com>',
'subject': 'v15rIke7MpaRhg15FvqRjzet'
}
model_name = "mail.compose.message"
model_mcm = self.registry(model_name)
new_mcm_id = model_mcm.create(cr, uid, mail_vals, context=context)
vals = model_mcm.read(
cr, uid, [new_mcm_id], [],
context=context, load='_classic_write')[0]
model_mcm._prepare_vals(vals)
session = ConnectorSession(self.cr, ADMIN_USER_ID, context=context)
fc.do_send_mail(session, model_name, mail_vals)
fc.do_send_mail(session, model_name, vals)
model_name = "mail.mail"
model_mail = self.registry(model_name)
mail_ids = model_mail.search(
Expand All @@ -97,4 +100,5 @@ def test_async_send_mail(self):
# Test the content of the mail
read_mail = model_mail.browse(self.cr, ADMIN_USER_ID, mail_ids[0])
self.assertEqual(read_mail.body, '<p>sample body</p>')
self.assertEqual(len(read_mail.attachment_ids), 1)
self.assertEqual(read_mail.attachment_ids[0].name, "RedHat_spec.doc")
49 changes: 31 additions & 18 deletions asynchronous_batch_mailings/wizard/mail_compose_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

_logger = logging.getLogger(__name__)

WORKER_PIVOT = 100
WORKER_PIVOT = 20
CHUNK_SIZE = 100


Expand All @@ -43,14 +43,21 @@ class mail_compose_message(orm.TransientModel):
_inherit = 'mail.compose.message'

def _prepare_vals(self, vals):
vals.pop('id')
"""
Remove useless keys and transform all o2m list values with magic number
"""
for key in ['id', 'notification_ids', 'notified_partner_ids',
'child_ids', 'vote_user_ids', ]:
vals.pop(key, False)
for key in vals.keys():
if(isinstance(vals[key], tuple)):
vals[key] = vals[key][0]
if (isinstance(vals[key], list)):
vals[key] = [
(6, False, vals[key])
]

def send_mail(self, cr, uid, ids, context=None):
"""
Send mail by asynchronous way depending parameters
Send mails by asynchronous way depending on parameters
"""
if context is None:
context = {}
Expand All @@ -64,18 +71,23 @@ def send_mail(self, cr, uid, ids, context=None):
worker_pivot = WORKER_PIVOT
if len(context['active_ids']) > worker_pivot:
res_ids = context['active_ids']
vals = self.read(cr, uid, ids, [], context=context)[0]
vals = self.read(
cr, uid, ids, [],
context=context, load='_classic_write')[0]
self._prepare_vals(vals)

session = ConnectorSession(cr, uid, context=context)
description = _('Send Mail "%s (Chunk Process)') %\
(vals['subject'])
description = _(
'Prepare Mailing for "%s" (Chunk Process)'
) % vals['subject']
_logger.info(
'Delay %s for ids: %s' % (description, res_ids))
prepare_mailings.delay(
session, self._name, vals, res_ids,
description=description, context=context)
return
return {'type': 'ir.actions.act_window_close'}

super(mail_compose_message, self).send_mail(
return super(mail_compose_message, self).send_mail(
cr, uid, ids, context=context)


Expand All @@ -89,9 +101,9 @@ def prepare_mailings(session, model_name, vals, active_ids, context=None):

if not chunck_size.isdigit() or int(chunck_size) < 1:
chunck_size = CHUNK_SIZE
_logger.warning('Could not retrieve a valid value from '
_logger.warning('No valid value provided for '
'"job_mail_chunck_size" parameter. '
'Chunck size is %s') % CHUNK_SIZE
'Value %s is used.' % CHUNK_SIZE)

chunck_size = int(chunck_size)
chunck_list_active_ids = \
Expand All @@ -100,11 +112,13 @@ def prepare_mailings(session, model_name, vals, active_ids, context=None):

i = 1
for chunck_active_ids in chunck_list_active_ids:
description = _('Send mail "%s" (Mailing %d/%d)') %\
description = _('Send Mails "%s" (chunk %s/%s)') %\
(vals['subject'], i, len(chunck_list_active_ids))
i = i + 1
_logger.info(
'Delay %s for ids: %s' % (description, chunck_active_ids))
send_mail.delay(
session, model_name, vals, chunck_active_ids,
self, model_name, vals, chunck_active_ids,
description=description, context=context)


Expand All @@ -119,13 +133,12 @@ def do_send_mail(session, model_name, vals, active_ids=None, context=None):
wizard
:result: send_mail
"""
if context is None:
context = {}
context = dict(context or {})
self, cr, uid = session, session.cr, session.uid
model = session.pool.get(model_name)
model = self.pool.get(model_name)
if active_ids:
context['active_ids'] = active_ids

wiz_id = model.create(self.cr, self.uid, vals, context=context)
wiz_id = model.create(cr, uid, vals, context=context)
context['not_async'] = True
model.send_mail(cr, uid, [wiz_id], context=context)

0 comments on commit 782437e

Please sign in to comment.