1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<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 _
25 from sync_google_contact import sync_google_contact
27 class google_contact_import(osv.osv_memory):
28 _name = "synchronize.base"
29 _inherit = 'synchronize.base'
31 'tools': fields.selection([('gmail','Gmail')], 'Tools'),
32 'create_partner': fields.selection([('group','Group'),('gmail_user','Gmail user')], 'Create Partner'),
35 def import_contact(self, cr, uid, ids, context=None):
36 # Only see the result, we will change the code
38 addresss_obj = self.pool.get('res.partner.address')
39 partner_obj = self.pool.get('res.partner')
40 user_obj = self.pool.get('res.users').browse(cr, uid, uid)
41 gmail_user = user_obj.gmail_user
42 gamil_pwd = user_obj.gmail_password
43 if not gmail_user or not gamil_pwd:
44 raise osv.except_osv(_('Error'), _("Please specify the user and password !"))
46 google_obj = sync_google_contact.google_lib(gmail_user, gamil_pwd)
47 contact = google_obj._get_contact()
52 for obj in self.browse(cr, uid, ids, context=context):
53 if obj.create_partner == 'group':
54 groups = google_obj._get_contact_allGroups()
55 for grp in groups.entry:
56 partner_name.append(grp.title.text)
57 group_links[grp.title.text] = grp.id.text
58 elif obj.create_partner == 'gmail_user':
59 google_obj = sync_google_contact.google_lib(gmail_user, gamil_pwd)
60 contact = google_obj._get_contact()
61 for user in contact.author:
62 partner_name.append(user.name.text)
64 # Creating partner for selected option.
65 for name in partner_name:
66 partner_id = partner_obj.search(cr, uid, [('name','ilike',name)], context=context)
68 partner_id.append(partner_obj.create(cr, uid, {'name': name}, context=context))
69 contact = google_obj._get_contact()
71 link = group_links.get(name)
73 for entry in contact.entry:
74 google_id = entry.id.text
75 contact_name = entry.title.text
76 phone_numbers = ','.join(phone_number.text for phone_number in entry.phone_number)
77 emails = ','.join(email.address for email in entry.email)
80 'phone': phone_numbers,
82 'google_id': google_id,
83 'partner_id': partner_id and partner_id[0]
85 if entry.group_membership_info and link:
86 for grp in entry.group_membership_info:
88 addresss_obj.create(cr, uid, data, context=context)
90 if obj.create_partner == 'gmail_user':
91 data.update({'partner_id': partner_id and partner_id[0]})
93 data.update({'partner_id': False})
95 contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
97 addresss_obj.create(cr, uid, data, context=context)
100 next = contact.GetNextLink()
103 contact = google_obj._get_contact(next.href)
107 'name': _('Partner'),
108 'domain': "[('id','in',"+partner_id+")]",
110 'view_mode': 'tree,form',
111 'res_model': 'res.partner',
113 'views': [(False, 'tree'),(False, 'form')],
114 'type': 'ir.actions.act_window',
117 return {'type': 'ir.actions.act_window_close'}
119 google_contact_import()
121 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: