[MERGE]:Merging the google_contact
authorDBR (OpenERP) <dbr@tinyerp.com>
Wed, 8 Jun 2011 12:50:29 +0000 (18:20 +0530)
committerDBR (OpenERP) <dbr@tinyerp.com>
Wed, 8 Jun 2011 12:50:29 +0000 (18:20 +0530)
bzr revid: dbr@tinyerp.com-20110608125029-gbv6z7869r3duxnv

addons/import_google/wizard/google_contact_import.py
addons/import_google/wizard/import_google.py

index 8616c36..f945d24 100644 (file)
@@ -31,14 +31,13 @@ from osv import osv
 from tools.translate import _
 from import_google import import_contact
 
+
 class google_login_contact(osv.osv_memory):
     _inherit = 'google.login'
     _name = 'google.login.contact'
-
-    def _get_next_action(self, cr, uid, context):
+    def _get_next_action(self, cr, uid, context=None):
         data_obj = self.pool.get('ir.model.data')
         data_id = data_obj._get_id(cr, uid, 'import_google', 'view_synchronize_google_contact_import_form')
-
         view_id = False
         if data_id:
             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
@@ -54,6 +53,7 @@ class google_login_contact(osv.osv_memory):
             'target': 'new',
         }
         return value
+    
 google_login_contact()
 
 class synchronize_google_contact(osv.osv_memory):
@@ -88,191 +88,41 @@ class synchronize_google_contact(osv.osv_memory):
         'create_partner': 'create_all',
         'group_name': 'all',
     }
-#
-#    def create_partner(self, cr, uid, data={}, context=None):
-#        if context == None:
-#            context = {}
-#        if not data:
-#            return False
-#        name = data.get("company") or data.get('name','')
-#        partner_pool = self.pool.get('res.partner')
-#        partner_id = partner_pool.create(cr, uid, {
-#                                                  'name': name,
-#                                                  'user_id': uid,
-#                                                  'address' : [(6, 0, [data['address_id']])],
-#                                                  'customer': data.get('customer', False),
-#                                                  'supplier': data.get('supplier', False)
-#                                        }, context=context)
-#        return partner_id
-#
-#    def set_partner(self, cr, uid, name, address_id, context=None):
-#        partner_pool = self.pool.get('res.partner')
-#        partner_ids = partner_pool.search(cr, uid, [('name', '=', name)], context=context)
-#        if partner_ids:
-#            address_pool = self.pool.get('res.partner.address')#TODO create partner of find the one with the same name
-#            data = {'partner_id' : partner_ids[0]}
-#            address_pool.write(cr, uid, [address_id], data, context=context)
-#            return partner_ids[0]
-#        return False
 
     def import_contact_all(self, cr, uid, ids, context=None):
-        tables = ['contact']
         obj = self.browse(cr, uid, ids, context=context)[0]
+        cust = obj.customer
+        sup = obj.supplier
+        tables=[]
         user_obj = self.pool.get('res.users').browse(cr, uid, uid)
-        google = self.pool.get('google.login')
-        
+
         gmail_user = user_obj.gmail_user
         gmail_pwd = user_obj.gmail_password
-        context = {}
+
+        google = self.pool.get('google.login')
         gd_client = google.google_login(gmail_user, gmail_pwd, type='contact')
-        context.update({'user': gmail_user,'password': gmail_pwd,'gd_client':gd_client})
-        
-        imp = import_contact(self, cr, uid,'google', "synchronize_google_contact", context=context)
+        if not gd_client:
+            raise osv.except_osv(_('Error'), _("Please specify correct user and password !"))        
+        if obj.group_name not in ['all']:
+            query = gdata.contacts.service.ContactsQuery()
+            query.group = obj.group_name
+            contact = gd_client.GetContactsFeed(query.ToUri())
+        else:
+            contact = gd_client.GetContactsFeed()
+        if obj.create_partner=='create_all':
+            tables.append('Contact')    
+        else:    
+            tables.append('Address')
+       
+        context.update({'gd_client':contact})
+        context.update({'client':gd_client})
+        context.update({'table':tables})
+        context.update({'customer':cust})
+        context.update({'supplier':sup})        
+        imp = google_import(self, cr, uid, 'google', "synchronize_google_contact", gmail_user, context)
         imp.set_table_list(tables)
