def add_widget(request): if request.method == 'POST': urlform = AddFromURLForm(request.POST) if urlform.is_valid(): url = urlform.cleaned_data['url'] cid = corg.get_petition_id(url) petition = corg.get_petition(cid) print petition c = Campaign() c.change_id = petition['petition_id'] c.title = petition['title'] c.url = petition['url'] c.overview=petition['overview'] c.signature_count=petition['signature_count'] c.image_url=petition['image_url'] try: c.save() return render_to_response("campaigns/thanks.html",{}, context_instance=RequestContext(request)) except IntegrityError: errors =urlform._errors.setdefault("url", ErrorList()) errors.append(u"Someone already submitted this campaign") else: urlform = AddFromURLForm() c={ "urlform": urlform } c.update(csrf(request)) return render_to_response("campaigns/add_widget.html",c, context_instance=RequestContext(request))
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 test_campaign_is_related_to_user(self): user_ = User.objects.create_user('Flumphy', '*****@*****.**', 'dire565Flumpher') user_.save() _campaign = Campaign() _campaign.Name = 'Test of the Flumph' _campaign.save() _campaign.Users.add(user_) _campaign.save() self.assertIn(_campaign, user_.campaigns.all())
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 new_campaign(request): if request.is_ajax(): if (request.POST.get("author") == request.user.username): c = Campaign() c.creator = request.user c.owner = request.user c.title = request.POST.get("title") c.description = request.POST.get("description") c.private = request.POST.get("private") if (request.POST.get("hasImg") == "true"): c.img = request.FILES['img'] c.save() return HttpResponse(json.dumps(str(c.id)), content_type="application/json") else: return HttpResponse(json.dumps("invalid credentials"), content_type="application/json") else: return Http404
def add_from_url_widget(request): if request.method == 'POST': form = AddFromURLForm(request.POST) if form.is_valid(): url = form.cleaned_data['url'] cid = corg.get_petition_id(url) petition = corg.get_petition(cid) print petition c = Campaign() c.change_id = petition['petition_id'] c.title = petition['title'] c.url = petition['url'] c.overview=petition['overview'] c.signature_count=petition['signature_count'] c.image_url=petition['image_url'] c.save() return render_to_response("campaigns/thanks.html", {}, context_instance = RequestContext(request)) else: return HttpResponseRedirect('../add')