Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions poweremail_oorq/poweremail_send_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ def save_to_mailbox(self, cursor, uid, ids, context=None):
for rec_id in new_rec_ids:
ctx['screen_vals'] = wiz
ctx['src_rec_ids'] = rec_id
if wiz['priority'] == '2':
job = self.save_to_mailbox_in_background_at_front(cursor, uid, ctx)
else:
job = self.save_to_mailbox_in_background(cursor, uid, ctx)
job_props = {
'on_commit': ctx.pop('on_commit', False),
'is_high_priority': wiz['priority'] == '2'
}
job = self.save_to_mailbox_async(cursor, uid, job_props, ctx)

j_pool.add_job(job)
if 'screen_vals' in ctx:
del ctx['screen_vals']
Expand All @@ -81,27 +83,44 @@ def save_to_mailbox(self, cursor, uid, ids, context=None):
mailbox_obj.write(cursor, uid, to_write, {'folder': 'outbox'}, ctx)
return res

@job(queue=config.get('poweremail_render_queue', 'poweremail'), at_front=True)
def save_to_mailbox_in_background_at_front(self, cursor, uid, context):
mailbox_obj = self.pool.get('poweremail.mailbox')
if not context:
def save_to_mailbox_async(self, cursor, uid, job_props, context=None):
if context is None:
context = {}
screen_vals = context.get('screen_vals', {})
ctx = context.copy()
del ctx['screen_vals']
if not screen_vals:
raise Exception("No screen_vals found in the context!")
on_commit = job_props.get('on_commit', False)
is_high_priority = job_props.get('is_high_priority', False)
if is_high_priority:
if on_commit:
job = self.save_to_mailbox_in_background_at_front_on_commit(cursor, uid, context)
else:
job = self.save_to_mailbox_in_background_at_front(cursor, uid, context)
else:
if on_commit:
job = self.save_to_mailbox_in_background_on_commit(cursor, uid, context)
else:
job = self.save_to_mailbox_in_background(cursor, uid, context)
return job

wiz_id = self.create(cursor, uid, screen_vals, ctx)
mail_ids = super(PoweremailSendWizard,
self).save_to_mailbox(cursor, uid, [wiz_id], ctx)
# When using `save_async`, we leave the folder as it should be
if not(context.get('save_async', False)):
mailbox_obj.write(cursor, uid, mail_ids, {'folder': 'drafts'}, ctx)
return mail_ids
@job(queue=config.get('poweremail_render_queue', 'poweremail'), at_front=True)
def save_to_mailbox_in_background_at_front(self, cursor, uid, context):
return self._save_to_mailbox_async_base(cursor, uid, context)

@job(queue=config.get('poweremail_render_queue', 'poweremail'))
def save_to_mailbox_in_background(self, cursor, uid, context):
return self._save_to_mailbox_async_base(cursor, uid, context)

@job(queue=config.get('poweremail_render_queue', 'poweremail'), on_commit=True)
def save_to_mailbox_in_background_on_commit(self, cursor, uid, context):
return self._save_to_mailbox_async_base(cursor, uid, context)

@job(queue=config.get('poweremail_render_queue', 'poweremail'), on_commit=True, at_front=True)
def save_to_mailbox_in_background_at_front_on_commit(self, cursor, uid, context):
return self._save_to_mailbox_async_base(cursor, uid, context)

def _save_to_mailbox_async_base(self, cursor, uid, context):
"""
Method is not async by itself. Don't call it directly
Call it from a method with @job
"""
mailbox_obj = self.pool.get('poweremail.mailbox')
if not context:
context = {}
Expand All @@ -115,7 +134,7 @@ def save_to_mailbox_in_background(self, cursor, uid, context):
mail_ids = super(PoweremailSendWizard,
self).save_to_mailbox(cursor, uid, [wiz_id], ctx)
# When using `save_async`, we leave the folder as it should be
if not(context.get('save_async', False)):
if not (context.get('save_async', False)):
mailbox_obj.write(cursor, uid, mail_ids, {'folder': 'drafts'}, ctx)
return mail_ids

Expand Down
147 changes: 147 additions & 0 deletions poweremail_oorq/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,153 @@ def test_enqueue_high_priority_mail_goes_first_position_of_render_queue(self):
('poweremail.send.wizard', 'save_to_mailbox_in_background_at_front')
)

def test_enqueue_high_priority_on_commit_mail_goes_first_position_of_render_queue(self):
imd_obj = self.openerp.pool.get('ir.model.data')
send_obj = self.openerp.pool.get('poweremail.send.wizard')

job = self.q.enqueue(enqueue_dummy_method)
self.assertEqual(len(self.q), 1)
self.assertEqual(self.q.jobs[0], job)

cursor = self.cursor
uid = self.uid
partner_id = imd_obj.get_object_reference(
cursor, uid, 'base', 'res_partner_asus'
)[1]

tmpl_id = self.create_template()

ctx = {
'active_id': partner_id,
'active_ids': [partner_id],
'src_rec_ids': [partner_id],
'src_model': 'res.partner',
'template_id': tmpl_id
}

wiz_id = send_obj.create(cursor, uid, {}, context=ctx)
wiz = send_obj.browse(cursor, uid, wiz_id)
self.assertEqual(wiz.priority, '2')

ctx2 = ctx.copy()
ctx2['save_async'] = True
ctx2['on_commit'] = True

wiz.save_to_mailbox(context=ctx2)
# Hack to put the job in to the queue without commiting
DB_CURSOR_COMMIT.send(cursor)
self.assertEqual(len(self.q), 2)
first_job = self.q.jobs[0]
self.assertEqual(
first_job.args[3:5],
('poweremail.send.wizard', 'save_to_mailbox_in_background_at_front_on_commit')
)

wiz_id = send_obj.create(cursor, uid, {}, context=ctx)
send_obj.write(cursor, uid, [wiz_id], {'priority': '1'})
wiz = send_obj.browse(cursor, uid, wiz_id)
self.assertEqual(wiz.priority, '1')

wiz.save_to_mailbox(context=ctx2)
# Hack to put the job in to the queue without commiting
DB_CURSOR_COMMIT.send(cursor)
self.assertEqual(len(self.q), 3)

last_job = self.q.jobs[-1]
self.assertEqual(
last_job.args[3:5],
('poweremail.send.wizard', 'save_to_mailbox_in_background_on_commit')
)

first_job = self.q.jobs[0]
self.assertEqual(
first_job.args[3:5],
('poweremail.send.wizard', 'save_to_mailbox_in_background_at_front_on_commit')
)

def test_save_to_mailbox_high_priority_on_commit(self):
imd_obj = self.openerp.pool.get('ir.model.data')
send_obj = self.openerp.pool.get('poweremail.send.wizard')

job = self.q.enqueue(enqueue_dummy_method)
self.assertEqual(len(self.q), 1)
self.assertEqual(self.q.jobs[0], job)

cursor = self.cursor
uid = self.uid
partner_id = imd_obj.get_object_reference(
cursor, uid, 'base', 'res_partner_asus'
)[1]

tmpl_id = self.create_template()

ctx = {
'active_id': partner_id,
'active_ids': [partner_id],
'src_rec_ids': [partner_id],
'src_model': 'res.partner',
'template_id': tmpl_id
}

wiz_id = send_obj.create(cursor, uid, {}, context=ctx)
wiz = send_obj.browse(cursor, uid, wiz_id)
self.assertEqual(wiz.priority, '2')

ctx2 = ctx.copy()
ctx2['save_async'] = True
ctx2['on_commit'] = True
wiz.save_to_mailbox(context=ctx2)
# Hack to put the job in to the queue without commiting
DB_CURSOR_COMMIT.send(cursor)
self.assertEqual(len(self.q), 2)
first_job = self.q.jobs[0]
self.assertEqual(
first_job.args[3:5],
('poweremail.send.wizard', 'save_to_mailbox_in_background_at_front_on_commit')
)

def test_save_to_mailbox_normal_priority_on_commit(self):
imd_obj = self.openerp.pool.get('ir.model.data')
send_obj = self.openerp.pool.get('poweremail.send.wizard')

job = self.q.enqueue(enqueue_dummy_method)
self.assertEqual(len(self.q), 1)
self.assertEqual(self.q.jobs[0], job)

cursor = self.cursor
uid = self.uid
partner_id = imd_obj.get_object_reference(
cursor, uid, 'base', 'res_partner_asus'
)[1]

tmpl_id = self.create_template()

ctx = {
'active_id': partner_id,
'active_ids': [partner_id],
'src_rec_ids': [partner_id],
'src_model': 'res.partner',
'template_id': tmpl_id
}

wiz_id = send_obj.create(cursor, uid, {}, context=ctx)
send_obj.write(cursor, uid, [wiz_id], {'priority': '1'})
wiz = send_obj.browse(cursor, uid, wiz_id)
self.assertEqual(wiz.priority, '1')

ctx2 = ctx.copy()
ctx2['save_async'] = True
ctx2['on_commit'] = True
wiz.save_to_mailbox(context=ctx2)
# Hack to put the job in to the queue without commiting
DB_CURSOR_COMMIT.send(cursor)
self.assertEqual(len(self.q), 2)
last_job = self.q.jobs[-1]
self.assertEqual(
last_job.args[3:5],
('poweremail.send.wizard', 'save_to_mailbox_in_background_on_commit')
)

def test_generate_mail_in_background_high_priority(self):
imd_obj = self.openerp.pool.get('ir.model.data')
tmpl_obj = self.openerp.pool.get('poweremail.templates')
Expand Down