~openerp-commiter/openobject-addons/stable-sja-branch

« back to all changes in this revision

Viewing changes to zarafa/sednaemail.py

  • Committer: sja-axelor
  • Date: 2009-10-13 09:52:57 UTC
  • Revision ID: suniljagyasi@gmail.com-20091013095257-8u26ww0r20z9y6ey
add

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#coding: utf-8
 
3
#
 
4
# (c) 2008 Sednacom <http://www.sednacom.fr>
 
5
#
 
6
# authors :
 
7
#  - Brice V. < brice@sednacom.fr >
 
8
 
 
9
from osv import osv, fields
 
10
import time
 
11
import htmlmail
 
12
import base64
 
13
 
 
14
def _default_from(*args):
 
15
    obj = args[0]
 
16
    cr = args[1]
 
17
    user = args[2]
 
18
    context= args[3]
 
19
    return obj.pool.get('res.users').read(cr, user, [user, ], ['zarafa_email', ])[0]['zarafa_email']
 
20
 
 
21
def _default_name(*args):
 
22
    context= args[3]
 
23
    name = context.get('case_name', '')
 
24
    return name
 
25
 
 
26
def _default_recipients(*args):
 
27
    context = args[3]
 
28
    recipients = context.get('case_recipients', False)
 
29
    return recipients
 
30
 
 
31
class email(osv.osv):
 
32
    """Sednacom email"""
 
33
    _name = 'sednacom.email'
 
34
 
 
35
    def _make_to(self, cr, uid, eids, name, arg, context={}):
 
36
        res = {}
 
37
        for data in self.browse(cr, uid, eids, context):
 
38
            res[data.id] = ', '.join([ contact.email for contact in data.recipients if contact.email])
 
39
        return res
 
40
 
 
41
    _columns = {
 
42
        'name' : fields.char('Title', size=64, required=True, readonly=True,
 
43
                    states={'draft' : [('readonly', False),]}) ,
 
44
        'body' : fields.text('Message', readonly=True,
 
45
                    states={'draft' : [('readonly', False),]}) ,
 
46
        'to' : fields.function(_make_to, method=True, string='To', type='char', size=256) ,
 
47
        'recipients' : fields.many2many('res.partner.address', 'sednacom_email_recipients', 'email', 'contact', 'Contacts',
 
48
                    readonly=True, states={'draft' : [('readonly', False),]}) ,
 
49
        'exp' : fields.char('From', size=128, required=True, readonly=True,
 
50
                    states={'draft' : [('readonly', False),]}) ,
 
51
        'datetime' : fields.datetime('Date', readonly=True,
 
52
                    states={'draft' : [('readonly', False),]}) ,
 
53
        'state' : fields.selection(
 
54
            [   ('draft','Draft'),
 
55
                ('sent','Sent'),
 
56
                ('received','Received'), ] ,
 
57
            'State', readonly=True,) ,
 
58
        'crm_case' : fields.many2one('crm.case', 'Case',
 
59
                readonly=True, states={'draft' : [('readonly', False),]}) ,
 
60
    }
 
61
 
 
62
    _defaults = {
 
63
        'datetime' : lambda *args: time.strftime('%Y-%m-%d %H:%M:%S') ,
 
64
        'exp' : _default_from ,
 
65
        'state' : lambda *args: 'draft' ,
 
66
        'name' : _default_name ,
 
67
        'recipients' : _default_recipients ,
 
68
    }
 
69
 
 
70
    _order = "datetime desc"
 
71
 
 
72
    def add_crm_log(self, cr, uid, eids, context={}):
 
73
        o_cch = self.pool.get('crm.case.history')
 
74
 
 
75
        data = self.read(cr, uid, eids, ['id', 'crm_case', 'body', 'exp', ])
 
76
 
 
77
        canal = context.get('canal_id', False)
 
78
        name = context.get('history_name', 'Email')
 
79
        res = True
 
80
        for row in data:
 
81
            if row['crm_case']:
 
82
                case_id = row['crm_case'][0]
 
83
                vals = {
 
84
                    'name' : name ,
 
85
                    'canal_id' : canal ,
 
86
                    'case_id' : case_id ,
 
87
                    'description' : row['body'] ,
 
88
                    'email' : row['exp'] ,
 
89
                }
 
90
                res = o_cch.create(cr, uid, vals)
 
91
            else:
 
92
                continue
 
93
        return res
 
94
 
 
95
 
 
96
    def send(self, cr, uid, eids, context={}):
 
97
        if not eids:
 
98
            return True
 
99
        datas = self.browse(cr, uid , eids, context)
 
100
 
 
101
        o_ia = self.pool.get('ir.attachment')
 
102
        b64d = base64.decodestring
 
103
 
 
104
        for data in datas:
 
105
            ia_ids = o_ia.search(cr, uid,
 
106
                [('res_model','=','sednacom.email'),('res_id','=',data.id),] )
 
107
            ia_data  = list()
 
108
            if ia_ids:
 
109
                ia_raw_data = o_ia.read(cr, uid, ia_ids, ['datas_fname', 'datas',])
 
110
                ia_data = [ ( d['datas_fname'],  b64d(d['datas']) ) \
 
111
                                    for d in ia_raw_data ]
 
112
 
 
113
            htmlmail.email_html_send_attach(
 
114
                data.exp ,
 
115
                [a.strip() for a in str(data.to).split(',')] ,
 
116
                data.name ,
 
117
                data.body ,
 
118
                False,
 
119
                False,
 
120
                False,
 
121
                False,
 
122
                ia_data,
 
123
                False
 
124
            )
 
125
 
 
126
        self.write(cr, uid, eids, {'state' : 'sent'})
 
127
        self.add_crm_log(cr, uid, eids, context)
 
128
        return True
 
129
 
 
130
    def zarafa_send(self, cr, uid, eids, context={}):
 
131
        if not eids:
 
132
            return True
 
133
        datas = self.browse(cr, uid , eids, context)
 
134
 
 
135
        o_ia = self.pool.get('ir.attachment')
 
136
        b64d = base64.decodestring
 
137
 
 
138
        for data in datas:
 
139
            ia_ids = o_ia.search(cr, uid,
 
140
                [('res_model','=','sednacom.email'),('res_id','=',data.id),] )
 
141
            ia_data  = list()
 
142
            if ia_ids:
 
143
                ia_raw_data = o_ia.read(cr, uid, ia_ids, ['datas_fname', 'datas',])
 
144
                ia_data = [ ( d['datas_fname'],  b64d(d['datas']) ) \
 
145
                                    for d in ia_raw_data ]
 
146
            htmlmail.zarafa_email_html_send_attach(
 
147
                self, cr, uid,
 
148
                data.exp ,
 
149
                [a.strip() for a in str(data.to).split(',')] ,
 
150
                data.name ,
 
151
                data.body ,
 
152
                False,
 
153
                False,
 
154
                False,
 
155
                False,
 
156
                ia_data,
 
157
                False
 
158
            )
 
159
 
 
160
        self.write(cr, uid, eids, {'state' : 'sent'})
 
161
        self.add_crm_log(cr, uid, eids, context)
 
162
        return True
 
163
 
 
164
email()