[REF] consistent, grepable ir.values object naming.
[odoo/odoo.git] / addons / email_template / email_template.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2009 Sharoon Thomas
6 #    Copyright (C) 2010-2010 OpenERP SA (<http://www.openerp.com>)
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
20 #
21 ##############################################################################
22
23 import base64
24 import random
25 import netsvc
26 import logging
27 import re
28
29 TEMPLATE_ENGINES = []
30
31 from osv import osv, fields
32 from tools.translate import _
33
34 try:
35     from mako.template import Template as MakoTemplate
36     TEMPLATE_ENGINES.append(('mako', 'Mako Templates'))
37 except ImportError:
38     logging.getLogger('init').warning("module email_template: Mako templates not installed")
39
40 try:
41     from django.template import Context, Template as DjangoTemplate
42     #Workaround for bug:
43     #http://code.google.com/p/django-tagging/issues/detail?id=110
44     from django.conf import settings
45     settings.configure()
46     #Workaround ends
47     TEMPLATE_ENGINES.append(('django', 'Django Template'))
48 except ImportError:
49     logging.getLogger('init').warning("module email_template: Django templates not installed")
50
51 import tools
52 import pooler
53 import logging
54
55 def get_value(cursor, user, recid, message=None, template=None, context=None):
56     """
57     Evaluates an expression and returns its value
58     @param cursor: Database Cursor
59     @param user: ID of current user
60     @param recid: ID of the target record under evaluation
61     @param message: The expression to be evaluated
62     @param template: BrowseRecord object of the current template
63     @param context: OpenERP Context
64     @return: Computed message (unicode) or u""
65     """
66     pool = pooler.get_pool(cursor.dbname)
67     if message is None:
68         message = {}
69     #Returns the computed expression
70     if message:
71         try:
72             message = tools.ustr(message)
73             object = pool.get(template.model_int_name).browse(cursor, user, recid, context=context)
74             env = {
75                 'user':pool.get('res.users').browse(cursor, user, user, context=context),
76                 'db':cursor.dbname
77                    }
78             if template.template_language == 'mako':
79                 templ = MakoTemplate(message, input_encoding='utf-8')
80                 reply = MakoTemplate(message).render_unicode(object=object,
81                                                              peobject=object,
82                                                              env=env,
83                                                              format_exceptions=True)
84             elif template.template_language == 'django':
85                 templ = DjangoTemplate(message)
86                 env['object'] = object
87                 env['peobject'] = object
88                 reply = templ.render(Context(env))
89             return reply or False
90         except Exception:
91             logging.exception("can't render %r", message)
92             return u""
93     else:
94         return message
95
96 class email_template(osv.osv):
97     "Templates for sending Email"
98
99     _name = "email.template"
100     _description = 'Email Templates for Models'
101
102     def change_model(self, cursor, user, ids, object_name, context=None):
103         if object_name:
104             mod_name = self.pool.get('ir.model').read(
105                                               cursor,
106                                               user,
107                                               object_name,
108                                               ['model'], context)['model']
109         else:
110             mod_name = False
111         return {
112                 'value':{'model_int_name':mod_name}
113                 }
114
115     _columns = {
116         'name' : fields.char('Name', size=100, required=True),
117         'object_name':fields.many2one('ir.model', 'Resource'),
118         'model_int_name':fields.char('Model Internal Name', size=200,),
119         'from_account':fields.many2one(
120                    'email_template.account',
121                    string="Email Account",
122                    help="Emails will be sent from this approved account."),
123         'def_to':fields.char(
124                  'Recipient (To)',
125                  size=250,
126                  help="The Recipient of email. "
127                  "Placeholders can be used here. "
128                  "e.g. ${object.email_to}"),
129         'def_cc':fields.char(
130                  'CC',
131                  size=250,
132                  help="Carbon Copy address(es), comma-separated."
133                     " Placeholders can be used here. "
134                     "e.g. ${object.email_cc}"),
135         'def_bcc':fields.char(
136                   'BCC',
137                   size=250,
138                   help="Blind Carbon Copy address(es), comma-separated."
139                     " Placeholders can be used here. "
140                     "e.g. ${object.email_bcc}"),
141         'reply_to':fields.char('Reply-To',
142                     size=250,
143                     help="The address recipients should reply to,"
144                     " if different from the From address."
145                     " Placeholders can be used here. "
146                     "e.g. ${object.email_reply_to}"),
147         'message_id':fields.char('Message-ID',
148                     size=250,
149                     help="Specify the Message-ID SMTP header to use in outgoing emails. Please note that this overrides the Resource tracking option! Placeholders can be used here."),
150         'track_campaign_item':fields.boolean('Resource Tracking',
151                                 help="Enable this is you wish to include a special \
152 tracking marker in outgoing emails so you can identify replies and link \
153 them back to the corresponding resource record. \
154 This is useful for CRM leads for example"),
155         'lang':fields.char(
156                    'Language',
157                    size=250,
158                    help="The default language for the email."
159                    " Placeholders can be used here. "
160                    "eg. ${object.partner_id.lang}"),
161         'def_subject':fields.char(
162                   'Subject',
163                   size=200,
164                   help="The subject of email."
165                   " Placeholders can be used here.",
166                   translate=True),
167         'def_body_text':fields.text(
168                     'Standard Body (Text)',
169                     help="The text version of the mail",
170                     translate=True),
171         'def_body_html':fields.text(
172                     'Body (Text-Web Client Only)',
173                     help="The text version of the mail",
174                     translate=True),
175         'use_sign':fields.boolean(
176                   'Signature',
177                   help="the signature from the User details"
178                   " will be appended to the mail"),
179         'file_name':fields.char(
180                 'Report Filename',
181                 size=200,
182                 help="Name of the generated report file. Placeholders can be used in the filename. eg: 2009_SO003.pdf",
183                 translate=True),
184         'report_template':fields.many2one(
185                   'ir.actions.report.xml',
186                   'Report to send'),
187         'attachment_ids': fields.many2many(
188                     'ir.attachment',
189                     'email_template_attachment_rel',
190                     'email_template_id',
191                     'attachment_id',
192                     'Attached Files',
193                     help="You may attach existing files to this template, "
194                          "so they will be added in all emails created from this template"),
195         'ref_ir_act_window':fields.many2one(
196                     'ir.actions.act_window',
197                     'Window Action',
198                     help="Action that will open this email template on Resource records",
199                     readonly=True),
200         'ref_ir_value':fields.many2one(
201                    'ir.values',
202                    'Wizard Button',
203                    help="Button in the side bar of the form view of this Resource that will invoke the Window Action",
204                    readonly=True),
205         'allowed_groups':fields.many2many(
206                   'res.groups',
207                   'template_group_rel',
208                   'templ_id', 'group_id',
209                   string="Allowed User Groups",
210                   help="Only users from these groups will be"
211                   " allowed to send mails from this Template"),
212         'model_object_field':fields.many2one(
213                  'ir.model.fields',
214                  string="Field",
215                  help="Select the field from the model you want to use."
216                  "\nIf it is a relationship field you will be able to "
217                  "choose the nested values in the box below\n(Note:If "
218                  "there are no values make sure you have selected the"
219                  " correct model)",
220                  store=False),
221         'sub_object':fields.many2one(
222                  'ir.model',
223                  'Sub-model',
224                  help='When a relation field is used this field'
225                  ' will show you the type of field you have selected',
226                  store=False),
227         'sub_model_object_field':fields.many2one(
228                  'ir.model.fields',
229                  'Sub Field',
230                  help="When you choose relationship fields "
231                  "this field will specify the sub value you can use.",
232                  store=False),
233         'null_value':fields.char(
234                  'Null Value',
235                  help="This Value is used if the field is empty",
236                  size=50, store=False),
237         'copyvalue':fields.char(
238                 'Expression',
239                 size=100,
240                 help="Copy and paste the value in the "
241                 "location you want to use a system value.",
242                 store=False),
243         'table_html':fields.text(
244              'HTML code',
245              help="Copy this html code to your HTML message"
246              " body for displaying the info in your mail.",
247              store=False),
248         #Template language(engine eg.Mako) specifics
249         'template_language':fields.selection(
250                 TEMPLATE_ENGINES,
251                 'Templating Language',
252                 required=True
253                 )
254     }
255
256     _defaults = {
257         'template_language' : lambda *a:'mako',
258
259     }
260
261     _sql_constraints = [
262         ('name', 'unique (name)','The template name must be unique !')
263     ]
264
265     def create_action(self, cr, uid, ids, context=None):
266         vals = {}
267         if context is None:
268             context = {}
269         template_obj = self.browse(cr, uid, ids, context=context)[0]
270         src_obj = template_obj.object_name.model
271         vals['ref_ir_act_window'] = self.pool.get('ir.actions.act_window').create(cr, uid, {
272              'name': template_obj.name,
273              'type': 'ir.actions.act_window',
274              'res_model': 'email_template.send.wizard',
275              'src_model': src_obj,
276              'view_type': 'form',
277              'context': "{'src_model':'%s','template_id':'%d','src_rec_id':active_id,'src_rec_ids':active_ids}" % (src_obj, template_obj.id),
278              'view_mode':'form,tree',
279              'view_id': self.pool.get('ir.ui.view').search(cr, uid, [('name', '=', 'email_template.send.wizard.form')], context=context)[0],
280              'target': 'new',
281              'auto_refresh':1
282         }, context)
283         ir_values_obj = self.pool.get('ir.values')
284         vals['ref_ir_value'] = ir_values_obj.create(cr, uid, {
285              'name': _('Send Mail (%s)') % template_obj.name,
286              'model': src_obj,
287              'key2': 'client_action_multi',
288              'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
289              'object': True,
290          }, context)
291         self.write(cr, uid, ids, {
292             'ref_ir_act_window': vals['ref_ir_act_window'],
293             'ref_ir_value': vals['ref_ir_value'],
294         }, context)
295         return True
296
297     def unlink_action(self, cr, uid, ids, context=None):
298         for template in self.browse(cr, uid, ids, context=context):
299             try:
300                 if template.ref_ir_act_window:
301                     self.pool.get('ir.actions.act_window').unlink(cr, uid, template.ref_ir_act_window.id, context)
302                 if template.ref_ir_value:
303                     ir_values_obj = self.pool.get('ir.values')
304                     ir_values_obj.unlink(cr, uid, template.ref_ir_value.id, context)
305             except:
306                 raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
307
308     def delete_action(self, cr, uid, ids, context=None):
309         self.unlink_action(cr, uid, ids, context=context)
310         return True
311
312     def unlink(self, cr, uid, ids, context=None):
313         self.unlink_action(cr, uid, ids, context=context)
314         return super(email_template, self).unlink(cr, uid, ids, context=context)
315
316     def copy(self, cr, uid, id, default=None, context=None):
317         if default is None:
318             default = {}
319         default = default.copy()
320         old = self.read(cr, uid, id, ['name'], context=context)
321         new_name = _("Copy of template %s") % old.get('name', 'No Name')
322         check = self.search(cr, uid, [('name', '=', new_name)], context=context)
323         if check:
324             new_name = new_name + '_' + random.choice('abcdefghij') + random.choice('lmnopqrs') + random.choice('tuvwzyz')
325         default.update({'name':new_name})
326         return super(email_template, self).copy(cr, uid, id, default, context)
327
328     def build_expression(self, field_name, sub_field_name, null_value, template_language='mako'):
329         """
330         Returns a template expression based on data provided
331         @param field_name: field name
332         @param sub_field_name: sub field name (M2O)
333         @param null_value: default value if the target value is empty
334         @param template_language: name of template engine
335         @return: computed expression
336         """
337
338         expression = ''
339         if template_language == 'mako':
340             if field_name:
341                 expression = "${object." + field_name
342                 if sub_field_name:
343                     expression += "." + sub_field_name
344                 if null_value:
345                     expression += " or '''%s'''" % null_value
346                 expression += "}"
347         elif template_language == 'django':
348             if field_name:
349                 expression = "{{object." + field_name
350                 if sub_field_name:
351                     expression += "." + sub_field_name
352                 if null_value:
353                     expression += "|default: '''%s'''" % null_value
354                 expression += "}}"
355         return expression
356
357     def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
358         if not model_object_field:
359             return {}
360         result = {}
361         field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
362         #Check if field is relational
363         if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
364             res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
365             if res_ids:
366                 result['sub_object'] = res_ids[0]
367                 result['copyvalue'] = self.build_expression(False,
368                                                       False,
369                                                       False,
370                                                       template_language)
371                 result['sub_model_object_field'] = False
372                 result['null_value'] = False
373         else:
374             #Its a simple field... just compute placeholder
375             result['sub_object'] = False
376             result['copyvalue'] = self.build_expression(field_obj.name,
377                                                   False,
378                                                   False,
379                                                   template_language
380                                                   )
381             result['sub_model_object_field'] = False
382             result['null_value'] = False
383         return {'value':result}
384
385     def onchange_sub_model_object_field(self, cr, uid, ids, model_object_field, sub_model_object_field, template_language, context=None):
386         if not model_object_field or not sub_model_object_field:
387             return {}
388         result = {}
389         field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
390         if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
391             res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
392             sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
393             if res_ids:
394                 result['sub_object'] = res_ids[0]
395                 result['copyvalue'] = self.build_expression(field_obj.name,
396                                                       sub_field_obj.name,
397                                                       False,
398                                                       template_language
399                                                       )
400                 result['sub_model_object_field'] = sub_model_object_field
401                 result['null_value'] = False
402         else:
403             #Its a simple field... just compute placeholder
404             result['sub_object'] = False
405             result['copyvalue'] = self.build_expression(field_obj.name,
406                                                   False,
407                                                   False,
408                                                   template_language
409                                                   )
410             result['sub_model_object_field'] = False
411             result['null_value'] = False
412         return {'value':result}
413
414     def onchange_null_value(self, cr, uid, ids, model_object_field, sub_model_object_field, null_value, template_language, context=None):
415         if not model_object_field and not null_value:
416             return {}
417         result = {}
418         field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
419         if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
420             res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
421             sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
422             if res_ids:
423                 result['sub_object'] = res_ids[0]
424                 result['copyvalue'] = self.build_expression(field_obj.name,
425                                                       sub_field_obj.name,
426                                                       null_value,
427                                                       template_language
428                                                       )
429                 result['sub_model_object_field'] = sub_model_object_field
430                 result['null_value'] = null_value
431         else:
432             #Its a simple field... just compute placeholder
433             result['sub_object'] = False
434             result['copyvalue'] = self.build_expression(field_obj.name,
435                                                   False,
436                                                   null_value,
437                                                   template_language
438                                                   )
439             result['sub_model_object_field'] = False
440             result['null_value'] = null_value
441         return {'value':result}
442
443     def _add_attachment(self, cursor, user, mailbox_id, name, data, filename, context=None):
444         """
445         Add an attachment to a given mailbox entry.
446
447         :param data: base64 encoded attachment data to store
448         """
449         attachment_obj = self.pool.get('ir.attachment')
450         attachment_data = {
451             'name':  (name or '') + _(' (Email Attachment)'),
452             'datas': data,
453             'datas_fname': filename,
454             'description': name or _('No Description'),
455             'res_model':'email_template.mailbox',
456             'res_id': mailbox_id,
457         }
458         attachment_id = attachment_obj.create(cursor,
459                                               user,
460                                               attachment_data,
461                                               context)
462         if attachment_id:
463             self.pool.get('email_template.mailbox').write(
464                               cursor,
465                               user,
466                               mailbox_id,
467                               {
468                                'attachments_ids':[(4, attachment_id)],
469                                'mail_type':'multipart/mixed'
470                               },
471                               context)
472
473     def generate_attach_reports(self,
474                                  cursor,
475                                  user,
476                                  template,
477                                  record_id,
478                                  mail,
479                                  context=None):
480         """
481         Generate report to be attached and attach it
482         to the email, and add any directly attached files as well.
483
484         @param cursor: Database Cursor
485         @param user: ID of User
486         @param template: Browse record of
487                          template
488         @param record_id: ID of the target model
489                           for which this mail has
490                           to be generated
491         @param mail: Browse record of email object
492         @return: True
493         """
494         if template.report_template:
495             reportname = 'report.' + \
496                 self.pool.get('ir.actions.report.xml').read(
497                                              cursor,
498                                              user,
499                                              template.report_template.id,
500                                              ['report_name'],
501                                              context)['report_name']
502             service = netsvc.LocalService(reportname)
503             data = {}
504             data['model'] = template.model_int_name
505             (result, format) = service.create(cursor,
506                                               user,
507                                               [record_id],
508                                               data,
509                                               context)
510             fname = tools.ustr(get_value(cursor, user, record_id,
511                                          template.file_name, template, context)
512                                or 'Report')
513             ext = '.' + format
514             if not fname.endswith(ext):
515                 fname += ext
516             self._add_attachment(cursor, user, mail.id, mail.subject, base64.b64encode(result), fname, context)
517
518         if template.attachment_ids:
519             for attachment in template.attachment_ids:
520                 self._add_attachment(cursor, user, mail.id, attachment.name, attachment.datas, attachment.datas_fname, context)
521
522         return True
523
524     def _generate_mailbox_item_from_template(self,
525                                       cursor,
526                                       user,
527                                       template,
528                                       record_id,
529                                       context=None):
530         """
531         Generates an email from the template for
532         record record_id of target object
533
534         @param cursor: Database Cursor
535         @param user: ID of User
536         @param template: Browse record of
537                          template
538         @param record_id: ID of the target model
539                           for which this mail has
540                           to be generated
541         @return: ID of created object
542         """
543         if context is None:
544             context = {}
545         #If account to send from is in context select it, else use enforced account
546         if 'account_id' in context.keys():
547             from_account = self.pool.get('email_template.account').read(
548                                                     cursor,
549                                                     user,
550                                                     context.get('account_id'),
551                                                     ['name', 'email_id'],
552                                                     context
553                                                     )
554         else:
555             from_account = {
556                             'id':template.from_account.id,
557                             'name':template.from_account.name,
558                             'email_id':template.from_account.email_id
559                             }
560         lang = get_value(cursor,
561                          user,
562                          record_id,
563                          template.lang,
564                          template,
565                          context)
566         if lang:
567             ctx = context.copy()
568             ctx.update({'lang':lang})
569             template = self.browse(cursor, user, template.id, context=ctx)
570
571         # determine name of sender, either it is specified in email_id or we
572         # use the account name
573         email_id = from_account['email_id'].strip()
574         email_from = re.findall(r'([^ ,<@]+@[^> ,]+)', email_id)[0]
575         if email_from != email_id:
576             # we should keep it all, name is probably specified in the address
577             email_from = from_account['email_id']
578         else:
579             email_from = tools.ustr(from_account['name']) + "<" + tools.ustr(email_id) + ">"
580
581         # FIXME: should do this in a loop and rename template fields to the corresponding
582         # mailbox fields. (makes no sense to have different names I think.
583         mailbox_values = {
584             'email_from': email_from,
585             'email_to':get_value(cursor,
586                                user,
587                                record_id,
588                                template.def_to,
589                                template,
590                                context),
591             'email_cc':get_value(cursor,
592                                user,
593                                record_id,
594                                template.def_cc,
595                                template,
596                                context),
597             'email_bcc':get_value(cursor,
598                                 user,
599                                 record_id,
600                                 template.def_bcc,
601                                 template,
602                                 context),
603             'reply_to':get_value(cursor,
604                                 user,
605                                 record_id,
606                                 template.reply_to,
607                                 template,
608                                 context),
609             'subject':get_value(cursor,
610                                     user,
611                                     record_id,
612                                     template.def_subject,
613                                     template,
614                                     context),
615             'body_text':get_value(cursor,
616                                       user,
617                                       record_id,
618                                       template.def_body_text,
619                                       template,
620                                       context),
621             'body_html':get_value(cursor,
622                                       user,
623                                       record_id,
624                                       template.def_body_html,
625                                       template,
626                                       context),
627             'account_id' :from_account['id'],
628             #This is a mandatory field when automatic emails are sent
629             'state':'na',
630             'folder':'drafts',
631             'mail_type':'multipart/alternative',
632         }
633
634         if template['message_id']:
635             # use provided message_id with placeholders
636             mailbox_values.update({'message_id': get_value(cursor, user, record_id, template['message_id'], template, context)})
637
638         elif template['track_campaign_item']:
639             # get appropriate message-id
640             mailbox_values.update({'message_id': tools.misc.generate_tracking_message_id(record_id)})
641
642         if not mailbox_values['account_id']:
643             raise Exception("Unable to send the mail. No account linked to the template.")
644         #Use signatures if allowed
645         if template.use_sign:
646             sign = self.pool.get('res.users').read(cursor,
647                                                    user,
648                                                    user,
649                                                    ['signature'],
650                                                    context)['signature']
651             if mailbox_values['body_text']:
652                 mailbox_values['body_text'] += sign
653             if mailbox_values['body_html']:
654                 mailbox_values['body_html'] += sign
655         mailbox_id = self.pool.get('email_template.mailbox').create(
656                                                              cursor,
657                                                              user,
658                                                              mailbox_values,
659                                                              context)
660
661         return mailbox_id
662
663
664     def generate_mail(self,
665                       cursor,
666                       user,
667                       template_id,
668                       record_ids,
669                       context=None):
670         if context is None:
671             context = {}
672         template = self.browse(cursor, user, template_id, context=context)
673         if not template:
674             raise Exception("The requested template could not be loaded")
675         result = True
676         mailbox_obj = self.pool.get('email_template.mailbox')
677         for record_id in record_ids:
678             mailbox_id = self._generate_mailbox_item_from_template(
679                                                                 cursor,
680                                                                 user,
681                                                                 template,
682                                                                 record_id,
683                                                                 context)
684             mail = mailbox_obj.browse(
685                                         cursor,
686                                         user,
687                                         mailbox_id,
688                                         context=context
689                                               )
690             if template.report_template or template.attachment_ids:
691                 self.generate_attach_reports(
692                                               cursor,
693                                               user,
694                                               template,
695                                               record_id,
696                                               mail,
697                                               context
698                                               )
699
700             self.pool.get('email_template.mailbox').write(
701                                                 cursor,
702                                                 user,
703                                                 mailbox_id,
704                                                 {'folder':'outbox'},
705                                                 context=context
706             )
707             # TODO : manage return value of all the records
708             result = self.pool.get('email_template.mailbox').send_this_mail(cursor, user, [mailbox_id], context)
709         return result
710
711 email_template()
712
713
714 ## FIXME: this class duplicates a lot of features of the email template send wizard,
715 ##        one of the 2 should inherit from the other!
716
717 class email_template_preview(osv.osv_memory):
718     _name = "email_template.preview"
719     _description = "Email Template Preview"
720
721     def _get_model_recs(self, cr, uid, context=None):
722         if context is None:
723             context = {}
724             #Fills up the selection box which allows records from the selected object to be displayed
725         self.context = context
726         if 'template_id' in context:
727             ref_obj_id = self.pool.get('email.template').read(cr, uid, context['template_id'], ['object_name'], context)
728             ref_obj_name = self.pool.get('ir.model').read(cr, uid, ref_obj_id['object_name'][0], ['model'], context)['model']
729             model_obj = self.pool.get(ref_obj_name)
730             ref_obj_ids = model_obj.search(cr, uid, [], 0, 20, 'id', context=context)
731             if not ref_obj_ids:
732                 ref_obj_ids = []
733
734             # also add the default one if requested, otherwise it won't be available for selection:
735             default_id = context.get('default_rel_model_ref')
736             if default_id and default_id not in ref_obj_ids:
737                 ref_obj_ids.insert(0, default_id)
738             return model_obj.name_get(cr, uid, ref_obj_ids, context)
739         return []
740
741     def default_get(self, cr, uid, fields, context=None):
742         if context is None:
743             context = {}
744         result = super(email_template_preview, self).default_get(cr, uid, fields, context=context)
745         if (not fields or 'rel_model_ref' in fields) and 'template_id' in context \
746            and not result.get('rel_model_ref'):
747             selectables = self._get_model_recs(cr, uid, context=context)
748             result['rel_model_ref'] = selectables and selectables[0][0] or False
749         return result
750
751     def _default_model(self, cursor, user, context=None):
752         """
753         Returns the default value for model field
754         @param cursor: Database Cursor
755         @param user: ID of current user
756         @param context: OpenERP Context
757         """
758         return self.pool.get('email.template').read(
759                                                    cursor,
760                                                    user,
761                                                    context['template_id'],
762                                                    ['object_name'],
763                                                    context).get('object_name', False)
764
765     _columns = {
766         'ref_template':fields.many2one(
767                                        'email.template',
768                                        'Template', readonly=True),
769         'rel_model':fields.many2one('ir.model', 'Model', readonly=True),
770         'rel_model_ref':fields.selection(_get_model_recs, 'Referred Document'),
771         'to':fields.char('To', size=250, readonly=True),
772         'cc':fields.char('CC', size=250, readonly=True),
773         'bcc':fields.char('BCC', size=250, readonly=True),
774         'reply_to':fields.char('Reply-To',
775                     size=250,
776                     help="The address recipients should reply to,"
777                          " if different from the From address."
778                          " Placeholders can be used here."),
779         'message_id':fields.char('Message-ID',
780                     size=250,
781                     help="The Message-ID header value, if you need to"
782                          "specify it, for example to automatically recognize the replies later."
783                         " Placeholders can be used here."),
784         'subject':fields.char('Subject', size=200, readonly=True),
785         'body_text':fields.text('Body', readonly=True),
786         'body_html':fields.text('Body', readonly=True),
787         'report':fields.char('Report Name', size=100, readonly=True),
788     }
789     _defaults = {
790         'ref_template': lambda self, cr, uid, ctx:ctx['template_id'] or False,
791         'rel_model': _default_model,
792     }
793     def on_change_ref(self, cr, uid, ids, rel_model_ref, context=None):
794         if context is None:
795             context = {}
796         if not rel_model_ref:
797             return {}
798         vals = {}
799         if context == {}:
800             context = self.context
801         template = self.pool.get('email.template').browse(cr, uid, context['template_id'], context)
802         #Search translated template
803         lang = get_value(cr, uid, rel_model_ref, template.lang, template, context)
804         if lang:
805             ctx = context.copy()
806             ctx.update({'lang':lang})
807             template = self.pool.get('email.template').browse(cr, uid, context['template_id'], ctx)
808         vals['to'] = get_value(cr, uid, rel_model_ref, template.def_to, template, context)
809         vals['cc'] = get_value(cr, uid, rel_model_ref, template.def_cc, template, context)
810         vals['bcc'] = get_value(cr, uid, rel_model_ref, template.def_bcc, template, context)
811         vals['reply_to'] = get_value(cr, uid, rel_model_ref, template.reply_to, template, context)
812         if template.message_id:
813             vals['message_id'] = get_value(cr, uid, rel_model_ref, template.message_id, template, context)
814         elif template.track_campaign_item:
815             vals['message_id'] = tools.misc.generate_tracking_message_id(rel_model_ref)
816         vals['subject'] = get_value(cr, uid, rel_model_ref, template.def_subject, template, context)
817         vals['body_text'] = get_value(cr, uid, rel_model_ref, template.def_body_text, template, context)
818         vals['body_html'] = get_value(cr, uid, rel_model_ref, template.def_body_html, template, context)
819         vals['report'] = get_value(cr, uid, rel_model_ref, template.file_name, template, context)
820         return {'value':vals}
821
822 email_template_preview()
823
824 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: