Code refactoring

This commit is contained in:
Youssef Elouahby 2018-11-22 23:13:30 +01:00
parent ae37615c5e
commit 47060e7577
2 changed files with 45 additions and 82 deletions

View File

@ -33,6 +33,7 @@ class GolemMailPresendWizard(models.TransientModel):
self.ensure_one() self.ensure_one()
wizard = self[0] wizard = self[0]
emails = wizard.recipient_ids.filtered(lambda r: not r.partner_id).mapped('email') emails = wizard.recipient_ids.filtered(lambda r: not r.partner_id).mapped('email')
semails = list(set([item.strip() for item in emails]))
partners = wizard.recipient_ids.filtered(lambda r: not r.opt_out).mapped('partner_id').ids partners = wizard.recipient_ids.filtered(lambda r: not r.opt_out).mapped('partner_id').ids
return { return {
'name' : _('Search results'), 'name' : _('Search results'),
@ -46,7 +47,7 @@ class GolemMailPresendWizard(models.TransientModel):
'default_no_auto_thread': False, 'default_no_auto_thread': False,
'default_from_presend_wizard': True, 'default_from_presend_wizard': True,
'partners_from_wizard': partners, 'partners_from_wizard': partners,
'emails_from_wizard': emails}, 'emails_from_wizard': semails},
'view_mode': 'form', 'view_mode': 'form',
'target': 'new' 'target': 'new'
} }

View File

