3427f9a9aceceef6306a18ee3a02227ccf7516e1
[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
25 from sync_google_contact import sync_google_contact
26
27 class google_contact_import(osv.osv_memory):
28     _name = "synchronize.base"    
29     _inherit = 'synchronize.base'
30     _columns = {
31         'tools': fields.selection([('gmail','Gmail')], 'Tools'),
32         'create_partner': fields.selection([('group','Group'),('gmail_user','Gmail user')], 'Create Partner'),
33      }
34         
35     def import_contact(self, cr, uid, ids, context=None):
36         # Only see the result, we will change the code
37         
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 !"))      
45
46         google_obj = sync_google_contact.google_lib(gmail_user, gamil_pwd)
47         contact = google_obj._get_contact()      
48         partner_id = []
49         partner_name = []
50         group_links = {}
51
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)
63                 
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)
67                 if not partner_id:
68                     partner_id.append(partner_obj.create(cr, uid, {'name': name}, context=context))
69                 contact = google_obj._get_contact()
70                 contact_ids = []
71                 link = group_links.get(name)
72                 while contact:
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)
78                         data = {
79                                 'name': contact_name,
80                                 'phone': phone_numbers,
81                                 'email': emails,
82                                 'google_id': google_id,
83                                 'partner_id': partner_id and partner_id[0]
84                          }
85                         if entry.group_membership_info and link:
86                             for grp in entry.group_membership_info:
87                                 if grp.href == link:
88                                     addresss_obj.create(cr, uid, data, context=context)
89                         else:
90                             if obj.create_partner == 'gmail_user':
91                                 data.update({'partner_id': partner_id and partner_id[0]})
92                             else:
93                                 data.update({'partner_id': False})
94                                 continue
95                             contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
96                             if not contact_ids:
97                                 addresss_obj.create(cr, uid, data, context=context)
98                     if not contact:
99                         break
100                     next = contact.GetNextLink()
101                     contact = None
102                     if next:
103                         contact = google_obj._get_contact(next.href)
104                 partner_id = []
105             if partner_id:
106                 return {
107                         'name': _('Partner'),
108                         'domain': "[('id','in',"+partner_id+")]",
109                         'view_type': 'form',
110                         'view_mode': 'tree,form',
111                         'res_model': 'res.partner',
112                         'context': context,
113                         'views': [(False, 'tree'),(False, 'form')],
114                         'type': 'ir.actions.act_window',
115                 }
116             else:
117                 return {'type': 'ir.actions.act_window_close'}   
118
119 google_contact_import()
120
121 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: