[REF] sync_google_contact: Store google id into ir_model_data. Associate with address record. Unlink data model record when address is deleted.

bzr revid: uco@tinyerp.com-20110223131803-8g7eghkm2qs6dopm
This commit is contained in:
Ujjvala Collins (OpenERP)
2011-02-23 18:48:03 +05:30
parent 2057645334
commit 15c6f4e2a3
5 changed files with 62 additions and 19 deletions
@@ -20,7 +20,6 @@
##############################################################################
from osv import fields,osv,orm
class res_users(osv.osv):
_inherit = "res.users"
@@ -30,14 +29,14 @@ class res_users(osv.osv):
}
res_users()
class res_partner_address(osv.osv):
class ir_model_data(osv.osv):
_inherit = "res.partner.address"
_inherit = "ir.model.data"
_columns = {
'google_id': fields.char('Google Contact Id', size=128, readonly=True),
}
res_partner_address()
ir_model_data()
# vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4:
+2 -2
View File
@@ -19,7 +19,7 @@
</record>
<record model="ir.actions.act_window" id="act_google_contact_import_form">
<field name="name">Import contacts from remote adress book</field>
<field name="name">Import contacts from remote address book</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">synchronize.base.contact.wizard.import</field>
<field name="view_type">form</field>
@@ -29,7 +29,7 @@
</record>
<menuitem name="Import contacts from remote adress book" id="menu_sync_contact"
<menuitem name="Import contacts from remote address book" id="menu_sync_contact"
parent="base.menu_address_book" action="act_google_contact_import_form"
sequence="40" />
+1
View File
@@ -19,6 +19,7 @@
#
##############################################################################
import sync_google_contact
import wizard
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields,osv,orm
class res_partner_address(osv.osv):
_inherit = "res.partner.address"
def unlink(self, cr, uid, ids, context=None):
model_obj = self.pool.get('ir.model.data')
model_ids = model_obj.search(cr, uid, [('res_id','in',ids)], context=context)
model_obj.unlink(cr, uid, model_ids, context=context)
return super(res_partner_address, self).unlink(cr, uid, ids, context=context)
res_partner_address()
# vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4:
@@ -2,7 +2,7 @@
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 L (<http://tiny.be>).
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -24,11 +24,9 @@ from tools.translate import _
import tools
try:
import gdata
from gdata import contacts
import gdata.contacts.service
import gdata.contacts
import gdata.contacts.client
import atom
except ImportError:
raise osv.except_osv(_('Google Contacts Import Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
@@ -41,7 +39,7 @@ class google_base_import(osv.osv_memory):
should be overwritten by subclasses
"""
names = super(google_base_import, self)._get_tools_name(cr, user, context=context)
names.append(('gmail','Gmail adress book'))
names.append(('gmail','Gmail address book'))
return names
@@ -203,20 +201,25 @@ class synchronize_google_contact(osv.osv_memory):
return {'type': 'ir.actions.act_window_close'}
def create_contact(self, cr, uid, gd_client,contact, partner_id=False,context=None):
model_obj = self.pool.get('ir.model.data')
addresss_obj = self.pool.get('res.partner.address')
addresses = []
partner_ids = []
while contact:
for entry in contact.entry:
data={}
data = {}
model_data = {
'name': 'google_contacts_information_%s' %(entry.id.text),
'model': 'res.partner.address',
'module': 'sync_google_contact',
}
name = tools.ustr(entry.title.text)
google_id = entry.id.text
emails = ','.join(email.address for email in entry.email)
if name and name != 'None':
data['name'] = name
if google_id:
data['google_id'] =google_id
model_data.update({'google_id': google_id})
if entry.phone_number:
for phone in entry.phone_number:
if phone.rel == gdata.contacts.REL_WORK:
@@ -225,11 +228,13 @@ class synchronize_google_contact(osv.osv_memory):
data['mobile'] = phone.text
if phone.rel == gdata.contacts.PHONE_WORK_FAX:
data['fax'] = phone.text
if emails:
data_ids = model_obj.search(cr, uid, [('google_id','=',google_id)])
if data_ids:
contact_ids = [model_obj.browse(cr, uid, data_ids[0], context=context).res_id]
elif emails:
data['email'] = emails
contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
else:
contact_ids = addresss_obj.search(cr, uid, [('google_id','=',google_id)])
if partner_id and name!='None':
partner_id, data = self.create_partner(cr, uid, data, context=context)
@@ -238,7 +243,10 @@ class synchronize_google_contact(osv.osv_memory):
addresses.append(contact_ids[0])
self.update_contact( cr, uid, contact_ids, data,context=context)
if not contact_ids:
addresses.append(addresss_obj.create(cr, uid, data, context=context))
res_id = addresss_obj.create(cr, uid, data, context=context)
addresses.append(res_id)
model_data.update({'res_id': res_id})
model_obj.create(cr, uid, model_data, context=context)
if not contact:
break
next = contact.GetNextLink()
@@ -252,8 +260,8 @@ class synchronize_google_contact(osv.osv_memory):
addresss_obj = self.pool.get('res.partner.address')
if context==None:
context={}
res={}
addr=addresss_obj.browse(cr,uid,contact_ids)[0]
res = {}
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'))
email = addr.email
phone = addr.phone