1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 L (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields,osv
23 from tools.translate import _
27 class google_base_import(osv.osv_memory):
28 _inherit = 'synchronize.base.contact.wizard.import'
30 def _get_tools_name(self, cr, user, context):
32 @return the list of value of the selection field
33 should be overwritten by subclasses
35 names = super(google_base_import, self)._get_tools_name(cr, user, context=context)
36 names.append(('gmail','Gmail adress book'))
41 'tools': fields.selection(_get_tools_name, 'App to synchronize with'),
45 def _get_actions_dic(self, cr, uid, context=None):
47 this method should be overwritten in specialize module
48 @return the dictonnaries of action
50 actions = super(google_base_import, self)._get_actions_dic(cr, uid, context=context)
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')
56 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
59 'name': _('Import Contact'),
61 'view_mode': 'form,tree',
62 'res_model': 'google.login.contact',
65 'views': [(view_id, 'form')],
66 'type': 'ir.actions.act_window',
69 actions['gmail'] = value
74 class google_contact_import(osv.osv_memory):
75 _inherit = 'google.login'
76 _name = 'google.login.contact'
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')
83 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
85 'name': _('Import Contact'),
87 'view_mode': 'form,tree',
88 'res_model': 'synchronize.google.contact.import',
91 'views': [(view_id, 'form')],
92 'type': 'ir.actions.act_window',
97 google_contact_import()
99 class synchronize_google_contact(osv.osv_memory):
100 _name = 'synchronize.google.contact.import'
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)
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'))
115 def _get_default_group(self, cr, uid, context=None):
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 "),
124 'group_name': _get_default_group,
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)
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
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')
140 user_obj = self.pool.get('res.users').browse(cr, uid, uid)
142 gmail_user = user_obj.gmail_user
143 gamil_pwd = user_obj.gmail_password
145 google = self.pool.get('google.login')
146 gd_client = google.google_login(cr,uid,user_obj.gmail_user,user_obj.gmail_password)
148 if not gmail_user or not gamil_pwd:
149 raise osv.except_osv(_('Error'), _("Please specify the user and password !"))
151 contact = gd_client.GetContactsFeed()
156 for obj in self.browse(cr, uid, ids, context=context):
158 for entry in contact.entry:
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)
166 if name and name != 'None':
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
181 data['email'] = emails
182 contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
184 contact_ids = addresss_obj.search(cr, uid, [('google_id','=',google_id)])
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 ''
195 res['name']=data.get('name','')
197 res['email']=data.get('email','')
199 res['mobile']=data.get('mobile','')
201 res['phone']=data.get('phone','')
203 res['fax']=data.get('fax','')
204 addresss_obj.write(cr,uid,contact_ids,res,context=context)
206 if obj.create_partner and obj.group_name == 'all':
208 partner_id, data = self.create_partner(cr, uid, ids, data, context=context)
209 partner_ids.append(partner_id[0])
211 addresses.append(addresss_obj.create(cr, uid, data, context=context))
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))
222 if obj.group_name == 'all' and not contact_ids:
223 addresses.append(addresss_obj.create(cr, uid, data, context=context))
227 next = contact.GetNextLink()
230 contact = gd_client.GetContactsFeed(next.href)
234 'name': _('Partner'),
235 'domain': "[('id','in', ["+','.join(map(str,partner_ids))+"])]",
237 'view_mode': 'tree,form',
238 'res_model': 'res.partner',
240 'views': [(False, 'tree'),(False, 'form')],
241 'type': 'ir.actions.act_window',
245 'name': _('Contacts'),
246 'domain': "[('id','in', ["+','.join(map(str,addresses))+"])]",
248 'view_mode': 'tree,form',
249 'res_model': 'res.partner.address',
251 'views': [(False, 'tree'),(False, 'form')],
252 'type': 'ir.actions.act_window',
255 return {'type': 'ir.actions.act_window_close'}
257 synchronize_google_contact()
259 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: