~serpent-consulting-services/openerp-usa/shipping_api_6-1

« back to all changes in this revision

Viewing changes to email_template_history/email_template_mailbox.py

  • Committer: npgllc
  • Date: 2012-08-02 17:13:27 UTC
  • Revision ID: npgllc-20120802171327-2xgyyjjb5d1kx26y
Removed all the 6.0 compatible modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 NovaPoint Group LLC (<http://www.novapointgroup.com>)
6
 
#    Copyright (C) 2004-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 time
24
 
import base64
25
 
 
26
 
import tools
27
 
import netsvc
28
 
from tools.translate import _
29
 
from osv import osv, fields
30
 
 
31
 
LOGGER = netsvc.Logger()
32
 
 
33
 
class email_template_mailbox(osv.osv):
34
 
    _inherit = "email_template.mailbox"
35
 
    """
36
 
    Inherit send_this_mail function to add email on partner history after sending email. 
37
 
    """
38
 
    def send_this_mail(self, cr, uid, ids=None, context=None):
39
 
        result = True
40
 
        attachment_pool = self.pool.get('ir.attachment')
41
 
        for id in (ids or []):
42
 
            try:
43
 
                account_obj = self.pool.get('email_template.account')
44
 
                values = self.read(cr, uid, id, [], context=context)
45
 
                attach_to_send = None
46
 
    
47
 
                if values['attachments_ids']:
48
 
                    attach_to_send = self.pool.get('ir.attachment').read(cr, uid, values['attachments_ids'], ['datas_fname', 'datas', 'name'],
49
 
                                                                         context=context)
50
 
                    attach_to_send = map(lambda x: (x['datas_fname'] or x['name'], base64.decodestring(x['datas'])), attach_to_send)
51
 
    
52
 
                if values.get('body_html'):
53
 
                    body = values.get('body_html')
54
 
                    subtype = "html"
55
 
                else:
56
 
                    body = values.get('body_text')
57
 
                    subtype = "plain"
58
 
                try:
59
 
                    result = tools.email_send(
60
 
                        values.get('email_from') or u'',
61
 
                        [values.get('email_to')],
62
 
                        values['subject'] or u'',
63
 
                        body or u'',
64
 
                        reply_to=values.get('reply_to') or u'',
65
 
                        email_bcc=[values.get('email_bcc') or u''],
66
 
                        email_cc=[values.get('email_cc') or u''],
67
 
                        subtype=subtype,
68
 
                        attach=attach_to_send,
69
 
                        openobject_id=values['message_id']
70
 
                        )
71
 
                except:
72
 
                    pass
73
 
    
74
 
                if result:
75
 
                    account = account_obj.browse(cr, uid, values['account_id'][0], context=context)
76
 
                    send_model_name = values.get('rel_model', False)
77
 
                    send_model_id = values.get('rel_model_ref', False)
78
 
                    template_id = values.get('ref_template', False)
79
 
                    if send_model_name:
80
 
                        send_model_name = self.pool.get('ir.model').browse(cr, uid, send_model_name[0], context=context).model
81
 
                    ## Code to add sent mail details in partner history
82
 
                    try:
83
 
                        if template_id:
84
 
                            historize = self.pool.get('email.template').read(cr, uid, template_id[0], ['add_to_history'],
85
 
                                                                             context=context)['add_to_history']
86
 
                        if send_model_name and send_model_id and historize:
87
 
                            if send_model_name in ['account.voucher', 'sale.order', 'crm.lead', 'calendar.attendee',
88
 
                                                   'crm.phonecall', 'purchase.order', 'account.invoice', 'account.voucher']:
89
 
                                send_model_pool = self.pool.get(send_model_name)
90
 
                                if send_model_pool:
91
 
                                    send_model = send_model_pool.browse(cr, uid, send_model_id, context=context)
92
 
                                    if send_model and send_model.partner_id and send_model.partner_id.id:
93
 
                                        self.pool.get('mailgate.message').create(cr, uid, {
94
 
                                            'partner_id': send_model.partner_id.id,
95
 
                                            'name': values['subject'] or u'',
96
 
                                            'model': send_model_name,
97
 
                                            'res_id': send_model_id,
98
 
                                            'date': time.strftime('%Y-%m-%d %H:%M:%S'),
99
 
                                            'history': True,
100
 
                                            'user_id': uid,
101
 
                                            'message': body or u'',
102
 
                                            'email_from': values.get('email_from') or u'',
103
 
                                            'email_to': values.get('email_to'),
104
 
                                            'email_cc': values.get('email_cc') or u'',
105
 
                                            'email_bcc': values.get('email_bcc') or u'',
106
 
                                            'attachment_ids': [[6, 0, values['attachments_ids']]],
107
 
                                            'description': body,
108
 
                                            }, context=context)
109
 
                    except (Exception), e:
110
 
                        logger = netsvc.Logger()
111
 
                        msg = "Adding email sent history in partner: " + str(e)
112
 
                        logger.notifyChannel("email_template_modification", netsvc.LOG_ERROR, msg)
113
 
                    if account.auto_delete:
114
 
                        self.write(cr, uid, id, {'folder': 'trash', 'state': 'sent', 'date_mail': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
115
 
                        self.unlink(cr, uid, [id], context=context)
116
 
                        # Remove attachments for this mail
117
 
                        attachment_pool.unlink(cr, uid, values['attachments_ids'], context=context)
118
 
                    else:
119
 
                        self.write(cr, uid, id, {'folder': 'sent', 'state': 'sent', 'date_mail': time.strftime("%Y-%m-%d %H:%M:%S")}, context=context)
120
 
                        self.historise(cr, uid, [id], "Email sent successfully", context=context)
121
 
                else:
122
 
                    self.write(cr, uid, id, {'state': 'exception', 'folder': 'outbox'}, context=context)
123
 
                    try:
124
 
                        error = result['error_msg'] 
125
 
                    except:
126
 
                        error = 'Unable to send mail. Please check SMTP is configured properly.'
127
 
                    self.historise(cr, uid, [id], error, context=context)
128
 
 
129
 
            except Exception, error:
130
 
                logger = netsvc.Logger()
131
 
                logger.notifyChannel("email-template", netsvc.LOG_ERROR, _("Sending of Mail %s failed. Probable Reason:Could not login to server\n \
132
 
                                                                            error: %s") % (id, error))
133
 
                self.historise(cr, uid, [id], error, context=context)
134
 
        return result
135
 
 
136
 
    _columns = {
137
 
        'rel_model': fields.many2one('ir.model', 'Model', readonly=True),
138
 
        'rel_model_ref': fields.integer('Referred Document', readonly=True),
139
 
        'ref_template': fields.many2one('email.template', 'Template', readonly=True),
140
 
        'state': fields.selection([
141
 
            ('sending', 'Sending'),
142
 
            ('draft', 'Draft'),
143
 
            ('sending','Sending'),
144
 
            ('exception', 'Exception'),
145
 
            ('sent', 'Sent'),
146
 
            ], 'Status', required=True),
147
 
        }
148
 
    _defaults = {
149
 
        'state': 'draft',
150
 
        'folder': 'drafts',
151
 
        }
152
 
 
153
 
email_template_mailbox()
154
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'