From 6362a0955dfcd08208b5e19eddc29268b7edbf3a Mon Sep 17 00:00:00 2001 From: Thibault Francois Date: Mon, 14 Mar 2011 19:13:30 +0100 Subject: [PATCH 1/2] [FIX] use import data, manage xml id and real id, poc of importing user and leads and assigned user to lead bzr revid: tfr@openerp.com-20110314181330-66m3f2ewa73nvnn0 --- addons/sugarcrm_syncro/import_sugarcrm.py | 95 +++++++++---------- .../sugarcrm_fields_mapping.py | 10 +- 2 files changed, 51 insertions(+), 54 deletions(-) diff --git a/addons/sugarcrm_syncro/import_sugarcrm.py b/addons/sugarcrm_syncro/import_sugarcrm.py index aec2ee2ba64..623a07f1574 100644 --- a/addons/sugarcrm_syncro/import_sugarcrm.py +++ b/addons/sugarcrm_syncro/import_sugarcrm.py @@ -39,29 +39,33 @@ def find_mapped_id(obj, cr, uid, res_model, sugar_id, context): return model_obj.search(cr, uid, [('model', '=', res_model), ('module', '=', 'sugarcrm_import'), ('name', '=', sugar_id)], context=context) -def import_users(surgar_obj, cr, uid, context=None): +def import_users(sugar_obj, cr, uid, context=None): + print "Import Users" if not context: context = {} - map_user = {'Users': - {'name': ['first_name', 'last_name'], + map_user = {'id' : 'id', + 'name': ['first_name', 'last_name'], 'login': 'user_name', - 'new_password' : 'pwd_last_changed', - } , - } - new_user_id = False - user_obj = surgar_obj.pool.get('res.users') + + + 'context_lang' : 'context_lang', + 'password' : 'password', + '.id' : '.id', + } + user_obj = sugar_obj.pool.get('res.users') PortType,sessionid = sugar.login(context.get('username',''), context.get('password','')) sugar_data = sugar.search(PortType,sessionid, 'Users') for val in sugar_data: user_ids = user_obj.search(cr, uid, [('login', '=', val.get('user_name'))]) - if user_ids: #we skip the import of admin but we should save the mapping in ir.model.data - #create_mapping(surgar_obj, cr, uid - continue - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, 'Users', map_user) - openerp_val = dict(zip(fields,datas)) - openerp_val['context_lang'] = context.get('lang','en_US') - new_user_id = user_obj.create(cr, uid, openerp_val, context) - return new_user_id + if user_ids: + val['.id'] = str(user_ids[0]) + else: + val['password'] = 'sugarcrm' #default password for all user + + val['context_lang'] = context.get('lang','en_US') + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_user) + #All data has to be imported separatly because they don't have the same field + user_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) def get_lead_status(surgar_obj, cr, uid, sugar_val,context=None): if not context: @@ -86,29 +90,30 @@ def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None): if not context: context = {} stage_id = '' - stage_dict = {'sales_stage': #field in the sugarcrm database - { #Mapping of sugarcrm stage : openerp opportunity stage Mapping + stage_dict = { #Mapping of sugarcrm stage : openerp opportunity stage Mapping 'Need Analysis': 'New', 'Closed Lost': 'Lost', 'Closed Won': 'Won', 'Value Proposition': 'Proposition', 'Negotiation/Review': 'Negotiation' - },} - stage = stage_dict['sales_stage'].get(sugar_val['sales_stage'], '') + } + stage = stage_dict.get(sugar_val['sales_stage'], '') stage_pool = surgar_obj.pool.get('crm.case.stage') stage_ids = stage_pool.search(cr, uid, [("type", '=', 'opportunity'), ('name', '=', stage)]) for stage in stage_pool.browse(cr, uid, stage_ids, context): stage_id = stage.id return stage_id -def import_leads(surgar_obj, cr, uid, context=None): +def import_leads(sugar_obj, cr, uid, context=None): + print "Import Lead" if not context: context = {} map_lead = { - 'Leads':{'name': ['first_name','last_name'], - 'contact_name': ['first_name','last_name'], + 'id' : 'id', + 'name': ['first_name', 'last_name'], + 'contact_name': ['first_name', 'last_name'], 'description': 'description', - 'partner_name': ['first_name','last_name'], + 'partner_name': ['first_name', 'last_name'], 'email_from': 'email1', 'phone': 'phone_work', 'mobile': 'phone_mobile', @@ -117,32 +122,24 @@ def import_leads(surgar_obj, cr, uid, context=None): 'street': 'primary_address_street', 'zip': 'primary_address_postalcode', 'city':'primary_address_city', - }, - } - user_id = False - lead_pool = surgar_obj.pool.get('crm.lead') - PortType,sessionid = sugar.login(context.get('username',''), context.get('password','')) - sugar_data = sugar.search(PortType,sessionid, 'Leads') - new_lead_id = False + 'user_id/id' : 'assigned_user_id', + + + 'stage_id.id' : 'stage_id.id', + 'type' : 'type', + + } + + lead_obj = sugar_obj.pool.get('crm.lead') + PortType, sessionid = sugar.login(context.get('username', ''), context.get('password', '')) + sugar_data = sugar.search(PortType, sessionid, 'Leads') for val in sugar_data: - #Need To FIx for Import user data record from sugarcrm. - if val.get('assigned_user_name'): - user_ids = surgar_obj.pool.get('res.users').search(cr, uid, [('name', '=', val.get("assigned_user_name"))]) - if not user_ids: - user_ids = surgar_obj.pool.get('res.users').create(cr, uid, {'name': val.get('assigned_user_name'), 'login': val.get("assigned_user_name")}) - if user_ids: - if isinstance(user_ids,list): - user_id = user_ids[0] - else: - user_id=user_ids - fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, 'Leads', map_lead) - stage_id = get_lead_status(surgar_obj, cr, uid, val, context) - openerp_val = dict(zip(fields,datas)) - openerp_val['type'] = 'lead' - openerp_val['user_id'] = user_id - openerp_val['stage_id'] = stage_id - new_lead_id = lead_pool.create(cr, uid, openerp_val, context) - return new_lead_id + val['type'] = 'lead' + stage_id = get_lead_status(sugar_obj, cr, uid, val, context) + val['stage_id.id'] = stage_id + fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) + lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) + def import_opportunities(surgar_obj, cr, uid, context=None): if not context: diff --git a/addons/sugarcrm_syncro/sugarcrm_fields_mapping.py b/addons/sugarcrm_syncro/sugarcrm_fields_mapping.py index ecc4534ba4a..b3f1c3002fa 100644 --- a/addons/sugarcrm_syncro/sugarcrm_fields_mapping.py +++ b/addons/sugarcrm_syncro/sugarcrm_fields_mapping.py @@ -19,14 +19,14 @@ # ############################################################################## -def sugarcrm_fields_mapp(dict_sugar,openerp_obj,openerp_dict): +def sugarcrm_fields_mapp(dict_sugar, openerp_dict): fields=[] data_lst = [] - for key,val in openerp_dict.get(openerp_obj).items(): - if key not in fields: + for key,val in openerp_dict.items(): + if key not in fields and dict_sugar.get(isinstance(val, list) and val[0] or val): fields.append(key) if isinstance(val, list): - data_lst.append(dict_sugar.get(val[0],['']) + ' ' + dict_sugar.get(val[1],[''])) + data_lst.append(' '.join(map(lambda x : dict_sugar[x], val))) else: - data_lst.append(dict_sugar.get(val,[''])) + data_lst.append(dict_sugar.get(val,'')) return fields,data_lst From 374639a0ed9a78f81745c414fa88da25d813dc0e Mon Sep 17 00:00:00 2001 From: Thibault Francois Date: Mon, 14 Mar 2011 19:18:25 +0100 Subject: [PATCH 2/2] [CLEAN] clean print + fixes bzr revid: tfr@openerp.com-20110314181825-osau1mngku7y3h8h --- addons/sugarcrm_syncro/import_sugarcrm.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/addons/sugarcrm_syncro/import_sugarcrm.py b/addons/sugarcrm_syncro/import_sugarcrm.py index 623a07f1574..cd2f9077a95 100644 --- a/addons/sugarcrm_syncro/import_sugarcrm.py +++ b/addons/sugarcrm_syncro/import_sugarcrm.py @@ -40,7 +40,6 @@ def find_mapped_id(obj, cr, uid, res_model, sugar_id, context): def import_users(sugar_obj, cr, uid, context=None): - print "Import Users" if not context: context = {} map_user = {'id' : 'id', @@ -105,7 +104,6 @@ def get_opportunity_status(surgar_obj, cr, uid, sugar_val,context=None): return stage_id def import_leads(sugar_obj, cr, uid, context=None): - print "Import Lead" if not context: context = {} map_lead = { @@ -140,7 +138,7 @@ def import_leads(sugar_obj, cr, uid, context=None): fields, datas = sugarcrm_fields_mapping.sugarcrm_fields_mapp(val, map_lead) lead_obj.import_data(cr, uid, fields, [datas], mode='update', current_module='sugarcrm_import', context=context) - +#Need fixes def import_opportunities(surgar_obj, cr, uid, context=None): if not context: context = {}