def retrieveSfdcCampaigns(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: sdfc = Salesforce() campaignList = sdfc.get_campaigns_delta(user_id, company_id, _str_from_date(sinceDateTime), run_type) saveSfdcCampaigns(user_id=user_id, company_id=company_id, campaignList=campaignList, job_id=job_id, run_type=run_type) try: message = 'Campaigns retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Campaigns' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return campaignList except Exception as e: print 'error while retrieving sfdc campaigns: ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def retrieveMktoCampaigns(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: mkto = Marketo(company_id) campaignList = mkto.get_programs() saveMktoCampaigns(user_id=user_id, company_id=company_id, campaignList=campaignList, job_id=job_id, run_type=run_type) try: message = 'Campaigns retrieved from Marketo' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Campaigns' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return campaignList except Exception as e: print 'error while retrieving marketo campaigns: ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def retrieveSfdcAccounts(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: if sinceDateTime is None: sinceDateTime = (datetime.now() - timedelta(days=1)).date() sfdc = Salesforce() accountList = sfdc.get_accounts_delta(user_id, company_id, _str_from_date(sinceDateTime), run_type) print 'got back accounts ' + str(len(accountList['records'])) saveSfdcAccounts(user_id=user_id, company_id=company_id, accountList=accountList, job_id=job_id, run_type=run_type) try: message = 'Accounts retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Accounts' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return accountList except Exception as e: send_notification(dict(type='error', success=False, message=str(e)))
def retrieveSfdcContacts(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: sdfc = Salesforce() if sinceDateTime is None: sinceDateTime = (datetime.now() - timedelta(days=30)).date() contactList = sdfc.get_contacts_delta(user_id, company_id, _str_from_date(sinceDateTime), run_type) print "got back contacts " + str(len(contactList["records"])) saveSfdcContacts( user_id=user_id, company_id=company_id, contactList=contactList, job_id=job_id, run_type=run_type ) try: message = "Contacts retrieved from Salesforce" notification = Notification() # notification.company_id = company_id notification.owner = user_id notification.module = "Contacts" notification.type = "Background task" notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict(type="error", success=False, message=str(e))) return contactList except Exception as e: send_notification(dict(type="error", success=False, message=str(e)))
def retrieveSfdcOppStageHistory(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #needs to be changed - satya try: #delete later #job_id_new = job_id #job_id = ObjectId("56a690e98afb006883048e7e") #set variables sfdc = Salesforce() #for leads if run_type == 'initial': opps = TempData.objects(Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & else: opps = TempDataDelta.objects(Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & oppListTemp = list(opps) if not oppListTemp: print 'no opps found' return oppList = [i['source_record'] for i in oppListTemp] batch_size = 500 #10 Activity Types at a time activitiesList = [] for i in range(0, len(oppList), batch_size): opp_list = '(' for opp in oppList[i:i+batch_size]: opp_list += '\'' + opp['Id'] + '\'' + ', ' opp_list = opp_list[:-2] opp_list += ')' activitiesList.extend(sfdc.get_stage_history_for_opportunity(user_id, company_id, opp_list, _str_from_date(sinceDateTime))) print 'got back stage history for SFDC opportunities ' + str(len(activitiesList)) #delete later #job_id = job_id_new saveSfdcOppStageHistory(user_id=user_id, company_id=company_id, activityList=activitiesList, job_id=job_id, run_type=run_type) try: message = 'Opportunity stage history retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Opportunities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return opp_list except Exception as e: print 'exception while retrieving SFDC opportunity stage history: ' + str(e) send_notification(dict(type='error', success=False, message=str(e)))
def retrieveHsptOpportunities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: print 'retrieving Hspt deals' hspt = Hubspot(company_id) oppList = [] # if run_type == 'initial': # oppList = hspt.get_deals(count=10000) # else: # oppList = hspt.get_deals(count=1000) # print 'got opps ' + str(len(oppList['results'])) # saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type) #print 'job id is ' + str(job_id) if run_type == 'initial': print 'initial run for opps' try: for opp in hspt.get_deals(count=10000)['results']: oppList.append(opp) if len(oppList) == 100: #print 'going to save' saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type) oppList = [] except Exception as e: print 'exception: ' + str(e) else: try: for opp in hspt.get_deals(count=1000)['results']: oppList.append(opp) if len(oppList) == 100: saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type) oppList = [] except Exception as e: print 'exception: ' + str(e) try: message = 'Opportunities retrieved from Hubspot' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Opportunities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return oppList except Exception as e: send_notification(dict(type='error', success=False, message=str(e)))
def retrieveSfdcOpportunities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: #company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id).first() # code commented out since we are no longer getting only Mkto related opportunities into Cx # if existingIntegration is not None and 'mkto' in existingIntegration['integrations']: # if this system is connected to Marketo # company_qry = 'company_id' # type_field_qry = 'leads__mkto__exists' # sfdc_account_field_qry = 'leads__mkto__sfdcAccountId__ne' # querydict = {company_qry: company_id, type_field_qry: True, sfdc_account_field_qry: None} # leads_with_sfdc_opps = Lead.objects(**querydict).only('mkto_id').only('leads__mkto__sfdcAccountId') # sfdc = Salesforce() # code commented out since we are no longer getting only Mkto related opportunities into Cx # account_list = '(' # for lead in leads_with_sfdc_opps: # account_list += '\'' + lead['leads']['mkto']['sfdcAccountId'] + '\'' + ', ' # account_list = account_list[:-2] # account_list += ')' if sinceDateTime is None: sinceDateTime = (datetime.now() - timedelta(days=30)).date() oppList = sfdc.get_opportunities_delta(user_id, company_id, _str_from_date(sinceDateTime), run_type) print 'got opps ' + str(len(oppList['records'])) #create list of Opp IDs to send for get_contacts call oppid_list = '(' for opp in oppList['records']: oppid_list += '\'' + opp['Id'] + '\'' + ', ' oppid_list = oppid_list[:-2] oppid_list += ')' contactList = sfdc.get_contacts_for_opportunities(user_id, company_id, oppid_list) # needed because SFDC does not have the Contact ID within the Opp record print 'got contacts for opps ' + str(len(contactList['records'])) saveSfdcOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, contactList=contactList, job_id=job_id, run_type=run_type) try: message = 'Opportunities retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Opportunities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return oppList except Exception as e: send_notification(dict(type='error', success=False, message=str(e)))
def retrieveBufrTwInteractions(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: print 'starting retrieveBufrTwInteractions for company ' + str(company_id) existingIntegration = CompanyIntegration.objects(company_id = company_id).first() if 'bufr' in existingIntegration['integrations']: # if Buffer is present and configured print 'found buffer' client_id = existingIntegration['integrations']['bufr']['client_id'] client_secret = existingIntegration['integrations']['bufr']['client_secret'] access_token = existingIntegration['integrations']['bufr']['access_token'] buffer = Buffer() api = Buffer.get_api(buffer, client_id=client_id, client_secret=client_secret, access_token=access_token) profiles = Buffer.get_twitter_profiles(buffer, api) for profile in profiles: results = buffer.get_twitter_updates(profile) saveBufrTwInteractions(user_id=user_id, company_id=company_id, results=results, job_id=job_id, run_type=run_type) #print 'Tw results are ' + str(results) else: print 'No integration found with Buffer' return JsonResponse({'error' : 'No integration found with Buffer'}) try: message = 'Twitter interactions retrieved from Buffer' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Social' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) ))
def exportToPdf(company_id, user_id, template_name, source_type, content_type, phantomjs_script, url, token, sessionid, authenticatedAccount, file_name): #, output, error try: user_id = ObjectId(user_id) output = NamedTemporaryFile(delete=False) error = NamedTemporaryFile(delete=False) external_process = Popen(["phantomjs", phantomjs_script, url, token, sessionid, authenticatedAccount, file_name], stdout=output, stderr=error) # external_process.communicate(30) export_file = open(file_name, 'rb') exportFile = ExportFile(company_id=company_id, owner_id=user_id, source=template_name, source_type=source_type, type=content_type, file_name=os.path.basename(file_name)) exportFile.file.put(export_file, content_type=content_type) exportFile.save() try: message = 'PDF file successfully exported' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your export of data from ' + template_name + ' is ready. It is available in My Exports with the file name ' + os.path.basename(file_name) + '.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Your PDF export is baked and ready', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) except Exception as e: print 'exception was ' + str(e) return str(e)
def retrieveMktoActivities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #return try: print 'getting mkto activities' #company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id).first() activityTypeIds = [] # if existingIntegration is not None: # activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] # for i in range(len(activityTypeArray)): # if (activityTypeArray[i]['name'] == 'Send Email' or activityTypeArray[i]['name'] == 'Email Delivered' or activityTypeArray[i]['name'] == 'Open Email' or activityTypeArray[i]['name'] == 'Visit Webpage' or activityTypeArray[i]['name'] == 'Fill out Form' or activityTypeArray[i]['name'] == 'Click Link' or activityTypeArray[i]['name'] == 'Email Bounced' or activityTypeArray[i]['name'] == 'Email Unsubscribed' or activityTypeArray[i]['name'] == 'Change Data Value'): # activityTypeIds.append(str(activityTypeArray[i]['id'])) if existingIntegration is not None: activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] for i in range(len(activityTypeArray)): activityTypeIds.append(str(activityTypeArray[i]['id'])) if activityTypeIds is None: return [] if sinceDateTime is None: sinceDateTime = datetime.now() - timedelta(days=30) #change to 365 mkto = Marketo(company_id) #get the 'Claritix Lead List' in order to only get activities for leads in that list listList = mkto.get_lists(id=None , name=['Claritix Leads List'], programName=None, workspaceName=None, batchSize=None) if listList and listList[0]: leadListId = listList[0]['id'] else: raise ValueError('Claritix Leads List not found') batch_size = 10 #10 Activity Types at a time activityList = [] for i in range(0, len(activityTypeIds), batch_size): activityTypeIdsTemp = activityTypeIds[i:i+batch_size] print 'gettng activities for ' + str(activityTypeIdsTemp) activityList.extend(mkto.get_lead_activity(activityTypeIdsTemp, sinceDatetime=sinceDateTime, leadListId=leadListId)) #delete leads from lead list in Marketo deleteList = mkto.get_leads_by_listId(listId=leadListId) deleteLeadIds = [str(e['id']) for e in deleteList] print 'leads to be removed from CX List are ' + str(deleteLeadIds) batch_size = 300 for i in range(0, len(deleteLeadIds), batch_size): mkto.remove_leads_from_list(listId=leadListId, leadsIds = deleteLeadIds[i:i+batch_size]) print 'leads removed from Mkto CX List' print 'going to save mkto activities - count ' + str(len(activityList)) saveMktoActivities(user_id=user_id, company_id=company_id, activityList=activityList, activityTypeArray=activityTypeArray, job_id=job_id, run_type=run_type) try: message = 'Activities retrieved from Marketo' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Activities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return activityList except Exception as e: print 'Error while retrieving activities from Marketo ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def retrieveSfdcLeadHistory(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #needs to be changed - satya try: #delete later #job_id_new = job_id #job_id = ObjectId("56a690e98afb006883048e7e") #set variables sfdc = Salesforce() #for leads if run_type == 'initial': leads = TempData.objects(Q(record_type='lead') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & else: leads = TempDataDelta.objects(Q(record_type='lead') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & leadListTemp = list(leads) if not leadListTemp: print 'no leads found' return leadList = [i['source_record'] for i in leadListTemp] batch_size = 500 #10 Activity Types at a time activitiesList = [] for i in range(0, len(leadList), batch_size): lead_list = '(' for lead in leadList[i:i+batch_size]: lead_list += '\'' + lead['Id'] + '\'' + ', ' lead_list = lead_list[:-2] lead_list += ')' activitiesList.extend(sfdc.get_history_for_lead(user_id, company_id, lead_list, _str_from_date(sinceDateTime))) print 'got back history for SFDC leads ' + str(len(activitiesList)) #delete later #job_id = job_id_new saveSfdcLeadHistory(user_id=user_id, company_id=company_id, activityList=activitiesList, job_id=job_id, run_type=run_type) # #for contacts # if run_type == 'initial': # contacts = TempData.objects(Q(record_type='contact') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & # else: # contacts = TempDataDelta.objects(Q(record_type='contact') & Q(source_system='sfdc') & Q(job_id=job_id)) #Q(job_id=job_id) & # # contactListTemp = list(contacts) # contactList = [i['source_record'] for i in contactListTemp] # # sfdc = Salesforce() # # batch_size = 500 #10 Activity Types at a time # activitiesList = [] # # for i in range(0, len(contactList), batch_size): # contact_list = '(' # for contact in contactList[i:i+batch_size]: # contact_list += '\'' + contact['Id'] + '\'' + ', ' # contact_list = contact_list[:-2] # contact_list += ')' # activitiesList.extend(sfdc.get_history_for_contact(user_id, company_id, contact_list, _str_from_date(sinceDateTime))) # # print 'got back history for SFDC contacts ' + str(len(activitiesList)) # saveSfdcHistory(user_id=user_id, company_id=company_id, activityList=activitiesList, job_id=job_id, run_type=run_type) # try: message = 'Lead history retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Leads' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return leadList except Exception as e: print 'exception while retrieving SFDC lead history: ' + str(e) send_notification(dict(type='error', success=False, message=str(e)))
def retrieveMktoLeadCreatedActivities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): #return try: print 'getting mkto lead created activities' # #company_id = request.user.company_id # existingIntegration = CompanyIntegration.objects(company_id = company_id).first() # activityTypeIds = [] # # if existingIntegration is not None: # # activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] # # for i in range(len(activityTypeArray)): # # if (activityTypeArray[i]['name'] == 'Send Email' or activityTypeArray[i]['name'] == 'Email Delivered' or activityTypeArray[i]['name'] == 'Open Email' or activityTypeArray[i]['name'] == 'Visit Webpage' or activityTypeArray[i]['name'] == 'Fill out Form' or activityTypeArray[i]['name'] == 'Click Link' or activityTypeArray[i]['name'] == 'Email Bounced' or activityTypeArray[i]['name'] == 'Email Unsubscribed' or activityTypeArray[i]['name'] == 'Change Data Value'): # # activityTypeIds.append(str(activityTypeArray[i]['id'])) # # if existingIntegration is not None: # activityTypeArray = existingIntegration.integrations['mkto']['metadata']['activity'] # for i in range(len(activityTypeArray)): # activityTypeIds.append(str(activityTypeArray[i]['id'])) # # if activityTypeIds is None: # return [] activityTypeIds = ['12'] #hard coded Mkto Activity Type for 'New Lead' activityTypeArray = [] # redundant - should be removed later if sinceDateTime is None: sinceDateTime = datetime.now() - timedelta(days=30) #change to 365 mkto = Marketo(company_id) batch_size = 10 #10 Activity Types at a time activityList = [] for i in range(0, len(activityTypeIds), batch_size): activityTypeIdsTemp = activityTypeIds[i:i+batch_size] print 'gettng activities for ' + str(activityTypeIdsTemp) activityList.extend(mkto.get_lead_activity(activityTypeIdsTemp, sinceDatetime=sinceDateTime)) print 'going to save mkto lead created activities - count ' + str(len(activityList)) saveMktoActivities(user_id=user_id, company_id=company_id, activityList=activityList, activityTypeArray=activityTypeArray, job_id=job_id, run_type=run_type) try: message = 'Lead Created Activities retrieved from Marketo' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Activities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return activityList except Exception as e: send_notification(dict( type='error', success=False, message=str(e) ))
def exportToCsv(object_type, system_code, data, source_type, chart_name, user_id, company_id): print 'in export to csv' if object_type is None or system_code is None or data is None: print 'returning due to none' return try: if object_type == 'lead': result = exportLeadsToCsv(system_code, data, chart_name, user_id, company_id) print 'got result ' + str(result) file_name = result['file_name'] if file_name != '': content_type = result['content_type'] export_file = open(file_name, 'rb') exportFile = ExportFile(company_id=company_id, owner_id=user_id, source=chart_name, source_type=source_type, type=content_type, file_name=os.path.basename(file_name)) exportFile.file.put(export_file, content_type=content_type) exportFile.save() try: message = 'CSV file successfully exported' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your download of data from ' + chart_name + ' is ready. It is available in My Exports with the file name ' + os.path.basename(file_name) + '.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Your CSV export is baked and ready', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) else: try: message = 'CSV download failed' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your download of data from ' + chart_name + ' failed. Please contact the Claritix team so that we can look into this.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Oh no! Your CSV download failed', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) else: return except Exception as e: print 'exception while trying to save CSV file: ' + str(e) send_notification(dict(type='error', success=False, message=str(e)))
def retrieveHsptCampaigns(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): '''remember that campaigns in HSPT API terms refers to email templates! here, we use actualCampaign to represent the Campaign and campaign to refer to email templates ''' try: actualCampaigns = [] actualCampaignsDict = {} actualCampaignsTempList = [] utc_day_start_epoch = calendar.timegm(sinceDateTime.timetuple()) * 1000 #use calendar.timegm and not mktime because of UTC utc_day_start_epoch = str('{0:f}'.format(utc_day_start_epoch).rstrip('0').rstrip('.')) print 'utc start epoch is ' + str(utc_day_start_epoch) utc_day_end_epoch = calendar.timegm(datetime.now().timetuple()) * 1000 utc_day_end_epoch = str('{0:f}'.format(utc_day_end_epoch).rstrip('0').rstrip('.')) print 'utc end epoch is ' + str(utc_day_end_epoch) hspt = Hubspot(company_id) if run_type == 'initial': campaignList = hspt.get_all_campaigns() #campaignList = hspt.get_recent_campaigns() else: campaignList = hspt.get_recent_campaigns() #print 'campaign list has entries' + str(len(campaignList['results'])) #print 'campaign list is ' + str(campaignList) #campaignIds = [i['id'] for i in campaignList['results']] for i in campaignList['results']: #if i['id'] != 23202074: # continue campaignStats = hspt.get_campaign_stats(i['id']) i['stats'] = campaignStats.get('stats', None) campaignDetails = hspt.get_campaign_details(campaignId=i['id'], appId=i['appId']) i['details'] = campaignDetails if 'contentId' not in campaignDetails: continue contentId = campaignDetails['contentId'] email_content = hspt.get_email_by_content_id(contentId) #get events #i['events'] = {} email_events = hspt.get_campaign_events(i['id'], i['appId'], utc_day_start_epoch, utc_day_end_epoch) #i['events'] = email_events #print 'email events are ' + str(email_events) #process each event to add to lead record #for event in i['events']: #set other variables i['name'] = email_content['name'] i['created'] = email_content['created'] i['last_event_timestamp'] = 0 #print 'guid is ' + str(email_content['campaign']) #if campaign GUID found, do actualCampaign stuff if 'campaign' in email_content and email_content['campaign'] is not None and email_content['campaign'] != "": if email_content['campaign'] not in actualCampaignsTempList: #first time this actualcampaign is being encountered #print 'campaign not found' actualCampaignsTempList.append(email_content['campaign']) #print '1' actualCampaignsDict[email_content['campaign']] = {} actualCampaignsDict[email_content['campaign']]['guid'] = email_content['campaign'] actualCampaignsDict[email_content['campaign']]['name'] = email_content['campaign_name'] #print '2' actualCampaignsDict[email_content['campaign']]['emails'] = [] actualCampaignsDict[email_content['campaign']]['emails'].append(i) #print '3' #print '1st dict is ' + str(actualCampaignsDict) else: #this actualcampaign has already been found before so add this email template to it #print 'campaign exists ' + str(actualCampaignsDict) actualCampaignsDict[email_content['campaign']]['emails'].append(i) #save email events separately to prevent Mongo size error #print 'email events is ' + str(email_events) if email_events is None: continue for eventType, events in email_events.iteritems(): for event in events: event_record = {'company_id': company_id, 'source_system':'hspt', 'campaign_guid': email_content['campaign'], 'email_id': i['id'], 'event_id': event['id'], 'event_type': eventType, 'created': event['created'], 'recipient': event['recipient'], 'details': event} saveHsptCampaignEmailEvent(user_id=user_id, company_id=company_id, event=event_record, job_id=job_id, run_type=run_type) #break #now that all email templates have been processed, iterate through the dict to fill the final array #print 'dict is ' + str(actualCampaignsDict) for key, value in actualCampaignsDict.iteritems(): actualCampaigns.append(value) #print 'campaigns result is ' + str(actualCampaigns) #with the content id, find the actual Campaign GUID and Name #campaignEventsTemp = hspt.get_campaign_events(campaignId=i['id'], appId=i['appId']) #i['events'] = campaignEventsTemp.get('results', None) # #campaignContacts = hspt.get_campaign_contacts(i['id']) # #i['contacts] = # #print 'campaign contacts are ' + str(campaignContacts) for campaign in actualCampaigns: saveHsptCampaigns(user_id=user_id, company_id=company_id, campaign=campaign, job_id=job_id, run_type=run_type) try: message = 'Email templates (Campaigns) retrieved from Hubspot' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Campaigns' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return campaignList except Exception as e: print 'error while retrieving hspt campaigns: ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))