def test_campaign_determiner_duplicates_true(self): stack = helpers.create_stack() c = Campaign.find_by_id_anon(stack['campaign_id']) e = Email.find_by_id_anon(stack['email_id']) e.update(selector_col_val='["dog"]') e2 = Email.create(account_id=c.account_id, name="EmailTwo", campaign_id=c.id) e2.update(selector_col_val='["dog"]') self.assertEqual(c.determiner_duplicates(),(True,'dog'))
def test_campaign_email_determiner_none(self): stack = helpers.create_stack() c = Campaign.find_by_id_anon(stack['campaign_id']) c.update(selector_col_name='dog') e = Email.find_by_id_anon(stack['email_id']) e.update(selector_col_val='["woof"]') e2 = Email.create(account_id=c.account_id, name="EmailTwo", campaign_id=c.id) e2.update(selector_col_val='["heina"]') self.assertEqual(c.email_determiner({'dog': 'cat'}), None)
def test_full_html(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) e.html = '<h1>this is the new test html, {{ First Name }}</h1>' e.preheader = "I am a preheader" check = "<span class='preheader'>%s</span>%s<br>%s" % (e.preheader, e.html, e.account.footer_html) self.assertEqual(e.full_html(), check)
def test_full_text(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) e.html = '<h1>this is the new test html, {{ First Name }}</h1>' e.text = 'text only test' e.preheader = "I am a preheader" check = "%s\n\n%s" % (e.text, e.account.footer_text) self.assertEqual(e.full_text(), check)
def test_render_html_attr(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) data = { 'First Name': 'Bobby Lee' } check = '<b>Body here, Hi Bobby Lee</b><br>http://localhost:5000/pf/vib/12345, http://localhost:5000/pf/unsubscribe/12345' self.assertEqual(e.render_html_attr(data,'12345'), check)
def test_render_text_attr(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) data = { 'First Name': 'Bobby Lee' } check = 'text only here Bobby Lee\n\nContact Us - http://localhost:5000/pf/vib/12345, http://localhost:5000/pf/unsubscribe/12345' self.assertEqual(e.render_text_attr(data,'12345'), check)
def test_check_keys(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) mylist = List.find_by_id_anon(stack['list_id']) e.html = "cats and {{dogs}}" e.save() mylist.import_data="{['cats': 'no_dogs']}" mylist.save() self.assertEqual( e.check_keys(stack['list_id']), (False, 'dogs') )
def delete(campaign_id, email_id): email = Email.find_by_id(email_id) if email and email.campaign_id == campaign_id: if email.sends: flash("Email has been sent, and therefore cannot be deleted.") else: try: email.delete() flash('Email deleted') except Exception, e: flash('Exception: %s' % e)
def test_dispatcher_send_email_from_data_no_email(self): stack = helpers.create_stack() d = Dispatcher.find_by_id_anon(stack['dispatcher_id']) c = Campaign.find_by_id_anon(stack['campaign_id']) e = Email.find_by_id_anon(stack['email_id']) c.update(selector_col_name='dog') e.update(selector_col_val='["false"]') send_cnt = Send.query.count() sk_cnt = int(d.get_skipped()) d.send_email_from_data({'dog': 'cat'}) self.assertEqual(send_cnt, Send.query.count()) self.assertEqual(str(sk_cnt + 1), d.get_skipped())
def test_campaign_get_selector_import_data(self): stack = helpers.create_stack() l = List.find_by_id_anon(stack['list_id']) l.import_data = [{"dog": "cat"}, {"dog": "bird"}] l.save() c = Campaign.find_by_id_anon(stack['campaign_id']) c.selector_col_name = 'dog' c.save() e = Email.find_by_id_anon(stack['email_id']) e.selector_col_val = '["cat"]' e.save() self.assertEqual(c.get_selector_import_data(), [{'dog': 'cat'}])
def image_upload(file_size_ok, campaign_id=None, email_id=None): email = Email.find_by_id(email_id) campaign = Campaign.find_by_id(campaign_id) if campaign and email and current_user.account_id == campaign.account_id and current_user.account_id == email.account_id: file = request.files['image_file'] if not file_size_ok: return jsonify({'error': 'File is too large.'}) if file and allowed_image_file(secure_filename(file.filename.lower())): filename = secure_filename(file.filename) url = upload_email_image(current_user.account_id, campaign_id, email_id, filename, file) return jsonify({'link': url}) else: return jsonify({'error': 'Invalid image type.'}) return jsonify({'error': 'Could not upload image.'})
def update(campaign_id=None, email_id=None): campaign = Campaign.find_by_id(campaign_id) if campaign_id else None if not campaign: return abort(404) email = Email.find_by_id(email_id) if not email: return abort(404) form = EditEmailForm(request.form) if campaign.list_id: keys = campaign.list_.get_import_data_keys() form.selector_col_val.choices = campaign.list_.get_unique_col_values( campaign.selector_col_name, True) else: keys = [] if form.validate_on_submit(): params = { 'name': form.name.data, 'subject': form.subject.data, 'preheader': form.preheader.data, 'html': form.html.data, 'text': form.text.data, 'selector_col_val': json.dumps(filter(None, form.selector_col_val.data)) } email.update(**params) flash('Email Updated') return redirect( url_for('email.edit', email_id=email.id, campaign_id=campaign.id)) flash_errors(form) return render_template('email/new.html', form=form, campaign=campaign, email=email, keys=keys)
def create(campaign_id=None): campaign = Campaign.find_by_id(campaign_id) if campaign_id else None if not campaign: return abort(404) form = NewEmailForm(request.form) if campaign.list_id: keys = campaign.list_.get_import_data_keys() form.selector_col_val.choices = campaign.list_.get_unique_col_values( campaign.selector_col_name, True) else: keys = [] templates = Template.get_all_choices() if form.validate_on_submit(): params = { 'name': form.name.data, 'subject': form.subject.data, 'preheader': form.preheader.data, 'selector_col_val': form.selector_col_val.data } if form.template.data: params['html'] = Template.get_html(form.template.data) params['account_id'] = current_user.account_id params['campaign_id'] = campaign.id email = Email.create(**params) if email: flash('Email Created') return redirect( url_for('email.edit', email_id=email.id, campaign_id=campaign.id)) flash_errors(form) return render_template('email/new.html', form=form, campaign=campaign, keys=keys, templates=templates)
def preview(campaign_id=None, email_id=None): campaign = Campaign.find_by_id(campaign_id) if campaign_id else None if not campaign: return abort(404) email = Email.find_by_id(email_id) if not email: return abort(404) list_id = email.campaign.list_id if email.campaign.list_id else request.args.get( 'list_id', None) l = List.find_by_id(list_id) if l: s_data = campaign.get_selector_import_data() rando_set = range(len(s_data)) if rando_set: rando = random.sample(rando_set, 1)[0] rando = s_data[rando] html = email.render_html_attr(rando, 'preview') # fake hash id if not email.text: email.text = 'No content.' text = email.render_text_attr(rando, 'preview') return render_template('email/preview.html', email=email, html=html, text=text, email_id=email_id, campaign_id=campaign_id) else: flash( "No valid data available for preview. You may need to set a selector value." ) return redirect( url_for('email.edit', email_id=email_id, campaign_id=campaign.id)) else: flash("Preview is not available until a list is selected.") return redirect( url_for('email.edit', email_id=email_id, campaign_id=campaign.id))
def edit(campaign_id=None, email_id=None): campaign = Campaign.find_by_id(campaign_id) if campaign_id else None if not campaign: return abort(404) email = Email.find_by_id(email_id) if not email: flash('Email not found') return redirect(url_for('campaign.edit', campaign_id=campaign.id)) form = EditEmailForm(obj=email) keys = [] if campaign.list_id: keys = campaign.list_.get_import_data_keys() form.selector_col_val.choices = campaign.list_.get_unique_col_values( campaign.selector_col_name, True) if email and email.selector_col_val: form.selector_col_val.data = email.selector_col_val return render_template('email/edit.html', form=form, campaign=campaign, email=email, keys=keys, auto_text=current_user.account.auto_text)
def test_campaign_email_determiner_no_specification(self): stack = helpers.create_stack() c = Campaign.find_by_id_anon(stack['campaign_id']) e = Email.find_by_id_anon(stack['email_id']) e2 = Email.create(account_id=c.account_id, name="EmailTwo", campaign_id=c.id) self.assertEqual(c.email_determiner({}), e)
def test_email_unsubscribe_link(self): self.assertEqual(Email.view_in_browser_link('abc'), 'http://localhost:5000/pf/vib/abc')
def test_add_additional_data(self): result = { 'UNSUBSCRIBE_LINK': 'http://localhost:5000/pf/unsubscribe/abc', 'VIEW_IN_BROWSER_LINK': 'http://localhost:5000/pf/vib/abc' } self.assertEqual(Email.add_additional_data({},'abc'), result)
def test_campaign_check_email_keys_bad(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) e.update(html="{{test}}") c = Campaign.find_by_id_anon(stack['campaign_id']) self.assertEqual(c.check_email_keys(),(True,None))
def test_campaign_check_email_keys_good(self): stack = helpers.create_stack() e = Email.find_by_id_anon(stack['email_id']) e.update(html="{{bad_test}}") c = Campaign.find_by_id_anon(stack['campaign_id']) self.assertEqual(c.check_email_keys(),(False,"bad_test"))
def test_campaign_selector_missing_true(self): stack = helpers.create_stack() c = Campaign.find_by_id_anon(stack['campaign_id']) e2 = Email.create(account_id=c.account_id, name="EmailTwo", campaign_id=c.id) self.assertEqual(c.selector_missing(),True)