MERGE:lp:~openerp-dev/openobject-addons/trunk-import_google-Backlog_2_ref-dbr
bzr revid: dbr@tinyerp.com-20110606071010-mf063vww9oivtwtl
This commit is contained in:
parent
26c410280d
commit
7e9db70ace
|
@ -81,4 +81,4 @@ class google_login(osv.osv_memory):
|
|||
|
||||
google_login()
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
|
@ -214,7 +214,6 @@ class import_framework(Thread):
|
|||
fields.append(key)
|
||||
value = val(dict(dict_sugar))
|
||||
data_lst.append(value)
|
||||
|
||||
return fields, data_lst
|
||||
|
||||
def _generate_xml_id(self, name, table):
|
||||
|
@ -297,7 +296,6 @@ class import_framework(Thread):
|
|||
domain_search = not domain_search and [('name', 'ilike', name)] or domain_search
|
||||
obj = self.obj.pool.get(model)
|
||||
xml_id = self._generate_xml_id(name, table)
|
||||
|
||||
xml_ref = self.mapped_id_if_exist(model, domain_search, table, name)
|
||||
fields.append('id')
|
||||
data.append(xml_id)
|
||||
|
@ -342,13 +340,17 @@ class import_framework(Thread):
|
|||
|
||||
|
||||
"""
|
||||
|
||||
self.data_started = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
self.cr = pooler.get_db(self.cr.dbname).cursor()
|
||||
try:
|
||||
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)
|
||||
|
@ -431,4 +433,4 @@ class import_framework(Thread):
|
|||
"""
|
||||
return "The import of data \n instance name : %s \n" % self.instance_name
|
||||
|
||||
#For example of use see import_sugarcrm
|
||||
#For example of use see import_sugarcrm
|
||||
|
|
|
@ -34,12 +34,96 @@ from osv import fields,osv
|
|||
from tools.translate import _
|
||||
import tools
|
||||
|
||||
from import_base.import_framework import *
|
||||
from import_base.mapper import *
|
||||
|
||||
class impor_contact(import_framework):
|
||||
|
||||
gd_client = False
|
||||
TABLE_CONTACT = 'Contact'
|
||||
TABLE_MEETING = 'Event'
|
||||
|
||||
def initialize(self):
|
||||
self.gd_client = gdata.contacts.service.ContactsService()
|
||||
self.gd_client.ClientLogin(self.context.get('user', False),self.context.get('password', False))
|
||||
|
||||
def get_mapping(self):
|
||||
return {
|
||||
self.TABLE_CONTACT: self.get_contact_mapping(),
|
||||
}
|
||||
def _retreive_data(self,entry):
|
||||
if 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
|
||||
else:
|
||||
data['phone'] = False
|
||||
if phone.rel == gdata.contacts.PHONE_MOBILE:
|
||||
data['mobile'] = phone.text
|
||||
else :
|
||||
data['mobile'] = False
|
||||
if phone.rel == gdata.contacts.PHONE_WORK_FAX:
|
||||
data['fax'] = phone.text
|
||||
else :
|
||||
data['fax'] = False
|
||||
|
||||
data.update({
|
||||
'mobile': data.has_key('mobile') and data['mobile'] or False,
|
||||
'phone':data.has_key('phone') and data['phone'] or False,
|
||||
'fax':data.has_key('fax') and data['fax'] or False,
|
||||
'type':'contact',
|
||||
'id_new':data['id'] + '_data_'+ name,
|
||||
})
|
||||
|
||||
|
||||
address = {
|
||||
'name': 'name',
|
||||
'type': 'type',
|
||||
'phone': 'phone',
|
||||
'mobile': 'mobile',
|
||||
'email': 'email',
|
||||
'fax': 'fax',
|
||||
}
|
||||
return self.import_object_mapping(address,data, 'res.partner.address', 'res.partner.address',data['id_new'], self.DO_NOT_FIND_DOMAIN)
|
||||
|
||||
def get_contact_mapping(self):
|
||||
contact = self.gd_client.GetContactsFeed()
|
||||
while contact:
|
||||
val = []
|
||||
for entry in contact.entry:
|
||||
val = self._retreive_data(entry)
|
||||
#val.append(self._retreive_data(entry))
|
||||
return {
|
||||
'model': 'res.partner.address',
|
||||
'import' : False,
|
||||
'dependencies': [],
|
||||
#'hook': self._retreive_data,
|
||||
'map': val
|
||||
}
|
||||
|
||||
class google_login_contact(osv.osv_memory):
|
||||
_inherit = 'google.login'
|
||||
_name = 'google.login.contact'
|
||||
def _get_next_action(self, cr, uid, context=None):
|
||||
|
||||
def _get_next_action(self, cr, uid, context):
|
||||
data_obj = self.pool.get('ir.model.data')
|
||||
data_id = data_obj._get_id(cr, uid, 'import_google_contact', '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
|
||||
|
@ -89,185 +173,190 @@ 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 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(self, cr, uid, ids, context=None):
|
||||
tables = ['contact']
|
||||
obj = self.browse(cr, uid, ids, context=context)[0]
|
||||
|
||||
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
|
||||
|
||||
google = self.pool.get('google.login')
|
||||
context = {}
|
||||
gd_client = google.google_login(gmail_user, gmail_pwd, type='contact')
|
||||
|
||||
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',
|
||||
}
|
||||
context.update({'user': gmail_user,'password': gmail_pwd,'gd_client':gd_client})
|
||||
|
||||
imp = impor_contact(self, cr, uid,'google', "synchronize_google_contact", context=context)
|
||||
imp.set_table_list(tables)
|
||||
imp.start()
|
||||
#
|
||||
# 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
|
||||
}
|
||||
# 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
|
||||
|
||||
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'])])
|
||||
# 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
|
||||
|
||||
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'}
|
||||
# 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'}
|
||||
|
||||
synchronize_google_contact()
|
||||
|
||||
|
|
Loading…
Reference in New Issue