Devishree Brahmbhatt (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-addons/trunk-import_google-merge-dbr into 
lp:~openerp-dev/openobject-addons/trunk-import_google.

Requested reviews:
  OpenERP R&D Team (openerp-dev)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-import_google-merge-dbr/+merge/64127
-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/trunk-import_google-merge-dbr/+merge/64127
Your team OpenERP R&D Team is requested to review the proposed merge of 
lp:~openerp-dev/openobject-addons/trunk-import_google-merge-dbr into 
lp:~openerp-dev/openobject-addons/trunk-import_google.
=== modified file 'import_base/import_framework.py'
--- import_base/import_framework.py	2011-06-06 07:10:10 +0000
+++ import_base/import_framework.py	2011-06-10 09:14:47 +0000
@@ -346,11 +346,6 @@
             result = []
             imported = set() #to invoid importing 2 times the sames modules
             for table in self.table_list:
-#                try:
-#                    to_import = self.get_mapping()[table].get('import', True)
-#                except:
-#                    import traceback,sys
-#                    info = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
                 to_import = self.get_mapping()[table].get('import', True)
                 if not table in imported:
                     res = self._resolve_dependencies(self.get_mapping()[table].get('dependencies', []), imported)

=== modified file 'import_google/wizard/google_contact_import.py'
--- import_google/wizard/google_contact_import.py	2011-06-08 05:32:50 +0000
+++ import_google/wizard/google_contact_import.py	2011-06-10 09:14:47 +0000
@@ -29,16 +29,15 @@
 from osv import fields
 from osv import osv
 from tools.translate import _
-from import_google import import_contact
+from import_google import google_import
+
 
 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 @@
             'target': 'new',
         }
         return value
+    
 google_login_contact()
 
 class synchronize_google_contact(osv.osv_memory):
@@ -88,191 +88,41 @@
         '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']
+    def import_contact(self, cr, uid, ids, context=None):
         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 @@
         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 @@
                         '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 {}

=== modified file 'import_google/wizard/import_google.py'
--- import_google/wizard/import_google.py	2011-06-08 05:32:50 +0000
+++ import_google/wizard/import_google.py	2011-06-10 09:14:47 +0000
@@ -37,16 +37,19 @@
 from import_base.import_framework import *
 from import_base.mapper import *
 
-class import_contact(import_framework):
+class google_import(import_framework):
     
     gd_client = False
     calendars = False
     DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
-    TABLE_CONTACT = 'contact'
+    TABLE_CONTACT = 'Contact'
+    TABLE_ADDRESS ='Address'
     TABLE_EVENT = 'Events'
    
     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,12 +59,23 @@
     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(),
-        }
+        if table == "Contact":
+            val = {
+                self.TABLE_CONTACT: self.get_contact(),
+            }
+        if table == "Address":
+            val = {
+                self.TABLE_ADDRESS: self.get_contact(),
+            }
+        elif table == "Events":
+            val = {
+                self.TABLE_EVENT: self.get_events(),
+            }
         return val.get(table)
     
 
@@ -257,3 +271,95 @@
                 }
         }
 
+
+    def get_contact(self):
+        contact=self.gclient
+        gclient=self.context.get('client',False)
+        table = self.context.get('table')[0] 
+        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 table == 'Contact':
+                    data.update({'customer': str(self.context.get('customer')),
+                                 'supplier': str(self.context.get('supplier'))})
+                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):
+        partner_id = False
+        address_pool = self.obj.pool.get('res.partner.address')
+        company_pool = self.obj.pool.get('res.company')
+        if 'company' in val:
+            cids = company_pool.search(self.cr, self.uid, [('name', '=', val.get('company'))])
+            if cids:
+                records = company_pool.browse(self.cr, self.uid, cids)
+                for rec in records:
+                    if rec.partner_id:
+                        partner_id = rec.partner_id
+        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': {
+                'id':'id',
+                'name': 'name',
+                'customer': 'customer',
+                'supplier': 'supplier',
+                'address/id': self.get_partner_address,
+                }
+            }   
+                    
+    def get_address_mapping(self):
+        return {
+            'model': 'res.partner.address',
+            'dependencies': [],
+            'map': {
+                'id':'id',
+                'name': 'name',
+                'city': 'city',
+                'phone': 'phone',
+                'mobile': 'mobile',
+                'email': 'email',
+                'fax': 'fax',
+                'function': 'function'
+                }
+        
+        }
+

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to