@ -82,15 +82,9 @@ class MailComposer(models.TransientModel):
subtype_id = wizard.subtype_id.id subtype_id = wizard.subtype_id.id
else: else:
subtype_id = self.sudo().env.ref('mail.mt_comment', raise_if_not_found=False).id subtype_id = self.sudo().env.ref('mail.mt_comment', raise_if_not_found=False).id
batch_mails = Mail
for res_ids in sliced_res_ids: for res_ids in sliced_res_ids:
batch_mails = Mail all_mail_values = wizard.get_mail_values(res_ids)
if wizard.from_presend_wizard:
emails_values, all_mail_values = wizard.get_mail_values(res_ids)
for email_val in emails_values:
batch_mails |= Mail.create(email_val)
else:
all_mail_values = wizard.get_mail_values(res_ids)
for res_id, mail_values in all_mail_values.iteritems(): for res_id, mail_values in all_mail_values.iteritems():
if wizard.composition_mode == 'mass_mail': if wizard.composition_mode == 'mass_mail':
batch_mails |= Mail.create(mail_values) batch_mails |= Mail.create(mail_values)
@ -99,84 +93,52 @@ class MailComposer(models.TransientModel):
message_type=wizard.message_type, message_type=wizard.message_type,
subtype_id=subtype_id, subtype_id=subtype_id,
**mail_values) **mail_values)
if self.env.context.get('emails_from_wizard', False):
mails = self._context['emails_from_wizard']
emails_values = wizard.get_mail_values_from_mails(mails)
for email_val in emails_values:
batch_mails |= Mail.create(email_val)
if wizard.composition_mode == 'mass_mail': if wizard.composition_mode == 'mass_mail':
batch_mails.send(auto_commit=auto_commit) batch_mails.send(auto_commit=auto_commit)
return {'type': 'ir.actions.act_window_close'} return {'type': 'ir.actions.act_window_close'}
@api.multi @api.multi
def get_mail_values(self, res_ids): def get_mail_values_from_mails(self, mails):
"""Generate the values that will be used by send_mail to create mail_messages
or mail_mails. """
self.ensure_one() self.ensure_one()
results = dict.fromkeys(res_ids, False) mail_values = {
rendered_values = {} 'subject': self.subject,
mass_mail_mode = self.composition_mode == 'mass_mail' 'body': self.body or '',
'parent_id': self.parent_id and self.parent_id.id,
# render all template-based value at once 'partner_ids': [partner.id for partner in self.partner_ids],
if mass_mail_mode and self.model: 'attachment_ids': [attach.id for attach in self.attachment_ids],
rendered_values = self.render_message(res_ids) 'author_id': self.author_id.id,
# compute alias-based reply-to in batch 'email_from': self.email_from,
reply_to_value = dict.fromkeys(res_ids, None) 'record_name': self.record_name,
if mass_mail_mode and not self.no_auto_thread: 'no_auto_thread': self.no_auto_thread,
# reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).browse(res_ids).message_get_reply_to(default=self.email_from) 'mail_server_id': self.mail_server_id.id,
reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).message_get_reply_to(res_ids, default=self.email_from)
for res_id in res_ids:
# static wizard (mail.message) values
mail_values = {
'subject': self.subject,
'body': self.body or '',
'parent_id': self.parent_id and self.parent_id.id,
'partner_ids': [partner.id for partner in self.partner_ids],
'attachment_ids': [attach.id for attach in self.attachment_ids],
'author_id': self.author_id.id,
'email_from': self.email_from,
'record_name': self.record_name,
'no_auto_thread': self.no_auto_thread,
'mail_server_id': self.mail_server_id.id,
} }
mail_values.update(notification=not self.auto_delete_message,
# mass mailing: rendering override wizard static values model=self.model,
if mass_mail_mode and self.model: record_name=False)
if self.model in self.env and hasattr(self.env[self.model], 'message_get_email_values'): # auto deletion of mail_mail
mail_values.update(self.env[self.model].browse(res_id).message_get_email_values()) if self.auto_delete or self.template_id.auto_delete:
# keep a copy unless specifically requested, reset record name (avoid browsing records) mail_values['auto_delete'] = True
mail_values.update(notification=not self.auto_delete_message, model=self.model, res_id=res_id, record_name=False) mail_values['reply_to'] = mail_values['email_from']
# auto deletion of mail_mail mail_values['body_html'] = mail_values.get('body', '')
if self.auto_delete or self.template_id.auto_delete: attachment_ids = []
mail_values['auto_delete'] = True for attach_id in mail_values.pop('attachment_ids'):
# rendered values using template new_attach_id = self.env['ir.attachment'].browse(attach_id).copy({
email_dict = rendered_values[res_id] 'res_model': self._name,
mail_values['partner_ids'] += email_dict.pop('partner_ids', []) 'res_id': self.id
mail_values.update(email_dict) })
if not self.no_auto_thread: attachment_ids.append(new_attach_id.id)
mail_values.pop('reply_to') mail_values['attachment_ids'] = self.env['mail.thread']._message_preprocess_attachments(
if reply_to_value.get(res_id): mail_values.pop('attachments', []),
mail_values['reply_to'] = reply_to_value[res_id] attachment_ids, 'mail.message', 0)
if self.no_auto_thread and not mail_values.get('reply_to'):
mail_values['reply_to'] = mail_values['email_from']
# mail_mail values: body -> body_html, partner_ids -> recipient_ids
mail_values['body_html'] = mail_values.get('body', '')
mail_values['recipient_ids'] = [(4, id) for id in mail_values.pop('partner_ids', [])]
# process attachments: should not be encoded before being processed by message_post / mail_mail create
mail_values['attachments'] = [(name, base64.b64decode(enc_cont)) for name, enc_cont in email_dict.pop('attachments', list())]
attachment_ids = []
for attach_id in mail_values.pop('attachment_ids'):
new_attach_id = self.env['ir.attachment'].browse(attach_id).copy({'res_model': self._name, 'res_id': self.id})
attachment_ids.append(new_attach_id.id)
mail_values['attachment_ids'] = self.env['mail.thread']._message_preprocess_attachments(
mail_values.pop('attachments', []),
attachment_ids, 'mail.message', 0)
results[res_id] = mail_values
emails_result = [] emails_result = []
if self.from_presend_wizard: for email in mails:
for email in self._context['emails_from_wizard']: mail_values['email_to'] = email
result = results[res_ids[0]] emails_result.append(dict(mail_values))
result.pop('recipient_ids', []) return emails_result
result.pop('res_id', [])
result['email_to'] = email
emails_result.append(dict(result))
return emails_result, results
return results