diff options
Diffstat (limited to 'addons/email_template/wizard')
7 files changed, 335 insertions, 344 deletions
diff --git a/addons/email_template/wizard/__init__.py b/addons/email_template/wizard/__init__.py index 1641152490e..b9ac29ec4c2 100644 --- a/addons/email_template/wizard/__init__.py +++ b/addons/email_template/wizard/__init__.py @@ -3,7 +3,7 @@ # # OpenERP, Open Source Management Solution # Copyright (C) 2009 Sharoon Thomas -# Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>) +# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -19,5 +19,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/> # ############################################################################## +import email_template_preview +import mail_compose_message -import email_template_send_wizard diff --git a/addons/email_template/wizard/email_compose_message_view.xml b/addons/email_template/wizard/email_compose_message_view.xml new file mode 100644 index 00000000000..cf233b20682 --- /dev/null +++ b/addons/email_template/wizard/email_compose_message_view.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="utf-8"?> +<openerp> + <data> + + <record model="ir.ui.view" id="email_compose_message_wizard_inherit_form"> + <field name="name">mail.compose.message.form</field> + <field name="model">mail.compose.message</field> + <field name="type">form</field> + <field name="inherit_id" ref="mail.email_compose_message_wizard_form"/> + <field name="arch" type="xml"> + <data> + <xpath expr="//label[@name='placeholder']" position="before"> + <group attrs="{'invisible':[('use_template','=',False)]}" colspan="4" col="4"> + <field name="template_id" colspan="3" + on_change="on_change_template(use_template, template_id, email_from, email_to, context)"/> + <label string="" name="flexspace" colspan="1"/> + </group> + <group colspan="1" col="6"> + <field name="use_template" invisible="1"/> + <button icon="gtk-paste" type="object" name="template_toggle" + string="" help="Use a message template" colspan="1"/> + <button icon="gtk-save" type="object" name="save_as_template" + string="" help="Save as a new template" colspan="1"/> + </group> + </xpath> + </data> + </field> + </record> + </data> +</openerp> diff --git a/addons/email_template/wizard/email_template_preview.py b/addons/email_template/wizard/email_template_preview.py new file mode 100644 index 00000000000..650b714866d --- /dev/null +++ b/addons/email_template/wizard/email_template_preview.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2009 Sharoon Thomas +# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/> +# +############################################################################## + +from osv import osv, fields + +class email_template_preview(osv.osv_memory): + _inherit = "email.template" + _name = "email_template.preview" + _description = "Email Template Preview" + _rec_name = "subject" + + def _get_records(self, cr, uid, context=None): + """ + Return Records of particular Email Template's Model + """ + if context is None: + context = {} + + template_id = context.get('template_id', False) + if not template_id: + return [] + email_template = self.pool.get('email.template') + template = email_template.browse(cr, uid, int(template_id), context=context) + template_object = template.model_id + model = self.pool.get(template_object.model) + record_ids = model.search(cr, uid, [], 0, 10, 'id', context=context) + default_id = context.get('default_res_id') + + if default_id and default_id not in record_ids: + record_ids.insert(0, default_id) + + return model.name_get(cr, uid, record_ids, context) + + + def default_get(self, cr, uid, fields, context=None): + if context is None: + context = {} + result = super(email_template_preview, self).default_get(cr, uid, fields, context=context) + + email_template = self.pool.get('email.template') + template_id = context.get('template_id') + if 'res_id' in fields and not result.get('res_id'): + records = self._get_records(cr, uid, context=context) + result['res_id'] = records and records[0][0] or False # select first record as a Default + if template_id and 'model_id' in fields and not result.get('model_id'): + result['model_id'] = email_template.read(cr, uid, int(template_id), ['model_id'], context).get('model_id', False) + return result + + _columns = { + 'res_id':fields.selection(_get_records, 'Sample Document'), + } + + def on_change_res_id(self, cr, uid, ids, res_id, context=None): + if not res_id: + return {} + vals = {} + email_template = self.pool.get('email.template') + template_id = context and context.get('template_id') + template = email_template.get_email_template(cr, uid, template_id=template_id, record_id=res_id, context=context) + model = template.model + vals['email_to'] = self.render_template(cr, uid, template.email_to, model, res_id, context) + vals['email_cc'] = self.render_template(cr, uid, template.email_cc, model, res_id, context) + vals['email_bcc'] = self.render_template(cr, uid, template.email_bcc, model, res_id, context) + vals['reply_to'] = self.render_template(cr, uid, template.reply_to, model, res_id, context) + vals['subject'] = self.render_template(cr, uid, template.subject, model, res_id, context) + description = self.render_template(cr, uid, template.body_text, model, res_id, context) or '' + if template.user_signature: + signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature + description += '\n' + signature + vals['body_text'] = description + vals['report_name'] = self.render_template(cr, uid, template.report_name, model, res_id, context) + return {'value': vals} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/email_template/wizard/email_template_preview_view.xml b/addons/email_template/wizard/email_template_preview_view.xml new file mode 100644 index 00000000000..4aab57396ad --- /dev/null +++ b/addons/email_template/wizard/email_template_preview_view.xml @@ -0,0 +1,53 @@ +<?xml version="1.0" encoding="utf-8"?> +<openerp> + <data> + <!-- Email Template Preview --> + <record model="ir.ui.view" id="email_template_preview_form"> + <field name="name">email_template.preview.form</field> + <field name="model">email_template.preview</field> + <field name="type">form</field> + <field name="arch" type="xml"> + <form string="Email Preview"> + <field name="model_id" invisible="1"/> + <field name="res_id" on_change="on_change_res_id(res_id, context)"/> + <group col="2" colspan="4"> + <field name="email_to" readonly="1"/> + <field name="email_cc" readonly="1" attrs="{'invisible': [('email_cc','=',False)]}"/> + <field name="email_bcc" readonly="1" attrs="{'invisible': [('email_bcc','=',False)]}"/> + <field name="reply_to" readonly="1" attrs="{'invisible': [('reply_to','=',False)]}"/> + <field name="subject" readonly="1"/> + </group> + <group col="4" colspan="4"> + <notebook> + <page string="Body (Text)"> + <field name="body_text" nolabel="1" colspan="4" height="350" width="350" readonly="1"/> + </page> + <page string="Body (Rich/HTML)" attrs="{'invisible': [('body_html','=', False)]}"> + <field name="body_html" nolabel="1" colspan="4" height="350" width="350" readonly="1"/> + </page> + </notebook> + </group> + <field name="report_name" colspan="4" readonly="1"/> + <separator colspan="4"/> + <group col="4" colspan="4"> + <label string=""/> + <button icon="gtk-ok" special="cancel" string="Close" colspan="1"/> + </group> + </form> + </field> + </record> + + <record id="wizard_email_template_preview" model="ir.actions.act_window"> + <field name="name">Template Preview</field> + <field name="res_model">email_template.preview</field> + <field name="src_model">email_template.preview</field> + <field name="type">ir.actions.act_window</field> + <field name="view_type">form</field> + <field name="view_mode">form</field> + <field name="auto_refresh" eval="1" /> + <field name="target">new</field> + <field name="context">{'template_id':active_id}</field> + </record> + + </data> +</openerp> diff --git a/addons/email_template/wizard/email_template_send_wizard.py b/addons/email_template/wizard/email_template_send_wizard.py deleted file mode 100644 index 4f6ab7af381..00000000000 --- a/addons/email_template/wizard/email_template_send_wizard.py +++ /dev/null @@ -1,279 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2009 Sharoon Thomas -# Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/> -# -############################################################################## - -from osv import osv, fields -from mako.template import Template -from mako import exceptions -import netsvc -import base64 -from tools.translate import _ -import tools -from email_template.email_template import get_value - - -## FIXME: this wizard duplicates a lot of features of the email template preview, -## one of the 2 should inherit from the other! - -class email_template_send_wizard(osv.osv_memory): - _name = 'email_template.send.wizard' - _description = 'This is the wizard for sending mail' - _rec_name = "subject" - - def _get_accounts(self, cr, uid, context=None): - if context is None: - context = {} - - template = self._get_template(cr, uid, context) - if not template: - return [] - - logger = netsvc.Logger() - - if template.from_account: - return [(template.from_account.id, '%s (%s)' % (template.from_account.name, template.from_account.email_id))] - else: - account_id = self.pool.get('email_template.account').search(cr,uid,[('company','=','no'),('user','=',uid)], context=context) - if account_id: - account = self.pool.get('email_template.account').browse(cr,uid,account_id, context) - return [(r.id,r.name + " (" + r.email_id + ")") for r in account] - else: - logger.notifyChannel(_("email-template"), netsvc.LOG_ERROR, _("No personal email accounts are configured for you. \nEither ask admin to enforce an account for this template or get yourself a personal email account.")) - raise osv.except_osv(_("Missing mail account"),_("No personal email accounts are configured for you. \nEither ask admin to enforce an account for this template or get yourself a personal email account.")) - - def get_value(self, cursor, user, template, message, context=None, id=None): - """Gets the value of the message parsed with the content of object id (or the first 'src_rec_ids' if id is not given)""" - if not message: - return '' - if not id: - id = context['src_rec_ids'][0] - return get_value(cursor, user, id, message, template, context) - - def _get_template(self, cr, uid, context=None): - if context is None: - context = {} - if not 'template' in context and not 'template_id' in context: - return None - if 'template_id' in context.keys(): - template_ids = self.pool.get('email.template').search(cr, uid, [('id','=',context['template_id'])], context=context) - elif 'template' in context.keys(): - # Old versions of email_template used the name of the template. This caused - # problems when the user changed the name of the template, but we keep the code - # for compatibility with those versions. - template_ids = self.pool.get('email.template').search(cr, uid, [('name','=',context['template'])], context=context) - if not template_ids: - return None - - template = self.pool.get('email.template').browse(cr, uid, template_ids[0], context) - - lang = self.get_value(cr, uid, template, template.lang, context) - if lang: - # Use translated template if necessary - ctx = context.copy() - ctx['lang'] = lang - template = self.pool.get('email.template').browse(cr, uid, template.id, ctx) - return template - - def _get_template_value(self, cr, uid, field, context=None): - if context is None: - context = {} - template = self._get_template(cr, uid, context) - if not template: - return False - if len(context['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 self.get_value(cr, uid, template, getattr(template, field), context) - - _columns = { - 'state':fields.selection([ - ('single','Simple Mail Wizard Step 1'), - ('multi','Multiple Mail Wizard Step 1'), - ('done','Wizard Complete') - ],'Status',readonly=True), - 'ref_template':fields.many2one('email.template','Template',readonly=True), - 'rel_model':fields.many2one('ir.model','Model',readonly=True), - 'rel_model_ref':fields.integer('Referred Document',readonly=True), - 'from':fields.selection(_get_accounts,'From Account',select=True), - 'to':fields.char('To',size=250,required=True), - 'cc':fields.char('CC',size=250,), - 'bcc':fields.char('BCC',size=250,), - 'reply_to':fields.char('Reply-To', - size=250, - help="The address recipients should reply to," - " if different from the From address." - " Placeholders can be used here."), - 'message_id':fields.char('Message-ID', - size=250, - help="The Message-ID header value, if you need to" - "specify it, for example to automatically recognize the replies later." - " Placeholders can be used here."), - 'subject':fields.char('Subject',size=200), - 'body_text':fields.text('Body',), - 'body_html':fields.text('Body',), - 'report':fields.char('Report File Name',size=100,), - 'signature':fields.boolean('Attach my signature to mail'), - #'filename':fields.text('File Name'), - 'requested':fields.integer('No of requested Mails',readonly=True), - 'generated':fields.integer('No of generated Mails',readonly=True), - 'full_success':fields.boolean('Complete Success',readonly=True), - 'attachment_ids': fields.many2many('ir.attachment','send_wizard_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'), - } - - #FIXME: probably better by overriding default_get directly - _defaults = { - 'state': lambda self,cr,uid,ctx: len(ctx['src_rec_ids']) > 1 and 'multi' or 'single', - 'rel_model': lambda self,cr,uid,ctx: self.pool.get('ir.model').search(cr,uid,[('model','=',ctx['src_model'])],context=ctx)[0], - 'rel_model_ref': lambda self,cr,uid,ctx: ctx['active_id'], - 'to': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_to', ctx), - 'cc': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_cc', ctx), - 'bcc': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_bcc', ctx), - 'subject':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_subject', ctx), - 'body_text':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_text', ctx), - 'body_html':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_html', ctx), - 'report': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'file_name', ctx), - 'signature': lambda self,cr,uid,ctx: self._get_template(cr, uid, ctx).use_sign, - 'ref_template':lambda self,cr,uid,ctx: self._get_template(cr, uid, ctx).id, - 'reply_to': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'reply_to', ctx), - 'reply_to': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'reply_to', ctx), - 'requested':lambda self,cr,uid,ctx: len(ctx['src_rec_ids']), - 'full_success': False, - 'attachment_ids': [], - } - - def fields_get(self, cr, uid, fields=None, context=None, write_access=True): - if context is None: - context = {} - result = super(email_template_send_wizard, self).fields_get(cr, uid, fields, context, write_access) - if 'attachment_ids' in result and 'src_model' in context: - result['attachment_ids']['domain'] = [('res_model','=',context['src_model']),('res_id','=',context['active_id'])] - return result - - def sav_to_drafts(self, cr, uid, ids, context=None): - if context is None: - context = {} - mailid = self.save_to_mailbox(cr, uid, ids, context=context) - if self.pool.get('email_template.mailbox').write(cr, uid, mailid, {'folder':'drafts'}, context): - return {'type':'ir.actions.act_window_close' } - - def send_mail(self, cr, uid, ids, context=None): - if context is None: - context = {} - mailid = self.save_to_mailbox(cr, uid, ids, context) - if self.pool.get('email_template.mailbox').write(cr, uid, mailid, {'folder':'outbox'}, context): - return {'type':'ir.actions.act_window_close' } - - def get_generated(self, cr, uid, ids=None, context=None): - if ids is None: - ids = [] - if context is None: - context = {} - logger = netsvc.Logger() - if context['src_rec_ids'] and len(context['src_rec_ids'])>1: - #Means there are multiple items selected for email. - mail_ids = self.save_to_mailbox(cr, uid, ids, context) - if mail_ids: - self.pool.get('email_template.mailbox').write(cr, uid, mail_ids, {'folder':'outbox'}, context) - logger.notifyChannel("email-template", netsvc.LOG_INFO, _("Emails for multiple items saved in outbox.")) - self.write(cr, uid, ids, { - 'generated':len(mail_ids), - 'state':'done' - }, context) - else: - raise osv.except_osv(_("Email Template"),_("Email sending failed for one or more objects.")) - return True - - def save_to_mailbox(self, cr, uid, ids, context=None): - def get_end_value(id, value): - if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets value from the template - return self.get_value(cr, uid, template, value, context, id) - else: - return value - - if context is None: - context = {} - mail_ids = [] - template = self._get_template(cr, uid, context) - for id in context['src_rec_ids']: - screen_vals = self.read(cr, uid, ids[0], [],context) - account = self.pool.get('email_template.account').read(cr, uid, screen_vals['from'], context=context) - vals = { - 'email_from': tools.ustr(account['name']) + "<" + tools.ustr(account['email_id']) + ">", - 'email_to': get_end_value(id, screen_vals['to']), - 'email_cc': get_end_value(id, screen_vals['cc']), - 'email_bcc': get_end_value(id, screen_vals['bcc']), - 'subject': get_end_value(id, screen_vals['subject']), - 'body_text': get_end_value(id, screen_vals['body_text']), - 'body_html': get_end_value(id, screen_vals['body_html']), - 'account_id': screen_vals['from'], - 'state':'na', - 'mail_type':'multipart/alternative' #Options:'multipart/mixed','multipart/alternative','text/plain','text/html' - } - if screen_vals['signature']: - signature = self.pool.get('res.users').read(cr, uid, uid, ['signature'], context)['signature'] - if signature: - vals['body_text'] = tools.ustr(vals['body_text'] or '') + signature - vals['body_html'] = tools.ustr(vals['body_html'] or '') + signature - - attachment_ids = [] - - #Create partly the mail and later update attachments - mail_id = self.pool.get('email_template.mailbox').create(cr, uid, vals, context) - mail_ids.append(mail_id) - if template.report_template: - reportname = 'report.' + self.pool.get('ir.actions.report.xml').read(cr, uid, template.report_template.id, ['report_name'], context)['report_name'] - data = {} - data['model'] = self.pool.get('ir.model').browse(cr, uid, screen_vals['rel_model'], context).model - - # Ensure report is rendered using template's language - ctx = context.copy() - if template.lang: - ctx['lang'] = self.get_value(cr, uid, template, template.lang, context, id) - service = netsvc.LocalService(reportname) - (result, format) = service.create(cr, uid, [id], data, ctx) - attachment_id = self.pool.get('ir.attachment').create(cr, uid, { - 'name': _('%s (Email Attachment)') % tools.ustr(vals['subject']), - 'datas': base64.b64encode(result), - 'datas_fname': tools.ustr(get_end_value(id, screen_vals['report']) or _('Report')) + "." + format, - 'description': vals['body_text'] or _("No Description"), - 'res_model': 'email_template.mailbox', - 'res_id': mail_id - }, context) - attachment_ids.append( attachment_id ) - - # Add document attachments - for attachment_id in screen_vals.get('attachment_ids',[]): - new_id = self.pool.get('ir.attachment').copy(cr, uid, attachment_id, { - 'res_model': 'email_template.mailbox', - 'res_id': mail_id, - }, context) - attachment_ids.append( new_id ) - - if attachment_ids: - self.pool.get('email_template.mailbox').write(cr, uid, mail_id, { - 'attachments_ids': [[6, 0, attachment_ids]], - 'mail_type': 'multipart/mixed' - }, context) - - return mail_ids -email_template_send_wizard() - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/email_template/wizard/email_template_send_wizard_view.xml b/addons/email_template/wizard/email_template_send_wizard_view.xml deleted file mode 100644 index a5ad037aee6..00000000000 --- a/addons/email_template/wizard/email_template_send_wizard_view.xml +++ /dev/null @@ -1,63 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<openerp> - <data> - - <record model="ir.ui.view" id="email_template_send_wizard_form"> - <field name="name">email_template.send.wizard.form</field> - <field name="model">email_template.send.wizard</field> - <field name="type">form</field> - <field name="arch" type="xml"> - <form string="Send mail Wizard"> - <group col="4" colspan="4"> - <field name="rel_model" colspan="2" /> - <field name="from" required="1" colspan="2" /> - </group> - <group col="4" colspan="4"> - <group col="6" colspan="4"> - <field name="to" select="1" colspan="4" /> - <newline /> - <field name="cc" select="2" colspan="4" /> - <newline /> - <field name="bcc" select="2" colspan="4" /> - <newline /> - <field name="subject" select="2" colspan="4" attrs="{'required':[('state','=','single')]}" /> - <newline /> - <field name="report" colspan="4" /> - </group> - <separator string="" colspan="4" /> - <notebook colspan="4"> - <page string="Body (Plain Text)"> - <field name="body_text" select="2" colspan="4" nolabel="1" /> - </page> - <page string="Body (HTML)"> - <field name="body_html" select="2" colspan="4" nolabel="1" /> - </page> - <page string="Attachments"> - <label string="Add here all attachments of the current document you want to include in the Email." colspan="4"/> - <field name="attachment_ids" colspan="4" nolabel="1"/> - </page> - </notebook> - <field name="signature" colspan="4" /> - </group> - <group col="4" colspan="4" attrs="{'invisible':[('state','!=','single')]}"> - <button icon="gtk-apply" name="sav_to_drafts" string="Save in Drafts" type="object" /> - <button icon="gtk-ok" name="send_mail" string="Send now" type="object" /> - <button icon="gtk-cancel" special="cancel" string="Discard Mail" /> - </group> - <group col="4" colspan="4" attrs="{'invisible':[('state','=','single')]}"> - <label string="Tip: Multiple emails are sent in the same language (the first one is proposed). We suggest you send emails in groups according to language." colspan="4"/> - <field name="requested" /> - <field name="generated" /> - <button icon="gtk-ok" name="get_generated" string="Send all mails" type="object" states="multi" colspan="2" /> - <button icon="gtk-cancel" special="cancel" string="Discard Mail" colspan="2" states="multi" /> - </group> - <button icon="gtk-ok" special="cancel" string="Close" colspan="4" states="done" /> - <field name="state" /> - <newline /> - <label string="After clicking send all mails, mails will be sent to outbox and cleared in next Send/Recieve" colspan="4"/> - </form> - </field> - </record> - </data> -</openerp> - diff --git a/addons/email_template/wizard/mail_compose_message.py b/addons/email_template/wizard/mail_compose_message.py new file mode 100644 index 00000000000..d40f9541b01 --- /dev/null +++ b/addons/email_template/wizard/mail_compose_message.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/> +# +############################################################################## + +import base64 + +from osv import osv +from osv import fields +from tools.translate import _ +import tools + + +def _reopen(self,res_id,model): + return {'type': 'ir.actions.act_window', + 'view_mode': 'form', + 'view_type': 'form', + 'res_id': res_id, + 'res_model': self._name, + 'target': 'new', + + # save original model in context, otherwise + # it will be lost on the action's context switch + 'mail.compose.target.model': model, + } + +class mail_compose_message(osv.osv_memory): + _inherit = 'mail.compose.message' + + def _get_templates(self, cr, uid, context=None): + """ + Return Email Template of particular Model. + """ + if context is None: + context = {} + record_ids = [] + email_template= self.pool.get('email.template') + model = False + if context.get('message_id'): + mail_message = self.pool.get('mail.message') + message_data = mail_message.browse(cr, uid, int(context.get('message_id')), context) + model = message_data.model + elif context.get('mail.compose.target.model') or context.get('active_model'): + model = context.get('mail.compose.target.model', context.get('active_model')) + if model: + record_ids = email_template.search(cr, uid, [('model', '=', model)]) + return email_template.name_get(cr, uid, record_ids, context) + [(False,'')] + return [] + + _columns = { + 'use_template': fields.boolean('Use Template'), + 'template_id': fields.selection(_get_templates, 'Template'), + } + + def on_change_template(self, cr, uid, ids, use_template, template_id, email_from=None, email_to=None, context=None): + if context is None: + context = {} + values = {} + if template_id: + res_id = context.get('active_id', False) + if context.get('mail.compose.message.mode') == 'mass_mail': + # use the original template values - to be rendered when actually sent + # by super.send_mail() + values = self.pool.get('email.template').read(cr, uid, template_id, self.fields_get_keys(cr, uid), context) + else: + # render the mail as one-shot + values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context) + # retrofit generated attachments in the expected field format + if values['attachments']: + attachment = values.pop('attachments') + attachment_obj = self.pool.get('ir.attachment') + att_ids = [] + for fname, fcontent in attachment.iteritems(): + data_attach = { + 'name': fname, + 'datas': base64.b64encode(fcontent), + 'datas_fname': fname, + 'description': fname, + 'res_model' : self._name, + 'res_id' : ids[0] if ids else False + } + att_ids.append(attachment_obj.create(cr, uid, data_attach)) + values['attachment_ids'] = att_ids + else: + # restore defaults + values = self.default_get(cr, uid, self.fields_get_keys(cr, uid), context) + values.update(use_template=use_template, template_id=template_id) + + return {'value': values} + + + def template_toggle(self, cr, uid, ids, context=None): + for record in self.browse(cr, uid, ids, context=context): + had_template = record.use_template + record.write({'use_template': not(had_template)}) + if had_template: + # equivalent to choosing an empty template + onchange_defaults = self.on_change_template(cr, uid, record.id, not(had_template), + False, email_from=record.email_from, + email_to=record.email_to, context=context) + record.write(onchange_defaults['value']) + return _reopen(self, record.id, record.model) + + def save_as_template(self, cr, uid, ids, context=None): + if context is None: + context = {} + email_template = self.pool.get('email.template') + model_pool = self.pool.get('ir.model') + for record in self.browse(cr, uid, ids, context=context): + model = record.model or context.get('active_model') + model_ids = model_pool.search(cr, uid, [('model', '=', model)]) + model_id = model_ids and model_ids[0] or False + model_name = '' + if model_id: + model_name = model_pool.browse(cr, uid, model_id, context=context).name + template_name = "%s: %s" % (model_name, tools.ustr(record.subject)) + values = { + 'name': template_name, + 'email_from': record.email_from or False, + 'subject': record.subject or False, + 'body_text': record.body_text or False, + 'email_to': record.email_to or False, + 'email_cc': record.email_cc or False, + 'email_bcc': record.email_bcc or False, + 'reply_to': record.reply_to or False, + 'model_id': model_id or False, + 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])] + } + template_id = email_template.create(cr, uid, values, context=context) + record.write({'template_id': template_id, + 'use_template': True}) + + # _reopen same wizard screen with new template preselected + return _reopen(self, record.id, model) + + # override the basic implementation + def render_template(self, cr, uid, template, model, res_id, context=None): + return self.pool.get('email.template').render_template(cr, uid, template, model, res_id, context=context) + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: |
