Пример #1
0
def saveSfdcOppStageHistoryToMaster(user_id=None, company_id=None, job_id=None, run_type=None): 
    #job_id = ObjectId("56b2b92c8afb0070a795c4b2")

    if run_type == 'initial':   
        #activities = TempData.objects(Q(company_id=company_id) & Q(record_type='activity') & Q(source_system='mkto') & Q(job_id=job_id) ).only('source_record') 
        collection = TempData._get_collection()
        activities = collection.find({'company_id': int(company_id), 'record_type': 'opp_stage_history', 'source_system': 'sfdc', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
        
    else:
        collection = TempDataDelta._get_collection()
        activities = collection.find({'company_id': int(company_id), 'record_type': 'opp_stage_history', 'source_system': 'sfdc', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
    
    try:
        print 'got history ' + str(activities.count())
        for activity in activities: 
            skipThis = False
            newActivity = activity['source_record']
            addThisActivity = True
            print 'act is ' + str(newActivity)
            newOppId = newActivity["OpportunityId"]
            if newOppId is not None:
                print 'trying to get opp'
                existingAccount = Account.objects(Q(company_id = company_id) & Q(opportunities__sfdc__Id = newOppId)).first()
                print 'account is ' + str(existingAccount)
            else:
                continue
            if existingAccount is None:
                continue
            
            for existingOpp in existingAccount['opportunities']['sfdc']: #loop through each opp to find the right one
                if existingOpp['Id'] == newOppId:
                    if 'stage_activities' not in existingOpp: #if no prior activities
                        existingOpp['stage_activities'] = [] #create list
                        existingOpp['stage_activities'].append(newActivity) #save activity
                        print 'saved virgin stage activity for opp'
                    else:
                        for existingActivity in existingOpp['stage_activities']: #check if this activity already exists
                            if existingActivity['Id'] == newActivity['Id']: #it exists, so skip the activity
                                skipThis = True
                                print 'skipping opp stage activity'
                                break
                        if skipThis:
                            break # get out of this for loop
                        else:
                            existingOpp['stage_activities'].append(newActivity)
                            print 'saved stage activity for opp'
                    existingAccount.save()
                            
            

    except Exception as e:
        print 'exception while saving SFDC Opp stage history to master' + str(e)
        send_notification(dict(
         type='error',
         success=False,
         message=str(e)
        ))                   
Пример #2
0
def saveSfdcContactHistoryToMaster(user_id=None, company_id=None, job_id=None, run_type=None): 
    #job_id = ObjectId("56a6faa28afb00042171cd89")

    if run_type == 'initial':   
        #activities = TempData.objects(Q(company_id=company_id) & Q(record_type='activity') & Q(source_system='mkto') & Q(job_id=job_id) ).only('source_record') 
        collection = TempData._get_collection()
        activities = collection.find({'company_id': int(company_id), 'record_type': 'contact_history', 'source_system': 'sfdc', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
        
    else:
        collection = TempDataDelta._get_collection()
        activities = collection.find({'company_id': int(company_id), 'record_type': 'contact_history', 'source_system': 'sfdc', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
    
    try:
        print 'got history ' + str(activities.count())
        for activity in activities: 
                
            newActivity = activity['source_record']
            addThisActivity = True
            print 'act is ' + str(newActivity)
            leadId = newActivity["ContactId"]
            if leadId is not None:
                print 'trying to get lead'
                existingLead = Lead.objects(Q(company_id = company_id) & Q(sfdc_contact_id = leadId)).first()
                print 'contact is ' + str(existingLead)
            else:
                continue
            if existingLead is None:
                continue
            
            if 'sfdc' in existingLead['activities']: # there are activities from SFDC for this leadId
                for existingActivity in existingLead['activities']['sfdc']:
                    if existingActivity['Id'] == newActivity['Id']: # this activity already exists so exit the loop
                        addThisActivity = False
                        break
                if addThisActivity:
                    existingLead['activities']['sfdc'].append(newActivity)
                    existingLead.save()
                    print 'saved new activity 1'
            else:
                print 'no sfdc acts'
                addThisActivity = True
                sfdc = []
                print 'appending'
                sfdc.append(newActivity)
                print 'appended'
                existingLead['activities']['sfdc'] = sfdc
                existingLead.save()
                print 'saved new activity 2'

    except Exception as e:
        print 'exception  while saving SFDC Contact history to master' + str(e)
        send_notification(dict(
         type='error',
         success=False,
         message=str(e)
        ))             
Пример #3
0
def saveMktoActivitiesToMaster(user_id=None, company_id=None, job_id=None, run_type=None): 
    #job_id = ObjectId("56a2dd408afb006f9e7cb851") 
    
    
    if run_type == 'initial':   
        #activities = TempData.objects(Q(company_id=company_id) & Q(record_type='activity') & Q(source_system='mkto') & Q(job_id=job_id) ).only('source_record') 
        collection = TempData._get_collection()
        activities = collection.find({'company_id': int(company_id), 'record_type': 'activity', 'source_system': 'mkto', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
        
    else:
        collection = TempDataDelta._get_collection()
        activities = collection.find({'company_id': int(company_id), 'record_type': 'activity', 'source_system': 'mkto', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
        
        #activities = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='activity') & Q(source_system='mkto') & Q(job_id=job_id) ).only('source_record') 
    
    
#     activityListTemp = list(activities)
#     activityList = [i['source_record'] for i in activityListTemp]
    
    existingIntegration = CompanyIntegration.objects(company_id = company_id).first()
    if existingIntegration is not None:
        activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity']
    else:
        print 'No activity type metadata found for Marketo'
        raise ValueError('No activity type metadata found for Marketo')
    
    try: 
        for i in range(len(activityTypeArray)):
            if activityTypeArray[i]['name'] == 'Change Data Value':
                changeActivityId = activityTypeArray[i]['id']
        print 'change id is ' + str(changeActivityId)
        
        for activity in activities: 
            
            newActivity = activity['source_record']
            addThisActivity = True
            
            #company_id = request.user.company_id
            mkto_id = str(newActivity['leadId'])
            print 'doing lead ' + mkto_id
            
            existingLead = Lead.objects(Q(mkto_id = str(mkto_id)) & Q(company_id = company_id)).first()
            if existingLead is not None: # we found this lead to attach the activities
                if 'mkto' in existingLead.activities:
                    currentActivities = existingLead.activities['mkto']        
                    for i in range(len(currentActivities)):
                        if currentActivities[i]['id'] == newActivity['id']: #check if this activity already exists in the lead dict
                            addThisActivity = False
                            break
                    if addThisActivity == True:
                        for i in range(len(activityTypeArray)):
                            if activityTypeArray[i]['id'] == newActivity['activityTypeId']:
                                newActivity['activityTypeName'] = activityTypeArray[i]['name']
                                break
                        currentActivities.append(newActivity)
                        existingLead.update(activities__mkto = currentActivities)
                        
                else:
                    currentActivities = []
                    for i in range(len(activityTypeArray)):
                        if activityTypeArray[i]['id'] == newActivity['activityTypeId']:
                            newActivity['activityTypeName'] = activityTypeArray[i]['name']
                            break
                    currentActivities.append(newActivity)
                    existingLead.update(activities__mkto = currentActivities)
                #addThisActivity == True and    
                if addThisActivity == True and newActivity['activityTypeId'] == changeActivityId and newActivity['primaryAttributeValue'] == 'Lead Status':
                    print 'processing status activity for id ' + mkto_id
                    #statusRecord = [];
                    newStatus = ''
                    oldStatus = ''
                    for attribute in newActivity['attributes']:
                        if attribute['name'] == 'New Value':
                            newStatus = attribute['value']
                        elif attribute['name'] == 'Old Value':
                            oldStatus = attribute['value']
#                         elif attribute['name'] == 'Reason':
#                             reason = attribute['value']    
                            #break
                    #statusRecord.append({'status': newStatus, 'date': newActivity['activityDate']})
                    newActivity['newStatus'] = newStatus 
                    newActivity['oldStatus'] = oldStatus 
                    newActivity['date'] = newActivity['activityDate']
                    if 'mkto' in existingLead.statuses:
                        currentStatuses = existingLead.statuses['mkto']
                        currentStatuses.append(newActivity) # changed on 1/22/2016 {'status': newStatus, 'date': newActivity['activityDate']})
                        existingLead.update(statuses__mkto = currentStatuses)
                    else:
                        currentStatuses = []
                        currentStatuses.append(newActivity) # changed on 1/22/2016{'status': newStatus, 'date': newActivity['activityDate']})
                        existingLead.update(statuses__mkto = currentStatuses)
                        
#                 if addThisActivity == True: # this activity was not foudn in the lead, so add it
#                     existingLead.activities['mkto'].append(newActivity)
#                 existingLead.save() # no concept of saving the activity if the lead does not exist
    except Exception as e:
            send_notification(dict(
             type='error',
             success=False,
             message=str(e)
            ))