def submit(list_id=None): l = List.find_by_id(list_id) form = ListForm(request.form) if form.validate_on_submit(): if l: l.update(name=form.name.data) flash('List Updated') return redirect(url_for('list_.index')) else: l = List.create(name=form.name.data, account_id=current_user.account_id) if l: flash('List Created') return redirect(url_for('list_.edit', list_id=l.id)) else: page = int(request.args.get('page', 1)) flash_errors(form) pagination = List.find_all_desc().paginate(page=page, per_page=20) return render_template('list/index.html', form=form, list=l, user=current_user, pagination=pagination)
def update(campaign_id=None): form = CampaignForm(request.form) list_id = request.form.get('list_id', None) form = setup_campaign_select_choices(form, list_id) campaign = Campaign.find_by_id(campaign_id) if form.validate_on_submit(): params = { 'name': form.name.data, 'from_email_dd': form.from_email_dd.data, 'from_name_dd': form.from_name_dd.data, 'reply_to_dd': form.reply_to_dd.data, 'to_email_dd': form.to_email_dd.data, 'to_name_dd': form.to_name_dd.data, 'from_email_ov': form.from_email_ov.data, 'from_name_ov': form.from_name_ov.data, 'reply_to_ov': form.reply_to_ov.data, 'to_email_ov': form.to_email_ov.data, 'to_name_ov': form.to_name_ov.data, 'selector_col_name': form.selector_col_name.data } if campaign.selector_col_name and campaign.selector_col_name != params['selector_col_name']: flash("Selector column changed, all emails have had selector values removed.", 'warning') for email in campaign.emails: email.update(selector_col_val=None) campaign.update(**params) flash('Campaign Updated') return redirect(url_for('campaign.edit', campaign_id=campaign.id)) flash_errors(form) list_form = CampaignListForm() list_form.list_id.choices = [('','')] + [(str(h.id), h.name) for h in List.find_all()] return render_template('campaign/edit.html', form=form, campaign=campaign, list_form=list_form)
def run_import(list_id): from liaison.models.list import List from liaison.lib.aws import get_contents_as_string from liaison.lib.extensions import redis_store as redis key = 'list_%s_import' % list_id list_ = List.find_by_id_anon(list_id) if list_: filename = list_.filename logger.info("\n filename: %s \n" % str(filename)) if "lists/" in filename: f = get_contents_as_string(filename) else: f = open(filename, 'rt') data = [] try: data = [ row for row in csv.DictReader( f.read().splitlines(), restkey='rk', restval='rv') ] list_.import_data = data result = list_.save() if result: redis.set(key, 'Upload Successful.') else: redis.set(key, 'List failed to save, the data may be corrupted.') except Exception, e: logger.info("Error: import_failure_list: %s: \n %s" % (e, traceback.format_exc())) redis.set(key, 'List failed to save, the data may be corrupted.') raise e finally:
def index(): form = ListForm() page = int(request.args.get('page', 1)) pagination = List.find_all_desc().paginate(page=page, per_page=20) return render_template('list/index.html', user=current_user, pagination=pagination, form=form)
def list_select(): list_id = request.args.get('list_id') keys = [] if list_id.isdigit(): list_ = List.find_by_id(int(list_id)) if list_: keys = list_.get_import_data_keys() return json.dumps(keys)
def check_keys(self, list_id): list_ = List.find_by_id_anon(list_id) keys = list_.random_data_sample(1)[0] renderer = pystache.Renderer(missing_tags='strict') try: renderer.render(self.html, keys) return True, None except KeyNotFoundError, e: return False, e.key
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 upload_file(list_id, file_size_ok): '''import a contact file to uploads/imports/{account_id}/{list_id}/filename ''' file = request.files['file'] if not file_size_ok: flash("File is limited to 100MB. Contact support for more info.", 'warning') return redirect(url_for('list_.edit', list_id=list_id)) l = List.find_by_id(list_id) if file and allowed_import_file(file.filename): l.filename = List.save_file(file, list_id, current_user.account.id) l.save() run_import.delay(list_id) flash( 'File uploaded. Data may take some time to load, when finished it will be displayed below.' ) else: flash( "File type not allowed, Please upload files with extensions: %s" % ", ".join(ALLOWED_IMPORT_EXTENSIONS), 'warning') return redirect(url_for('list_.edit', list_id=list_id))
def list_update(campaign_id=None): campaign = Campaign.find_by_id(campaign_id) list_id = request.form.get('list_id') l = List.find_by_id(list_id) if list_id else None acct_id = current_user.account_id if campaign and acct_id == campaign.account_id and l and acct_id == l.account_id: campaign.update(list_id=list_id) return jsonify({'success': 'true'}) else: return jsonify({'success': 'false'})
def delete(list_id): l = List.find_by_id(list_id) if l: if l.campaigns: flash('Cannot delete, attached to one or more campaigns', 'warning') return redirect(url_for('list_.index')) try: l.delete() flash('List deleted') except Exception, e: flash('Exception: %s' % e)
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 edit(campaign_id=None): if campaign_id: campaign = Campaign.find_by_id(campaign_id) if campaign: form = CampaignForm(obj=campaign) form = setup_campaign_select_choices(form, campaign.list_id) if campaign.list_id else form if not form.from_email_ov.data: form.from_email_ov.data = current_user.account.default_from_email else: flash('Campaign not found') return redirect('/campaigns/') list_form = CampaignListForm() list_form.list_id.choices = [('','')] + [(str(h.id), h.name) for h in List.find_all()] return render_template('campaign/edit.html', form=form, campaign=campaign, list_form=list_form, emails=campaign.emails)
def setup_campaign_select_choices(form, list_id): l = List.find_by_id(list_id) if l: keys = l.get_import_data_keys() else: keys = [] key_list = [('','')] + [(str(key), str(key)) for key in keys] form.from_email_dd.choices = key_list form.reply_to_dd.choices = key_list form.from_name_dd.choices = key_list form.to_email_dd.choices = key_list form.to_name_dd.choices = key_list form.selector_col_name.choices = key_list return form
def edit(list_id): l = List.find_by_id(list_id) if l: key = 'list_%s_import' % list_id if get_cache(key): flash(get_cache(key), 'warning') del_cache(key) random_data = l.random_data_sample() length = l.total_send_count() form = ListForm(obj=l) return render_template('list/edit.html', form=form, list=l, data=random_data, length=length) else: flash('List not found') return redirect(url_for('list_.index'))
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))