~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to zarafa/sednazarafa.py

  • Committer: Fabien Pinckaers
  • Date: 2009-02-06 10:25:35 UTC
  • mfrom: (3547.1.3 trunk-extra-addons)
  • Revision ID: fp@tinyerp.com-20090206102535-1eykxxd9d8uwb8cv
Adding Sednacom Zarafa Module

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 StringIO
 
11
import urllib2
 
12
 
 
13
_ZARAFA_TINYERP_MAP = {
 
14
    'zid' : 'zarafa_id' ,
 
15
    'name' : 'name' ,
 
16
    'email' : 'email' ,
 
17
    'company' : 'partner_id' ,
 
18
    'phone' : 'phone' ,
 
19
    'mobile' : 'mobile' ,
 
20
    'fax' : 'fax' ,
 
21
}
 
22
 
 
23
_MARK_START = 'contact start'
 
24
_MARK_END = 'contact end'
 
25
 
 
26
_ZARAFA_CONTACT_URL = 'http://%(zarafa_server)s/webaccess/' \
 
27
        'contact.php?user=%(zarafa_user)s' \
 
28
        '&pwd=%(zarafa_password)s'
 
29
 
 
30
class res_partner_address(osv.osv):
 
31
    """Address, with new data for zarafa"""
 
32
    _name = 'res.partner.address'
 
33
    _inherit = 'res.partner.address'
 
34
    _description = __doc__
 
35
 
 
36
    _columns = {
 
37
        'zarafa_id' : fields.char('Z-Id', size=128),
 
38
    }
 
39
 
 
40
    def unlink(self, cr, uid, rpa_ids, context={}):
 
41
        zids = ', '.join(["'%s'" % val["zarafa_id"] for val in \
 
42
                    self.read(cr, uid, rpa_ids, ["zarafa_id",]) \
 
43
                        if val["zarafa_id"] ])
 
44
        res = super(res_partner_address, self).unlink(cr, uid, rpa_ids, context)
 
45
        sql = "delete from zarafa_contact where zid in (%s)" % zids
 
46
        cr.execute(sql)
 
47
        return res
 
48
 
 
49
res_partner_address()
 
50
 
 
51
class zarafa_contact(osv.osv):
 
52
    """Contacts, with features to import from Zarafa server"""
 
53
    _name = 'zarafa.contact'
 
54
    _description = __doc__
 
55
 
 
56
    _columns = {
 
57
        'name' : fields.char('Name', size=128, required=True) ,
 
58
        'email' : fields.char('Email', size=128, required=True) ,
 
59
        'phone' : fields.char('Phone', size=128) ,
 
60
        'mobile' : fields.char('Mobile', size=128) ,
 
61
        'fax' : fields.char('Fax', size=128) ,
 
62
        'company' : fields.char('Company', size=128) ,
 
63
        'zid' : fields.char('Z-Id', size=128, required=True) ,
 
64
        'state' : fields.selection(
 
65
            [   ('new','New'),
 
66
                ('update','Update'), ] ,
 
67
            'State', readonly=True,) ,
 
68
    }
 
69
 
 
70
    def _import(self, cr, uid, context={}):
 
71
        data = self._get_zarafa_contacts(cr, uid, context)
 
72
        res = self._proc_data(cr, uid, data, context)
 
73
        self._do_rpa(cr, uid, self.search(cr, uid, []))
 
74
        return True
 
75
 
 
76
    def _get_zarafa_contacts(self, cr, uid, context={}):
 
77
 
 
78
        o_ru = self.pool.get('res.users')
 
79
        zud = o_ru.read(cr, uid, [uid,], ['zarafa_server', 'zarafa_user', 'zarafa_password'])[0]
 
80
 
 
81
        zurl = _ZARAFA_CONTACT_URL % zud
 
82
        ures = urllib2.urlopen(zurl)
 
83
 
 
84
        return ures.read()
 
85
 
 
86
    def _proc_data(self, cr, uid, data, context={}):
 
87
        sio = StringIO.StringIO(data)
 
88
 
 
89
        res = []
 
90
        while True:
 
91
            try:
 
92
                cur_val = sio.next().strip()
 
93
            except StopIteration:
 
94
                break
 
95
            if cur_val == 'contact start':
 
96
                vals = {}
 
97
            elif cur_val == 'contact end':
 
