def saveSfdcCampaignsToMaster(user_id=None, company_id=None, job_id=None, run_type=None): #job_id = ObjectId("56d25c6af6fd2a15df46cd60") if run_type == 'initial': campaigns = TempData.objects(Q(company_id=company_id) & Q(record_type='campaign') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) else: campaigns = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='campaign') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) campaignListTemp = list(campaigns) campaignList = [i['source_record'] for i in campaignListTemp] try: for newCampaign in campaignList: #['records']: #company_id = request.user.company_id guid = 'sfdc_' + str(newCampaign['Id']) Campaign.objects(Q(guid = guid) & Q(company_id=company_id)).modify(upsert=True, new=True, set__campaigns__sfdc = newCampaign, set__updated_date = datetime.utcnow, set_on_insert__source_system = 'sfdc', set_on_insert__guid = guid, set_on_insert__company_id = company_id) # for oldCampaign in campaignList['records']: # # mktoCampaigns = [] # # mktoCampaigns.append(campaign) # campaign = Campaign() # campaign.company_id = request.user.company_id # campaign.derived_id = 'sfdc_' + str(oldCampaign['Id']) # campaign.campaigns["sfdc"] = oldCampaign # campaign.save() except Exception as e: print 'error while saving sfdc campaign: ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def associateEmailWithCampaign(name, channel, company_id, email_id, job_id, run_type): '''Associate an email template with a Hubspot campaign - from website traffic because there is no direct API method to extract campaigns ''' try: existingCampaign = Campaign.objects(Q(name=name) & Q(channel=channel) & Q(company_id=company_id) & Q(source_system='hspt')).first() if existingCampaign is None: return #this should not happen else: emails = existingCampaign['emails'] if emails is None: #create a new email for the campaign _doAssociationEmailWithCampaign(email_id, existingCampaign, company_id, job_id, run_type) else: email_exists = False for email in emails: emailDetails = None if email['id'] == email_id: #if email exists, only update the details email_exists = True emailDetails = _get_hspt_email_details(email_id, existingCampaign, company_id, job_id, run_type) email['details'] = emailDetails existingCampaign.save() print 'existing email updated' if email_exists == False: #else, create a new email for the campaign _doAssociationEmailWithCampaign(email_id, existingCampaign, company_id, job_id, run_type) except Exception as e: print 'error while associating email with Hspt campaign from traffic ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def saveHsptCampaignsToMaster(user_id=None, company_id=None, job_id=None, run_type=None): if run_type == 'initial': campaigns = TempData.objects(Q(company_id=company_id) & Q(record_type='campaign') & Q(source_system='hspt') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) else: campaigns = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='campaign') & Q(source_system='hspt') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) campaignListTemp = list(campaigns) campaignList = [i['source_record'] for i in campaignListTemp] try: for newCampaign in campaignList: #company_id = request.user.company_id #derived_id = 'hspt_' + str(newCampaign['id']) guid = newCampaign['guid'] name = newCampaign['name'] source_system = 'hspt' channel = 'email' emails = newCampaign['emails'] #save all email events for this campaign first saveHsptCampaignEmailEventRecords(company_id=company_id, run_type=run_type, job_id=job_id, guid=guid) #now update or create campaign record existingCampaign = Campaign.objects(Q(guid = guid) & Q(company_id=company_id) & Q(source_system=source_system)).first() #.modify(upsert=True, new=True, set__emails = emails, set_on_insert__name = name, set_on_insert__guid = guid, set_on_insert__company_id = company_id, set_on_insert__updated_date=datetime.utcnow) if existingCampaign is None: #new campaign so add it newCampaign = Campaign(emails = emails, name = name, guid = guid, company_id = company_id, updated_date=datetime.utcnow, source_system=source_system) newCampaign.save() else: #campaign already exists so update relevant fields #print 'existing campaign is ' + str(existingCampaign) if 'emails' not in existingCampaign: #should never happen continue #print 'before emails ' + str(emails) for email in emails: #loop through each email found in API call for this campaign #save events separately #print 'entering emails' #now create/update the campaign record email_found = False for existingEmail in existingCampaign['emails']: #check if the API email already exists if email['id'] == existingEmail['id']: #we found this email already existing so update specific fields email_found = True existingEmail['stats'] = email['stats'] existingEmail['details'] = email['details'] if email_found: existingCampaign.save() break #move to the next API email since we already found a match if email_found == False: #email was not found so add it to existing emails existingCampaign['emails'].append(email) existingCampaign.save() except Exception as e: print 'exception ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def getCount(request, id): object = request.GET.get('object') try: if object == 'lead': result = {'count' : Lead.objects(company_id = id).count()} elif object == 'campaign': result = {'count' : Campaign.objects(company_id = id).count()} else: result = 'Nothing to count' return JsonResponse(result, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
def createHsptCampaignFromTraffic(name, channel, company_id): '''Create campaigns for Hubspot from website traffic because there is no direct API method to extract campaigns ''' try: existingCampaign = Campaign.objects(Q(name=name) & Q(channel=channel) & Q(company_id=company_id) & Q(source_system='hspt')).first() if existingCampaign is None: newCampaign = Campaign(name=name, channel=channel, company_id=company_id, source_system='hspt') newCampaign.save() except Exception as e: print 'error while saving Hspt campaign from traffic ' + str(e) send_notification(dict( type='error', success=False, message=str(e) ))
def getCampaigns(request, id): try: company_id = request.user.company_id system_type = request.GET.get('system_type') page_number = int(request.GET.get('page_number')) items_per_page = int(request.GET.get('per_page')) start_date = int(request.GET.get('start_date')) end_date = int(request.GET.get('end_date')) sub_view = request.GET.get('subview') filters = request.GET.get('filters') filters = json.loads(filters) superfilters = request.GET.get('superfilters') super_filters = json.loads(superfilters) #print 'super filters are ' + str(super_filters) date_field = None querydict_filters = {} offset = (page_number - 1) * items_per_page # existingIntegration = CompanyIntegration.objects(company_id = company_id ).first() # code = None # if existingIntegration is not None: # for source in existingIntegration.integrations.keys(): # defined_system_type = SuperIntegration.objects(Q(code = source) & Q(system_type = system_type)).first() # if defined_system_type is not None: # code = source # # if code is None: # return JsonResponse({'Error' : 'Marketing Automation system not found'}) #projection = {'$project': {'_id': '$campaigns.' + code + '.id', 'created_date': '$campaigns.' + code + '.createdAt', 'name': '$campaigns.' + code + '.name', 'description': '$campaigns.' + code + '.description', 'url': '$campaigns.' + code + '.url', 'type': '$campaigns.' + code + '.type', 'channel': '$campaigns.' + code + '.channel', } } match = {'$match' : { }} date_field = None collection = Campaign._get_collection() company_field_qry = 'company_id' querydict = {company_field_qry: company_id} code = '' if super_filters is not None: if 'date_types' in super_filters: # need to filter by a certain type of date date_field = super_filters['date_types'] if date_field is not None: if start_date is not None: start_date = datetime.fromtimestamp(float(start_date) / 1000) if end_date is not None: end_date = datetime.fromtimestamp(float(end_date) / 1000) local_start_date = get_current_timezone().localize(start_date, is_dst=None) utc_day_start = local_start_date.astimezone(pytz.timezone('UTC')) utc_day_start_string = datetime.strftime(utc_day_start, '%Y-%m-%dT%H:%M:%SZ+0000') utc_day_start_string_crm = datetime.strftime(utc_day_start, '%Y-%m-%dT%H:%M:%S.000+0000') local_end_date = get_current_timezone().localize(end_date, is_dst=None) utc_day_end = local_end_date.astimezone(pytz.timezone('UTC')) utc_day_end_string = datetime.strftime(utc_day_end, '%Y-%m-%dT%H:%M:%SZ+0000') utc_day_end_string_crm = datetime.strftime(utc_day_end, '%Y-%m-%dT%H:%M:%S.000+0000') #print 'utc start string is ' + str(utc_day_start_string) #print 'utc end string is ' + str(utc_day_end_string) #remove the date_types item #super_filters.pop('date_types') date_field_original = date_field date_field = date_field.replace('.', '__') date_field_start_qry = date_field + '__gte' date_field_end_qry = date_field + '__lte' if filters is not None: for key, value in filters.items(): if value is not None and value != '': querydict_filters['campaigns__' + code + '__' + key] = value #creates an additional querydict that can be added to the main qd match['$match']['campaigns.' + code + '.' + key] = value if sub_view == 'allcampaigns': if date_field is None: total = collection.find({'company_id': int(company_id)}).count() #.hint('company_id_1') queryset = Campaign.objects(**querydict).skip(offset).limit(items_per_page) else: total = collection.find({'company_id': int(company_id), date_field_original: {'$gte':utc_day_start_string, '$lte':utc_day_end_string}}).count() #.hint('company_id_1') querydict[date_field_start_qry] = utc_day_start_string querydict[date_field_end_qry] = utc_day_end_string queryset = Campaign.objects(**querydict).skip(offset).limit(items_per_page) elif sub_view == 'onlyma' or sub_view == 'onlycrm': if sub_view == 'onlyma': code = _get_system(company_id, 'MA') else: code = _get_system(company_id, 'CRM') if code is None: return JsonResponse({'Error' : 'No source system found'}) querydict['source_system'] = code if date_field is None: total = collection.find({'company_id': int(company_id), 'source_system': code}).count() #.hint('company_id_1') queryset = Campaign.objects(**querydict).skip(offset).limit(items_per_page) else: if sub_view == 'onlycrm' and code == 'sfdc': if date_field_original == 'campaigns.mkto.createdAt': date_field_original = 'campaigns.sfdc.CreatedDate' date_field_start_qry = 'campaigns__sfdc__CreatedDate__gte' date_field_end_qry = 'campaigns__sfdc__CreatedDate__lte' elif date_field_original == 'campaigns.mkto.updatedAt': date_field_original = 'campaigns.sfdc.LastModifiedDate' date_field_start_qry = 'campaigns__sfdc__LastModifiedDate__gte' date_field_end_qry = 'campaigns__sfdc__LastModifiedDate__lte' total = collection.find({'company_id': int(company_id), 'source_system': code, date_field_original: {'$gte':utc_day_start_string, '$lte':utc_day_end_string}}).count() #.hint('company_id_1') querydict[date_field_start_qry] = utc_day_start_string querydict[date_field_end_qry] = utc_day_end_string queryset = Campaign.objects(**querydict).skip(offset).limit(items_per_page) elif sub_view == 'crmfromma' or sub_view == 'crmnotma': code = _get_system(company_id, 'CRM') if code is None: return JsonResponse({'Error' : 'No source system found'}) querydict['source_system'] = code mapping = CompanyIntegration.objects(company_id=company_id).only('mapping').first() print 'mapping is ' + str(mapping) if mapping is None or len(mapping) == 0: return JsonResponse({'Error' : 'No mapping found in company settings'}) ma_user = None ma_code = _get_system(company_id, 'MA') if ma_code == 'mkto': ma_user = mapping['mapping'].get('mkto_sync_user', None) if ma_user is None or ma_code is None: return JsonResponse({'Error' : 'No marketing automation details found'}) if code == 'sfdc': if sub_view == 'crmfromma': user_field_qry = 'campaigns.sfdc.CreatedById' querydict['campaigns__sfdc__CreatedById'] = ma_user else: user_field_qry = 'campaigns.sfdc.CreatedById__ne' querydict['campaigns__sfdc__CreatedById__ne'] = ma_user if date_field is None: total = collection.find({'company_id': int(company_id), 'source_system': code, user_field_qry: ma_user}).count() #.hint('company_id_1') queryset = Campaign.objects(**querydict).skip(offset).limit(items_per_page) else: if date_field_original == 'campaigns.mkto.createdAt': date_field_original = 'campaigns.sfdc.CreatedDate' date_field_start_qry = 'campaigns__sfdc__CreatedDate__gte' date_field_end_qry = 'campaigns__sfdc__CreatedDate__lte' elif date_field_original == 'campaigns.mkto.updatedAt': date_field_original = 'campaigns.sfdc.LastModifiedDate' date_field_start_qry = 'campaigns__sfdc__LastModifiedDate__gte' date_field_end_qry = 'campaigns__sfdc__LastModifiedDate__lte' total = collection.find({'company_id': int(company_id), 'source_system': code, user_field_qry: ma_user, date_field_original: {'$gte':utc_day_start_string, '$lte':utc_day_end_string}}).count() #.hint('company_id_1') querydict[date_field_start_qry] = utc_day_start_string querydict[date_field_end_qry] = utc_day_end_string queryset = Campaign.objects(**querydict).skip(offset).limit(items_per_page) serializer = CampaignSerializer(queryset, many=True) type = 'campaigns' return JsonResponse({'count' : total, 'results': serializer.data, 'type': type}) except Exception as e: return JsonResponse({'Error' : str(e)})