-        imp.start()
-        return {'type': 'ir.actions.act_window_close'}
-#
-#        if not gd_client:
-#            raise osv.except_osv(_('Error'), _("Please specify correct user and password !"))
-#       
-#        if obj.group_name not in ['all']:
-#            query = gdata.contacts.service.ContactsQuery()
-#            query.group = obj.group_name
-#            contact = gd_client.GetContactsFeed(query.ToUri())
-#        else:
-#            contact = gd_client.GetContactsFeed()
-#
-#        ids = self.create_contact(cr, uid, ids, gd_client, contact, option=obj.create_partner,context=context)
-#        if not ids:
-#            return {'type': 'ir.actions.act_window_close'}
-#
-#        return {
-#                'name': _(obj.create_partner =='create_all' and 'Partners') or _('Contacts'),
-#                'domain': "[('id','in', ["+','.join(map(str,ids))+"])]",
-#                'view_type': 'form',
-#                'view_mode': 'tree,form',
-#                'res_model': obj.create_partner =='create_all' and 'res.partner' or 'res.partner.address',
-#                'context': context,
-#                'views': [(False, 'tree'),(False, 'form')],
-#                'type': 'ir.actions.act_window',
-#        }
-
-
-#    def create_contact(self, cr, uid, ids, gd_client, contact, option,context=None):
-#        model_obj = self.pool.get('ir.model.data')
-#        addresss_obj = self.pool.get('res.partner.address')
-#        company_pool = self.pool.get('res.company')
-#        addresses = []
-#        partner_ids = []
-#        contact_ids = []
-#        if 'tz' in context and context['tz']:
-#            time_zone = context['tz']
-#        else:
-#            time_zone = tools.get_server_timezone()
-#        au_tz = timezone(time_zone)
-#        while contact:
-#            for entry in contact.entry:
-#                data = self._retreive_data(entry)
-#                google_id = data.pop('id')
-#                model_data = {
-#                    'name':  google_id,
-#                    'model': 'res.partner.address',
-#                    'module': 'sync_google_contact',
-#                    'noupdate': True
-#                }
-#
-#                data_ids = model_obj.search(cr, uid, [('model','=','res.partner.address'), ('name','=', google_id)])
-#                if data_ids:
-#                    contact_ids = [model_obj.browse(cr, uid, data_ids[0], context=context).res_id]
-#                elif data['email']:
-#                    contact_ids = addresss_obj.search(cr, uid, [('email', 'ilike', data['email'])])
-#
-#                if contact_ids:
-#                    addresses.append(contact_ids[0])
-#                    address = addresss_obj.browse(cr, uid, contact_ids[0], context=context)
-#                    google_updated = entry.updated.text
-#                    utime = dateutil.parser.parse(google_updated)
-#                    au_dt = au_tz.normalize(utime.astimezone(au_tz))
-#                    updated_dt = datetime.datetime(*au_dt.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S')
-#                    if address.write_date < updated_dt:
-#                        self.update_contact(cr, uid, contact_ids, data, context=context)
-#                    res_id = contact_ids[0]
-#                if not contact_ids:
-#                    #create or link to an existing partner only if it's a new contact
-#                    data.update({'type': 'default'})
-#                    res_id = addresss_obj.create(cr, uid, data, context=context)
-#                    data['address_id'] = res_id
-#                    if option == 'create_all':
-#                        obj = self.browse(cr, uid, ids, context=context)[0]
-#                        data['customer'] = obj.customer
-#                        data['supplier'] = obj.supplier
-#                        res = False
-#                        if 'company' in data:
-#                            res = self.set_partner(cr, uid, data.get('company'), res_id, context=context)
-#                            if res:
-#                                partner_ids.append(res)
-#                        if not res:
-#                            partner_id = self.create_partner(cr, uid, data, context=context)
-#                            partner_ids.append(partner_id)
-#                    addresses.append(res_id)
-#
-#                if not data_ids: #link to google_id if it was not the case before
-#                    model_data.update({'res_id': res_id})
-#                    model_obj.create(cr, uid, model_data, context=context)
-#
-#            next = contact.GetNextLink()
-#            contact = next and gd_client.GetContactsFeed(next.href) or None
-#
-#        if option == 'create_all':
-#            return partner_ids
-#        else:
-#            return addresses
-
-#    def _retreive_data(self, entry):
-#        data = {}
-#        data['id'] = entry.id.text
-#        name = tools.ustr(entry.title.text)
-#        if name == "None":
-#            name = entry.email[0].address
-#        data['name'] = name
-#        emails = ','.join(email.address for email in entry.email)
-#        data['email'] = emails
-#        if entry.organization:
-#            if entry.organization.org_name:
-#                data.update({'company': entry.organization.org_name.text})
-#            if entry.organization.org_title:
-#                data.update ({'function': entry.organization.org_title.text})
-#
-#
-#        if entry.phone_number:
-#            for phone in entry.phone_number:
-#                if phone.rel == gdata.contacts.REL_WORK:
-#                    data['phone'] = phone.text
-#                if phone.rel == gdata.contacts.PHONE_MOBILE:
-#                    data['mobile'] = phone.text
-#                if phone.rel == gdata.contacts.PHONE_WORK_FAX:
-#                    data['fax'] = phone.text
-#        return data
-
-#    def update_contact(self, cr, uid, contact_ids, data, context=None):
-#        addresss_obj = self.pool.get('res.partner.address')
-#        vals = {}
-#        addr = addresss_obj.browse(cr,uid,contact_ids)[0]
-#        name = str((addr.name or addr.partner_id and addr.partner_id.name or '').encode('utf-8'))
-#
-#        if name != data.get('name'):
-#            vals['name'] = data.get('name','')
-#        if addr.email != data.get('email'):
-#            vals['email'] = data.get('email','')
-#        if addr.mobile != data.get('mobile'):
-#            vals['mobile'] = data.get('mobile','')
-#        if addr.phone != data.get('phone'):
-#            vals['phone'] = data.get('phone','')
-#        if addr.fax != data.get('fax'):
-#            vals['fax'] = data.get('fax','')
-#
-#        addresss_obj.write(cr, uid, contact_ids, vals, context=context)
-#        return {'type': 'ir.actions.act_window_close'}
+        imp.start()            
+        return {}   
 
 synchronize_google_contact()
 
@@ -303,7 +153,6 @@ class google_login_calendar(osv.osv_memory):
         return value
 google_login_calendar()
 
-
 class synchronize_google_calendar_events(osv.osv_memory):
     """
     Wizard to initiate import specific calendar or all calendars
@@ -354,7 +203,7 @@ class synchronize_google_calendar_events(osv.osv_memory):
                         'password': gmail_pwd,
                         'calendars': calendars,
                         'instance': 'calendar'})
-        imp = import_contact(self, cr, uid, 'import_google', "import_google_calendar", [gmail_user], context)
+        imp = google_import(self, cr, uid, 'import_google', "import_google_calendar", [gmail_user], context)
         imp.set_table_list(table)
         imp.start()
         return {}
index 1408662..97b2220 100644 (file)
@@ -47,6 +47,8 @@ class import_contact(import_framework):
    
     def initialize(self):
         google = self.obj.pool.get('google.login')
+        self.external_id_field = 'Id'
+        self.gclient=self.context.get('gd_client', False)
         self.gd_client = google.google_login(self.context.get('user'), 
                                        self.context.get('password'), 
                                         type = self.context.get('instance'))
@@ -56,11 +58,14 @@ class import_contact(import_framework):
     def get_mapping(self):
         return { 
             self.TABLE_EVENT: self.get_event_mapping(),
+            self.TABLE_CONTACT: self.get_contact_mapping(),
+            self.TABLE_ADDRESS: self.get_address_mapping(),
         }
         
     def get_data(self, table):
         val = {
             self.TABLE_EVENT: self.get_events(),
+            self.TABLE_CONTACT: self.get_contact(),
         }
         return val.get(table)
     
@@ -257,3 +262,79 @@ class import_contact(import_framework):
                 }
         }
 
+
+    def get_contact(self):
+        contact=self.gclient
+        gclient=self.context.get('client',False)
+        datas = [] 
+        while contact:      
+            for entry in contact.entry:
+                data = {}
+                data['id'] = entry.id.text
+                name = tools.ustr(entry.title.text)
+                if name == "None":
+                    name = entry.email[0].address
+                data['name'] = name
+                emails = ','.join(email.address for email in entry.email)
+                data['email'] = emails
+                if entry.organization:
+                    if entry.organization.org_name:
+                        data.update({'company': entry.organization.org_name.text})
+                    if entry.organization.org_title:
+                        data.update ({'function': entry.organization.org_title.text})
+                if entry.phone_number:
+                    for phone in entry.phone_number:
+                        if phone.rel == gdata.contacts.REL_WORK:
+                            data['phone'] = phone.text
+                        if phone.rel == gdata.contacts.PHONE_MOBILE:
+                            data['mobile'] = phone.text
+                        if phone.rel == gdata.contacts.PHONE_WORK_FAX:
+                            data['fax'] = phone.text  
+                datas.append(data)        
+            next = contact.GetNextLink()
+            contact = next and gclient.GetContactsFeed(next.href) or None     
+        return datas
+    
+    
+    def get_partner_address(self,val):
+            field_map = {
+                'name': 'name',
+                'type': 'Type',
+                'city': 'city',
+                'phone': 'phone',
+                'mobile': 'mobile',
+                'email': 'email',
+                'fax': 'fax',
+            }
+            val.update({'Type':'contact'})
+            val.update({'id_new': val['id']+'address_contact' })
+            return self.import_object_mapping(field_map , val, 'res.partner.address', self.context.get('table')[0], val['id_new'], self.DO_NOT_FIND_DOMAIN)
+        
+    def get_contact_mapping(self):
+        return {
+            'model': 'res.partner',
+            'dependencies': [],
+            'map': {
+                'name': 'name',
+                'customer': str(self.context.get('customer')),
+                'supplier': str(self.context.get('supplier')),
+                'address/id': self.get_partner_address,
+                }
+            }   
+                    
+    def get_address_mapping(self):
+        print"RRRRRRRRRREEElated to Address ONLY......................"
+        return {
+            'model': 'res.partner.address',
+            'dependencies': [],
+            'map': {
+               'name': 'name',
+                'city': 'city',
+                'phone': 'phone',
+                'mobile': 'mobile',
+                'email': 'email',
+                'fax': 'fax'
+                }
+        
+        }
+        
\ No newline at end of file