98
                res.append(vals)
 
99
            else:
 
100
                if not cur_val:
 
101
                    continue
 
102
                fn, fv = cur_val.split(':')
 
103
                vals[fn.strip()] = fv.strip()
 
104
                if fn == 'zid':
 
105
                    zids = self.search(cr, uid, [('zid','=',fv),])
 
106
                    if zids:
 
107
                        vals['state'] = 'update:%s' % zids[0]
 
108
                    else:
 
109
                        vals['state'] = 'new'
 
110
 
 
111
        for val in res:
 
112
            if val['state'] == 'new':
 
113
                self.create(cr, uid, val)
 
114
            else:
 
115
                state = val['state']
 
116
                val['state'],zid = state.split(':')
 
117
                self.write(cr, uid, int(zid), val)
 
118
 
 
119
        return res
 
120
 
 
121
    def _do_rpa(self, cr, uid, zcids, context={}):
 
122
        data = self.read(cr, uid, zcids, ['id', 'state', ])
 
123
        to_create = []
 
124
        to_update = []
 
125
        for row in data:
 
126
            if row['state'] == 'new':
 
127
                to_create.append(row['id'])
 
128
            else:
 
129
                to_update.append(row['id'])
 
130
 
 
131
        self._create_contact(cr, uid, to_create, context)
 
132
        self._update_contact(cr, uid, to_update, context)
 
133
 
 
134
        return True
 
135
 
 
136
    def _get_partner(self, cr, uid, name, context={}):
 
137
        o_rp =  self.pool.get('res.partner')
 
138
        rp_ids = o_rp.search(cr, uid, [('name','=',name),])
 
139
        if rp_ids:
 
140
            rp_id = rp_ids[-1]
 
141
        else:
 
142
            rp_id = o_rp.create(cr, uid, {'name' : name})
 
143
        return rp_id
 
144
 
 
145
    def _create_contact(self, cr, uid, zcids, context={}):
 
146
        o_rpa = self.pool.get('res.partner.address')
 
147
        o_rp =  self.pool.get('res.partner')
 
148
        res = list()
 
149
        for data in self.browse(cr, uid, zcids, context):
 
150
            part_id = self._get_partner(cr, uid, data.company or data.name, context)
 
151
            vals = {
 
152
                'name' : data.name ,
 
153
                'email' : data.email ,
 
154
                'phone' : data.phone ,
 
155
                'mobile' : data.mobile ,
 
156
                'fax' : data.fax ,
 
157
                'zarafa_id' : data.zid ,
 
158
                'partner_id' : part_id ,
 
159
            }
 
160
            o_rpa.create(cr, uid, vals, context)
 
161
            res.append(data.id)
 
162
        self.write(cr, uid, res, {'state' : 'update'})
 
163
        return res
 
164
 
 
165
    def _update_contact(self, cr, uid, zcids, context={}):
 
166
        o_rpa = self.pool.get('res.partner.address')
 
167
        res = list()
 
168
        for data in self.browse(cr, uid, zcids, context):
 
169
            try:
 
170
                rpa_id = o_rpa.search(cr, uid, [('zarafa_id','=',data.zid),])[0]
 
171
            except IndexError:
 
172
                return self._create_contact(cr, uid, [data.id, ], context)
 
173
            vals = {
 
174
                'name' : data.name ,
 
175
                'email' : data.email ,
 
176
                'phone' : data.phone ,
 
177
                'mobile' : data.mobile ,
 
178
                'fax' : data.fax ,
 
179
            }
 
180
            o_rpa.write(cr, uid, rpa_id, vals)
 
181
            res.append(data.id)
 
182
        return res
 
183
 
 
184
 
 
185
zarafa_contact()
 
186
 
 
187
 
 
188
class res_users(osv.osv):
 
189
    """Users with Zarafa connection"""
 
190
    _name = 'res.users'
 
191
    _inherit = 'res.users'
 
192
 
 
193
    _columns = {
 
194
        'zarafa_server' : fields.char("Zarafa server", size=128) ,
 
195
        'zarafa_user' : fields.char("Zarafa user", size=64) ,
 
196
        'zarafa_password' : fields.char("Zarafa password", size=64, invisible=True) ,
 
197
        'zarafa_email' : fields.char("Zarafa email", size=64) ,
 
198
    }
 
199
 
 
200
res_users()