[IMP] email.compose.message: improvements for mass mailing and email_teplate

bzr revid: rha@tinyerp.com-20110414083543-0aq55ib9j2dn0ouw
This commit is contained in:
Rifakat Haradwala (Open ERP) 2011-04-14 14:05:43 +05:30
commit 482ca7ddd3
3 changed files with 86 additions and 133 deletions

View File

@ -28,90 +28,43 @@ class email_compose_message(osv.osv_memory):
_name = 'email.compose.message'
_inherit = 'email.compose.message'
def get_template_data(self, cr, uid, res_id, template_id, context=None):
if context is None:
context = {}
def get_template_data(self, cr, uid, template_id, context=None):
result = {}
template_pool = self.pool.get('email.template')
if template_id:
template = template_pool.get_email_template(cr, uid, template_id=template_id, context=context)
def _get_template_value(field):
if not template:
return False
if len(context.get('src_rec_ids',[])) > 1: # Multiple Mail: Gets original template values for multiple email change
return getattr(template, field)
else: # Simple Mail: Gets computed template values
return template_pool.get_template_value(cr, uid, getattr(template, field), template.model, context.get('active_id'), context)
body = _get_template_value('body') or False
if context.get('active_model') and context.get('active_id') and template.user_signature:
model_pool = self.pool.get(context['active_model'])
user = model_pool.browse(cr, uid, context['active_id'], context=context).user_id
signature = user and user.signature or ''
body = body + '\n' + signature
result.update({
'template_id' : template.id,
'smtp_server_id' : template.smtp_server_id.id,
'body' : body,
'subject' : _get_template_value('subject') or False,
'attachment_ids' : template_pool.read(cr, uid, template.id, ['attachment_ids'])['attachment_ids'] or [],
'res_id' : res_id or False,
'email_to' : _get_template_value('email_to') or False,
'email_cc' : _get_template_value('email_cc') or False,
'email_bcc' : _get_template_value('email_bcc') or False,
'reply_to' : _get_template_value('reply_to') or False,
'model' : template.model or False,
'auto_delete': template.auto_delete
})
return result
def default_get(self, cr, uid, fields, context=None):
if not template_id:
return result
if context is None:
context = {}
result = super(email_compose_message, self).default_get(cr, uid, fields, context=context)
template_id = context.get('template_id', False)
vals = {}
if template_id and context.get('active_model') and context.get('active_id'):
vals = self.get_template_data(cr, uid, context.get('active_id'), template_id, context)
template_pool = self.pool.get('email.template')
resource_id = context.get('active_id')
template = template_pool.get_email_template(cr, uid, template_id=template_id, context=context)
if not vals:
return result
def _get_template_value(field):
if context.get('mass_mail',False): # Mass Mail: Gets original template values for multiple email change
return getattr(template, field)
else:
return self.get_template_value(cr, uid, getattr(template, field), template.model, resource_id, context=context)
if 'template_id' in fields:
result.update({'template_id' : vals.get('template_id', False)})
if 'smtp_server_id' in fields:
result.update({'smtp_server_id' : vals.get('smtp_server_id', False)})
if 'attachment_ids' in fields:
result.update({'attachment_ids' : vals.get('attachment_ids', False)})
if 'model' in fields:
result.update({'model' : vals.get('model', False)})
if 'res_id' in fields:
result.update({'res_id' : vals.get('res_id', False)})
if 'email_to' in fields:
result.update({'email_to' : vals.get('email_to', False)})
if 'email_cc' in fields:
result.update({'email_cc' : vals.get('email_cc', False)})
if 'email_bcc' in fields:
result.update({'email_bcc' : vals.get('email_bcc', False)})
if 'subject' in fields:
result.update({'subject' : vals.get('name', False)})
if 'body' in fields:
result.update({'body' : vals.get('body', False)})
if 'reply_to' in fields:
result.update({'reply_to' : vals.get('reply_to', False)})
body = _get_template_value('body') or False
if context.get('active_model') and resource_id and template.user_signature:
model_pool = self.pool.get(context['active_model'])
user = model_pool.browse(cr, uid, resource_id, context=context).user_id
signature = user and user.signature or ''
body = body + '\n' + signature
result.update({
'template_id' : template.id,
'smtp_server_id' : template.smtp_server_id.id,
'body' : body,
'subject' : _get_template_value('subject') or False,
'attachment_ids' : template_pool.read(cr, uid, template.id, ['attachment_ids'])['attachment_ids'] or [],
'res_id' : resource_id or False,
'email_to' : _get_template_value('email_to') or False,
'email_cc' : _get_template_value('email_cc') or False,
'email_bcc' : _get_template_value('email_bcc') or False,
'reply_to' : _get_template_value('reply_to') or False,
'model' : template.model or False,
'auto_delete': template.auto_delete
})
return result
def _get_templates(self, cr, uid, context=None):
@ -138,17 +91,14 @@ class email_compose_message(osv.osv_memory):
'template_id': fields.selection(_get_templates, 'Template'),
}
def get_template_value(self, cr, uid, message, model, resource_id, context=None):
template_pool = self.pool.get('email.template')
return template_pool.get_template_value(cr, uid, message, model, resource_id, context)
def on_change_template(self, cr, uid, ids, template_id, context=None):
if context is None:
context = {}
vals = {}
resource_id = context.get('active_id', False)
if template_id and resource_id:
vals.update(self.get_template_data(cr, uid, resource_id, template_id, context=context))
else:
vals.update({'attachment_ids' : []})
return {'value': vals}
return {'value': self.get_template_data(cr, uid, template_id, context=context)}
email_compose_message()

View File

@ -101,6 +101,7 @@ class email_compose_message(osv.osv_memory):
_columns = {
'attachment_ids': fields.many2many('ir.attachment','email_message_send_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
'auto_delete': fields.boolean('Auto Delete', help="Permanently delete emails after sending"),
'filter_id': fields.many2one('ir.filters', 'Filters'),
}
def get_value(self, cr, uid, model, res_id, context=None):
@ -153,64 +154,65 @@ class email_compose_message(osv.osv_memory):
if context is None:
context = {}
record = self.browse(cr, uid, ids[0], context=context)
if context.get('mass_mail') and context['active_ids'] and context.get('template_id'):
email_message_pool = self.pool.get('email.message')
email_temp_pool = self.pool.get('email.template')
for res_id in context['active_ids']:
subject = email_temp_pool.get_template_value(cr, uid, record.subject, context['active_model'], res_id)
body = email_temp_pool.get_template_value(cr, uid, record.body, context['active_model'], res_id)
email_to = email_temp_pool.get_template_value(cr, uid, record.email_to, context['active_model'], res_id)
email_from = email_temp_pool.get_template_value(cr, uid, record.email_from, context['active_model'], res_id)
email_cc = email_temp_pool.get_template_value(cr, uid, record.email_cc, context['active_model'], res_id)
reply_to = email_temp_pool.get_template_value(cr, uid, record.reply_to, context['active_model'], res_id)
email_id = email_message_pool.schedule_with_attach(cr, uid, email_from,
email_to, subject or False, body or False, context['active_model'], email_cc or False, openobject_id=int(res_id),
context=context)
return {'type': 'ir.actions.act_window_close'}
if context.get('mass_mail') and context.get('active_ids') and not context.get('template_id'):
self.do_mass_mail(cr, uid, context['active_ids'], record.subject or False, record.body or False, context=context)
return {'type': 'ir.actions.act_window_close'}
email_id = self.save_to_mailbox(cr, uid, ids, context)
return {'type': 'ir.actions.act_window_close'}
def do_mass_mail(self, cr, uid, ids, subject, body, context=None):
if context is None:
context = {}
if context.get('active_model'):
email_message_pool = self.pool.get('email.message')
model_pool = self.pool.get(context['active_model'])
for data in model_pool.browse(cr, uid, ids, context=context):
email_id = email_message_pool.schedule_with_attach(cr, uid,
data.user_id and data.user_id.address_id and data.user_id.address_id.email or False,
data.email_from or False, subject, body, model=context['active_model'],
email_cc=tools.ustr(data.email_cc or ''), openobject_id=int(data.id), context=context)
return True
def save_to_mailbox(self, cr, uid, ids, context=None):
email_ids = []
email_message_pool = self.pool.get('email.message')
attachment = []
email_ids = []
for mail in self.browse(cr, uid, ids, context=context):
for attach in mail.attachment_ids:
attachment.append((attach.datas_fname, attach.datas))
references = False
message_id = False
# Reply Email
if context.get('mail',False) == 'reply' and mail.message_id:
references = mail.references and mail.references + "," + mail.message_id or mail.message_id
else:
message_id = mail.message_id
email_id = email_message_pool.schedule_with_attach(cr, uid, mail.email_from, mail.email_to, mail.subject, mail.body,
context.update({'mass_mail': True})
print 'mail contx::', context
# Mass mailing
if context.get('mass_mail', False):
if context['active_ids'] and context['active_model']:
active_ids = context['active_ids']
active_model = context['active_model']
else:
active_model = mail.model
active_model_pool = self.pool.get(active_model)
active_ids = active_model_pool.search(cr, uid, eval(mail.filter_id.domain), context=eval(mail.filter_id.context))
for active_id in active_ids:
subject = self.get_template_value(cr, uid, mail.subject, active_model, active_id)
body = self.get_template_value(cr, uid, mail.body, active_model, active_id)
email_to = self.get_template_value(cr, uid, mail.email_to, active_model, active_id)
email_from = self.get_template_value(cr, uid, mail.email_from, active_model, active_id)
email_cc = self.get_template_value(cr, uid, mail.email_cc, active_model, active_id)
email_bcc = self.get_template_value(cr, uid, mail.email_bcc, active_model, active_id)
reply_to = self.get_template_value(cr, uid, mail.reply_to, active_model, active_id)
email_id = email_message_pool.schedule_with_attach(cr, uid, email_from, email_to, subject, body,
model=mail.model, email_cc=email_cc, email_bcc=email_bcc, reply_to=reply_to,
attach=attachment, message_id=message_id, references=references, openobject_id=int(mail.res_id),
subtype=mail.sub_type, x_headers=mail.headers, priority=mail.priority, smtp_server_id=mail.smtp_server_id and mail.smtp_server_id.id,
auto_delete=mail.auto_delete or False, context=context)
email_ids.append(email_id)
return {'type': 'ir.actions.act_window_close'}
else:
email_id = email_message_pool.schedule_with_attach(cr, uid, mail.email_from, mail.email_to, mail.subject, mail.body,
model=mail.model, email_cc=mail.email_cc, email_bcc=mail.email_bcc, reply_to=mail.reply_to,
attach=attachment, message_id=message_id, references=references, openobject_id=int(mail.res_id),
subtype=mail.sub_type, x_headers=mail.headers, priority=mail.priority, smtp_server_id=mail.smtp_server_id and mail.smtp_server_id.id,
auto_delete=mail.auto_delete or False, context=context)
email_ids.append(email_id)
return email_ids
auto_delete=mail.auto_delete, context=context)
email_ids.append(email_id)
return {'type': 'ir.actions.act_window_close'}
def get_template_value(self, cr, uid, message, model, resource_id, context=None):
return message
email_compose_message()

View File

@ -7,9 +7,10 @@
<field name="model">email.compose.message</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Reply Email">
<form string="Compose Email">
<group col="6" colspan="4">
<field name="model" invisible="1"/>
<field name="model" invisible="context.get('active_model',False)"/>
<field name='filter_id' invisible="context.get('active_model',False)"/>
<field name="smtp_server_id" widget="selection" colspan="4" invisible="1"/>
<field name="email_from" colspan="4" required="1"/>
<field name="email_to" colspan="4" required="1"/>
@ -39,7 +40,7 @@
</record>
<record id="action_email_compose_message_wizard" model="ir.actions.act_window">
<field name="name">Send E-mail</field>
<field name="name">Compose E-mail</field>
<field name="res_model">email.compose.message</field>
<field name="src_model">email.compose.message</field>
<field name="type">ir.actions.act_window</field>