8464e15c01c1cd0b917e028d23d6925ab6aaffb8
[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 L (<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 import gdata.contacts
26
27 class google_base_import(osv.osv_memory):
28     _inherit = 'synchronize.base.contact.wizard.import'
29
30     def _get_tools_name(self, cr, user, context):
31         """
32         @return the list of value of the selection field
33         should be overwritten by subclasses
34         """
35         names = super(google_base_import, self)._get_tools_name(cr, user, context=context)
36         names.append(('gmail','Gmail adress book'))
37         return names
38
39
40     _columns = {
41         'tools':  fields.selection(_get_tools_name, 'App to synchronize with'),
42     }
43
44
45     def _get_actions_dic(self, cr, uid, context=None):
46         """
47             this method should be overwritten in specialize module
48             @return the dictonnaries of action
49         """
50         actions = super(google_base_import, self)._get_actions_dic(cr, uid, context=context)
51
52         data_obj = self.pool.get('ir.model.data')
53         data_id = data_obj._get_id(cr, uid, 'google_base_account', 'view_google_login_form')
54         view_id = False
55         if data_id:
56             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
57
58         value = {
59             'name': _('Import Contact'),
60             'view_type': 'form',
61             'view_mode': 'form,tree',
62             'res_model': 'google.login.contact',
63             'view_id': False,
64             'context': context,
65             'views': [(view_id, 'form')],
66             'type': 'ir.actions.act_window',
67             'target': 'new',
68         }
69         actions['gmail'] = value
70         return actions
71
72 google_base_import()
73
74 class google_contact_import(osv.osv_memory):
75     _inherit = 'google.login'
76     _name = 'google.login.contact'
77
78     def _get_next_action(self, cr, uid, context=None):
79         data_obj = self.pool.get('ir.model.data')
80         data_id = data_obj._get_id(cr, uid, 'sync_google_contact', 'view_synchronize_google_contact_import_form')
81         view_id = False
82         if data_id:
83             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
84         value = {
85             'name': _('Import Contact'),
86             'view_type': 'form',
87             'view_mode': 'form,tree',
88             'res_model': 'synchronize.google.contact.import',
89             'view_id': False,
90             'context': context,
91             'views': [(view_id, 'form')],
92             'type': 'ir.actions.act_window',
93             'target': 'new',
94         }
95         return value
96
97 google_contact_import()
98
99 class synchronize_google_contact(osv.osv_memory):
100     _name = 'synchronize.google.contact.import'
101
102     def _get_group(self, cr, uid, context=None):
103         user_obj = self.pool.get('res.users').browse(cr, uid, uid)
104         google=self.pool.get('google.login')
105         gd_client = google.google_login(cr,uid,user_obj.gmail_user,user_obj.gmail_password)
106         res = []
107         if gd_client:
108             groups = gd_client.GetGroupsFeed()
109             for grp in groups.entry:
110                 res.append((grp.id.text, grp.title.text))
111         res.append(('none','None'))
112         res.append(('all','All Groups'))
113         return res
114
115     def _get_default_group(self, cr, uid, context=None):
116         return 'all'
117
118     _columns = {
119         'create_partner': fields.boolean('Create Partner', help="It will create Partner for given gmail user otherwise only adds contacts in Partner Addresses.")  ,
120         'group_name': fields.selection(_get_group, "Group Name", size=32,help="Choose which group to import, By defult it take all "),
121      }
122
123     _defaults = {
124         'group_name': _get_default_group,
125     }
126
127     def create_partner(self, cr, uid, ids, data={}, context=None):
128         partner_obj = self.pool.get('res.partner')
129         name = data.get('name', '')
130         partner_id = partner_obj.search(cr, uid, [('name','ilike',name)], context=context)
131         if not partner_id:
132             partner_id.append(partner_obj.create(cr, uid, {'name': name}, context=context))
133         data.update({'partner_id': partner_id and partner_id[0]})
134         return partner_id, data
135
136     def import_contact(self, cr, uid, ids, context=None):
137         addresss_obj = self.pool.get('res.partner.address')
138         partner_obj = self.pool.get('res.partner')
139
140         user_obj = self.pool.get('res.users').browse(cr, uid, uid)
141
142         gmail_user = user_obj.gmail_user
143         gamil_pwd = user_obj.gmail_password
144
145         google = self.pool.get('google.login')
146         gd_client = google.google_login(cr,uid,user_obj.gmail_user,user_obj.gmail_password)
147
148         if not gmail_user or not gamil_pwd:
149             raise osv.except_osv(_('Error'), _("Please specify the user and password !"))
150
151         contact = gd_client.GetContactsFeed()
152         partner_id = []
153         addresses = []
154         partner_ids = []
155
156         for obj in self.browse(cr, uid, ids, context=context):
157             while contact:
158                 for entry in contact.entry:
159                     data={}
160                     partner_id = False
161                     name = tools.ustr(entry.title.text)
162                     google_id = entry.id.text
163                     phone_numbers = ','.join(phone_number.text for phone_number in entry.phone_number)
164                     emails = ','.join(email.address for email in entry.email)
165                     data['name'] = ''
166                     if name and name != 'None':
167                         data['name'] = name
168
169                     if google_id:
170                         data['google_id'] =google_id
171                     if entry.phone_number:
172                         for phone in entry.phone_number:
173                             if phone.rel == gdata.contacts.REL_WORK:
174                                 data['phone'] = phone.text
175                             if phone.rel == gdata.contacts.PHONE_MOBILE:
176                                 data['mobile'] = phone.text
177                             if phone.rel == gdata.contacts.PHONE_WORK_FAX:
178                                 data['fax'] = phone.text
179
180                     if emails:
181                         data['email'] = emails
182                         contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
183                     else:
184                         contact_ids = addresss_obj.search(cr, uid, [('google_id','=',google_id)])
185                     if  contact_ids :
186                         res={}
187                         addr=addresss_obj.browse(cr,uid,contact_ids)[0]
188                         name = str((addr.name or addr.partner_id and addr.partner_id.name or '').encode('utf-8'))
189                         notes = addr.partner_id and addr.partner_id.comment or ''
190                         email = addr.email or ''
191                         phone = addr.phone or ''
192                         mobile = addr.mobile or ''
193                         fax = addr.fax or ''
194                         if not name:
195                             res['name']=data.get('name','')
196                         if not email:
197                             res['email']=data.get('email','')
198                         if not mobile:
199                             res['mobile']=data.get('mobile','')
200                         if not phone:
201                             res['phone']=data.get('phone','')
202                         if not fax:
203                             res['fax']=data.get('fax','')
204                         addresss_obj.write(cr,uid,contact_ids,res,context=context)
205
206                     if obj.create_partner and obj.group_name == 'all':
207                         if name:
208                             partner_id, data = self.create_partner(cr, uid, ids, data, context=context)
209                             partner_ids.append(partner_id[0])
210                         if not contact_ids  :
211                             addresses.append(addresss_obj.create(cr, uid, data, context=context))
212                         if contact_ids:
213                             addresss_obj.write(cr,uid,contact_ids,{'partner_id':partner_id[0]})
214                     if obj.group_name and entry.group_membership_info and not contact_ids:
215                         for grp in entry.group_membership_info:
216                             if grp.href == obj.group_name:
217                                 if obj.create_partner:
218                                     partner_id, data = self.create_partner(cr, uid, ids, data, context=context)
219                                     partner_ids.append(partner_id[0])
220                                     addresses.append(addresss_obj.create(cr, uid, data, context=context))
221                     else:
222                         if obj.group_name == 'all' and not contact_ids:
223                             addresses.append(addresss_obj.create(cr, uid, data, context=context))
224
225                 if not contact:
226                     break
227                 next = contact.GetNextLink()
228                 contact = None
229                 if next:
230                     contact = gd_client.GetContactsFeed(next.href)
231
232         if partner_ids:
233             return {
234                     'name': _('Partner'),
235                     'domain': "[('id','in', ["+','.join(map(str,partner_ids))+"])]",
236                     'view_type': 'form',
237                     'view_mode': 'tree,form',
238                     'res_model': 'res.partner',
239                     'context': context,
240                     'views': [(False, 'tree'),(False, 'form')],
241                     'type': 'ir.actions.act_window',
242             }
243         elif addresses:
244             return {
245                     'name': _('Contacts'),
246                     'domain': "[('id','in', ["+','.join(map(str,addresses))+"])]",
247                     'view_type': 'form',
248                     'view_mode': 'tree,form',
249                     'res_model': 'res.partner.address',
250                     'context': context,
251                     'views': [(False, 'tree'),(False, 'form')],
252                     'type': 'ir.actions.act_window',
253             }
254         else:
255             return {'type': 'ir.actions.act_window_close'}
256
257 synchronize_google_contact()
258
259 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: