def saveHsptContacts(user_id=None, company_id=None, leadList=None): try: for newLead in leadList: newLead = vars(newLead)['_field_values'] hspt_id = str(newLead['vid']) #print 'gs id is ' + str(hspt_id) #hspt_sfdc_id = str(newLead['sfdcLeadId']) # check if there is a corresponding lead from SFDC hspt_sfdc_id = None if 'salesforceleadid' in newLead['properties']: hspt_sfdc_id = str(newLead['properties']['salesforceleadid']) # temp fix by satya till SFDC ID field in Hubspot is discovered #addThisList = True existingLeadSfdc = None existingLead = None existingLead = Lead.objects(Q(company_id=company_id) & Q(hspt_id=hspt_id)).first() if existingLead is not None and 'hspt' in existingLead.leads: # we found this lead already in the DB Lead.objects(Q(company_id=company_id) & Q(hspt_id=hspt_id)).update(leads__hspt=newLead) # if 'hspt' in existingLead.lists: # currentLists = existingLead.lists['mkto'] # for i in range(len(currentLists)): # if currentLists[i]['id'] == newList['id']: # check if this activity already exists in the lead dict # addThisList = False # if addThisList == True: # currentLists.append(newList) # existingLead.update(lists__mkto=currentLists) # else: # currentLists = [] # currentLists.append(newList) # existingLead.update(lists__mkto=currentLists) elif existingLead is None: # this lead does not exist if hspt_sfdc_id is not None: # but has a SFDC lead id print 'found lead with SFDC ID ' + str(hspt_sfdc_id) existingLeadSfdc = Lead.objects(Q(company_id=company_id) & Q(leads__sfdc__Id=hspt_sfdc_id)).first() if existingLeadSfdc is not None: # we found a SFDC lead record which is matched to this new Mkto lead existingLeadSfdc.hspt_id = hspt_id existingLeadSfdc.leads['hspt'] = newLead existingLeadSfdc.save() # currentLists = [] # currentLists.append(newList) # existingLeadSfdc.update(lists__mkto=currentLists) if existingLeadSfdc is None and existingLead is None: # no matches found so save new record lead = Lead() lead.hspt_id = hspt_id lead.company_id = company_id lead.leads["hspt"] = newLead lead.save() # currentLists = [] # currentLists.append(newList) # lead.update(lists__mkto=currentLists) except Exception as e: send_notification(dict(type='error', success=False, message=str(e)))