[IMP] sync_google_contact: Update and link google contact with partner address if...
[odoo/odoo.git] / addons / sync_google_contact / wizard / google_contact_import.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields,osv
23 from tools.translate import _
24 import tools
25 try:
26     import gdata
27     import gdata.contacts.service
28     import gdata.contacts
29     import gdata.contacts.client
30 except ImportError:
31     raise osv.except_osv(_('Google Contacts Import Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
32
33 class google_base_import(osv.osv_memory):
34     _inherit = 'synchronize.base.contact.wizard.import'
35
36     def _get_tools_name(self, cr, user, context):
37         """
38         @return the list of value of the selection field
39         should be overwritten by subclasses
40         """
41         names = super(google_base_import, self)._get_tools_name(cr, user, context=context)
42         names.append(('gmail','Gmail address book'))
43         return names
44
45
46     _columns = {
47         'tools':  fields.selection(_get_tools_name, 'App to synchronize with'),
48     }
49
50
51     def _get_actions_dic(self, cr, uid, context=None):
52         """
53             this method should be overwritten in specialize module
54             @return the dictonnaries of action
55         """
56         actions = super(google_base_import, self)._get_actions_dic(cr, uid, context=context)
57
58         data_obj = self.pool.get('ir.model.data')
59         data_id = data_obj._get_id(cr, uid, 'google_base_account', 'view_google_login_form')
60         view_id = False
61         if data_id:
62             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
63
64         value = {
65             'name': _('Import Contact'),
66             'view_type': 'form',
67             'view_mode': 'form,tree',
68             'res_model': 'google.login.contact',
69             'view_id': False,
70             'context': context,
71             'views': [(view_id, 'form')],
72             'type': 'ir.actions.act_window',
73             'target': 'new',
74         }
75         actions['gmail'] = value
76         return actions
77
78 google_base_import()
79
80 class google_contact_import(osv.osv_memory):
81     _inherit = 'google.login'
82     _name = 'google.login.contact'
83
84     def _get_next_action(self, cr, uid, context=None):
85         data_obj = self.pool.get('ir.model.data')
86         data_id = data_obj._get_id(cr, uid, 'sync_google_contact', 'view_synchronize_google_contact_import_form')
87         view_id = False
88         if data_id:
89             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
90         value = {
91             'name': _('Import Contact'),
92             'view_type': 'form',
93             'view_mode': 'form,tree',
94             'res_model': 'synchronize.google.contact.import',
95             'view_id': False,
96             'context': context,
97             'views': [(view_id, 'form')],
98             'type': 'ir.actions.act_window',
99             'target': 'new',
100         }
101         return value
102
103 google_contact_import()
104
105 class synchronize_google_contact(osv.osv_memory):
106     _name = 'synchronize.google.contact.import'
107
108     def _get_group(self, cr, uid, context=None):
109         user_obj = self.pool.get('res.users').browse(cr, uid, uid)
110         google=self.pool.get('google.login')
111         gd_client = google.google_login(cr,uid,user_obj.gmail_user,user_obj.gmail_password)
112         res = []
113         if gd_client:
114             groups = gd_client.GetGroupsFeed()
115             for grp in groups.entry:
116                 res.append((grp.id.text, grp.title.text))
117         res.append(('none','None'))
118         res.append(('all','All Groups'))
119         return res
120
121     def _get_default_group(self, cr, uid, context=None):
122         return 'all'
123
124     _columns = {
125         'create_partner': fields.boolean('Create Partner', help="It will create Partner for given gmail user otherwise only adds contacts in Partner Addresses.")  ,
126         'group_name': fields.selection(_get_group, "Group Name", size=32,help="Choose which group to import, By defult it take all "),
127      }
128
129     _defaults = {
130         'group_name': _get_default_group,
131     }
132
133     def create_partner(self, cr, uid, data={}, context=None):
134         partner_obj = self.pool.get('res.partner')
135         name = data.get('name','')
136         partner_id = partner_obj.search(cr, uid, [('name','ilike',name)], context=context)
137         if not partner_id:
138             partner_id.append(partner_obj.create(cr, uid, {'name': name, 'address' : [(6, 0, [data['address_id']])]}, context=context))
139         return partner_id, data
140
141     def import_contact(self, cr, uid, ids, context=None):
142         obj=self.browse(cr, uid, ids, context=context)[0]
143         if obj.group_name == 'none':
144             return { 'type': 'ir.actions.act_window_close' }
145
146         user_obj = self.pool.get('res.users').browse(cr, uid, uid)
147
148         gmail_user = user_obj.gmail_user
149         gamil_pwd = user_obj.gmail_password
150
151         google = self.pool.get('google.login')
152         gd_client = google.google_login(cr,uid,user_obj.gmail_user,user_obj.gmail_password)
153
154         if not gmail_user or not gamil_pwd:
155             raise osv.except_osv(_('Error'), _("Please specify the user and password !"))
156
157         if obj.group_name not in ['all','none']:
158             query = gdata.contacts.service.ContactsQuery()
159             query.group =obj.group_name
160             contact = gd_client.GetContactsFeed(query.ToUri())
161         else:
162             contact = gd_client.GetContactsFeed()
163
164         ids = self.create_contact( cr, uid, gd_client,contact, partner_id=obj.create_partner,context=context)
165         if not ids:
166             return {'type': 'ir.actions.act_window_close'}
167
168         return {
169                 'name': _('Partner'),
170                 'domain': "[('id','in', ["+','.join(map(str,ids))+"])]",
171                 'view_type': 'form',
172                 'view_mode': 'tree,form',
173                 'res_model': obj.create_partner and 'res.partner' or 'res.partner.address',
174                 'context': context,
175                 'views': [(False, 'tree'),(False, 'form')],
176                 'type': 'ir.actions.act_window',
177         }
178
179
180     def create_contact(self, cr, uid, gd_client,contact, partner_id=False,context=None):
181         model_obj = self.pool.get('ir.model.data')
182         addresss_obj = self.pool.get('res.partner.address')
183         addresses = []
184         partner_ids = []
185         contact_ids=[]
186         while contact:
187             for entry in contact.entry:
188                 data = {}
189                 model_data = {
190                     'name': 'google_contacts_information_%s' %(entry.id.text),
191                     'model': 'res.partner.address',
192                     'module': 'sync_google_contact',
193                 }
194                 name = tools.ustr(entry.title.text)
195                 if name == "None":
196                     name = entry.email[0].address
197
198                 google_id = entry.id.text
199                 emails = ','.join(email.address for email in entry.email)
200                 if name and name != 'None':
201                     data['name'] = name
202
203                 if google_id:
204                     model_data.update({'google_id': google_id})
205                 if entry.phone_number:
206                     for phone in entry.phone_number:
207                         if phone.rel == gdata.contacts.REL_WORK:
208                             data['phone'] = phone.text
209                         if phone.rel == gdata.contacts.PHONE_MOBILE:
210                             data['mobile'] = phone.text
211                         if phone.rel == gdata.contacts.PHONE_WORK_FAX:
212                             data['fax'] = phone.text
213
214                 data_ids = model_obj.search(cr, uid, [('google_id','=',google_id)])
215                 if data_ids:
216                     contact_ids = [model_obj.browse(cr, uid, data_ids[0], context=context).res_id]
217                 elif emails:
218                     data['email'] = emails
219                     contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
220
221                 if contact_ids:
222                     addresses.append(contact_ids[0])
223                     self.update_contact(cr, uid, contact_ids, data, context=context)
224                     data_ids = model_obj.search(cr, uid, [('res_id','=',contact_ids[0]), ('google_id','=','')])
225                     model_data.update({'google_id': google_id})
226                     model_obj.write(cr, uid, data_ids, model_data, context=context)
227                 if not contact_ids:
228                     #create or link to an existing partner only if it's a new contact
229                     res_id = addresss_obj.create(cr, uid, data, context=context)
230                     data['address_id'] = res_id
231                     if partner_id:
232                         partner_id, data = self.create_partner(cr, uid, data, context=context)
233                         partner_ids.append(partner_id[0])
234                     addresses.append(res_id)
235                     model_data.update({'res_id': res_id})
236                     model_obj.create(cr, uid, model_data, context=context)
237
238             next = contact.GetNextLink()
239             contact = next and gd_client.GetContactsFeed(next.href) or None
240
241         if partner_id:
242             return partner_ids
243         else:
244             return addresses
245
246     def update_contact(self, cr, uid, contact_ids, data,context=None):
247         addresss_obj = self.pool.get('res.partner.address')
248         if context==None:
249             context={}
250         res = {}
251         addr = addresss_obj.browse(cr,uid,contact_ids)[0]
252         name = str((addr.name or addr.partner_id and addr.partner_id.name or '').encode('utf-8'))
253         addres=addr.partner_id
254         email = addr.email
255         phone = addr.phone
256         mobile = addr.mobile
257         fax = addr.fax
258         if not name:
259             res['name']=data.get('name','')
260         if not email:
261             res['email']=data.get('email','')
262         if not mobile:
263             res['mobile']=data.get('mobile','')
264         if not phone:
265             res['phone']=data.get('phone','')
266         if not fax:
267             res['fax']=data.get('fax','')
268         if data.get('partner_id') and not addres :
269             res['partner_id'] = data.get('partner_id')
270         addresss_obj.write(cr,uid,contact_ids,res,context=context)
271         return {}
272
273 synchronize_google_contact()
274
275 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: