Beispiel #1
0
 def test_check_for_recent_found(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     limit = pytz.utc.localize(datetime.utcnow())
     Dispatcher.create(
         campaign_id=d.campaign_id,
         created_at=limit,
         account_id=d.account_id,
         user_id=d.user_id,
         list_id=d.list_id
     )
     limit = limit - timedelta(minutes=5) # so d doesn't trip it
     d.update(created_at=limit)
     self.assertEqual(d.check_for_recent(d.campaign_id), True)
Beispiel #2
0
 def test_dispatcher_send(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     disp_count = redis.llen("dispatcher")
     d.send()
     self.assertEqual(redis.llen("dispatcher"), disp_count + 1)
     redis.delete("dispatcher")
Beispiel #3
0
 def test_get_percent_complete_cache(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.expire_counts()
     redis.set('dispatcher_%s_percent' % d.id, 15)
     self.assertEqual(d.get_percent_complete(), 15)
     redis.delete('dispatcher_%s_percent' % d.id)
Beispiel #4
0
def submit_dispatch(campaign_id):
    campaign = Campaign.find_by_id(campaign_id) if campaign_id else None
    if not campaign:
        return abort(404)

    form = DispatcherConfirmForm(request.form)
    form.send_at.data = datetime.strptime(form.send_at.data, '%Y-%m-%d %H:%M:%S') if form.send_at.data else None
    if form.validate_on_submit():
        params = {
            'campaign_id': campaign.id,
            'account_id': current_user.account_id,
            'user_id': current_user.id,
            'list_id': campaign.list_id,
            'import_data': campaign.list_.import_data
        }
        if form.submit_send_at.data:
            params['send_at'] = form.send_at.data
            params['state'] = 15
            scheduled = 1
        else:
            params['send_at'] = None
            scheduled = 0

        d = Dispatcher.create(**params)
        if d and d.id:
            if not d.send_at:
                d.send() # fire emails => celery

            return redirect(url_for('campaign.success', campaign_id=campaign_id, scheduled=scheduled))
        else:
            flash('There was an error beginning the send, none have been sent.', 'warning')
    else:
        flash_errors(form)
    return redirect(url_for('campaign.edit', campaign_id=campaign_id))
Beispiel #5
0
 def test_dispatcher_send_bad_state(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     disp_count = redis.llen("dispatcher")
     d.update(state=1)
     d.send()
     self.assertEqual(redis.llen("dispatcher"), disp_count)
     self.assertEqual(d.state, 1)
Beispiel #6
0
 def test_get_percent_complete_not_ready(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.expire_counts()
     [d.incr_sent() for _ in range(5)]
     [d.incr_skipped() for _ in range(2)]
     self.assertEqual(d.get_percent_complete(), 0)
     redis.delete('dispatcher_%s_percent' % d.id)
Beispiel #7
0
 def test_dispatcher_send_email_from_data_success(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     sent_cnt_db = Send.query.count()
     sent_cnt_cache = int(d.get_sent())
     d.send_email_from_data({'dog': 'cat'})
     self.assertEqual(sent_cnt_db + 1, Send.query.count())
     self.assertEqual(str(sent_cnt_cache + 1), d.get_sent())
Beispiel #8
0
def prep_data_task(dispatcher_id):
    ''' state 1'''
    from liaison.models.dispatcher import Dispatcher
    dispatcher = Dispatcher.find_by_id_anon(dispatcher_id)
    try:
        dispatcher.prep_data()
    except:
        dispatcher.update(state=101)
        raise
Beispiel #9
0
 def test_dispatcher_send_email_from_data_already_exists(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     send = Send.find_by_id_anon(stack['send_id'])
     sent_count_cache = int(d.get_sent())
     send_cnt = Send.query.count()
     d.send_email_from_data({'hash_id': send.hash_id})
     self.assertEqual(send_cnt, Send.query.count())
     self.assertEqual(sent_count_cache, int(d.get_sent()))
Beispiel #10
0
def index():
    if check_redis():
        redis = True
    else:
        redis = False
        flash('Cache is offline, percentage complete may be incorrect, please contact support.', 'warning')
    page = int(request.args.get('page', 1))
    pagination = Dispatcher.find_all_desc().paginate(page=page, per_page=20)
    return render_template('dispatcher/index.html', pagination=pagination, redis=redis)
Beispiel #11
0
 def test_expire_counts(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.expire_counts()
     d.incr_sent()
     d.incr_skipped()
     d.incr_queued()
     self.assertEqual(int(d.get_sent()), 1)
     d.expire_counts()
     self.assertEqual(int(d.get_sent()), 0)
Beispiel #12
0
 def test_get_percent_complete_under_90(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.expire_counts()
     [d.incr_queued() for _ in range(10)]
     [d.incr_sent() for _ in range(5)]
     [d.incr_skipped() for _ in range(2)]
     self.assertEqual(d.get_percent_complete(), 70)
     self.assertEqual(redis.exists('dispatcher_%s_percent' % d.id), True)
     d.expire_counts()
     redis.delete('dispatcher_%s_percent' % d.id)
Beispiel #13
0
 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())
Beispiel #14
0
 def test_dispatcher_queue_emails(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.expire_counts()
     d.prep_data()
     count = len(d.adjusted_data)
     mail_count = redis.llen("mail")
     d.queue_emails()
     self.assertEqual(d.state, 3)
     self.assertEqual(redis.llen("mail"), mail_count + count)
     self.assertEqual(d.get_queued(), str(count))
     self.assertEqual(d.queued_count, count)
     redis.delete("dispatcher")
Beispiel #15
0
 def test_dispatcher_prep_data(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     orig_data = d.import_data
     disp_count = redis.llen("dispatcher")
     d.prep_data()
     self.assertEqual(d.state, 1)
     self.assertEqual(redis.llen("dispatcher"), disp_count + 1)
     self.assertEqual(d.import_data, orig_data)
     self.assertEqual(d.adjusted_data[0].keys(), ['name', 'hash_id'])
     self.assertEqual(len(d.adjusted_data[1].get('hash_id')), 64)
     self.assertEqual(len(d.adjusted_data), len(orig_data))
     redis.delete("dispatcher")
Beispiel #16
0
 def test_get_percent_complete_done(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.update(queued_count=10)
     d.expire_counts()
     [d.incr_queued() for _ in range(10)]
     [d.incr_sent() for _ in range(6)]
     [d.incr_skipped() for _ in range(4)]
     self.assertEqual(d.get_percent_complete(), 100)
     self.assertEqual(d.percent_complete, 100)
     self.assertEqual(d.state, 10)
     self.assertEqual(d.sent_count, 6)
     self.assertEqual(d.skipped_count, 4)
     redis.delete('dispatcher_%s_percent' % d.id)
Beispiel #17
0
def dispatch_retry_for_lost_tasks(d_id):
    d = Dispatcher.find_by_id_anon(d_id)
    response = d.retry_for_lost_tasks()
    flash(response)
    return redirect(url_for('admin.dispatch', d_id=d.id))
Beispiel #18
0
def dispatch(d_id):
    d = Dispatcher.find_by_id_anon(d_id)
    return render_template('admin/dispatch.html', d=d, r_up=check_redis())
Beispiel #19
0
def dispatch_next(d_id):
    d = Dispatcher.find_by_id_anon(d_id)
    response = d.next()
    flash(response)
    return redirect(url_for('admin.dispatch', d_id=d.id))
Beispiel #20
0
def queue_emails_task(dispatcher_id):
    '''state 2'''
    from liaison.models.dispatcher import Dispatcher
    dispatcher = Dispatcher.find_by_id_anon(dispatcher_id)
    dispatcher.queue_emails()
Beispiel #21
0
def send_email_task(dispatcher_id, data):
    ''' state 3 '''
    from liaison.models.dispatcher import Dispatcher
    dispatcher = Dispatcher.find_by_id_anon(dispatcher_id)
    dispatcher.send_email_from_data(data)
Beispiel #22
0
 def test_get_percent_complete_already(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.percent_complete = 100
     self.assertEqual(d.get_percent_complete(), 100)
Beispiel #23
0
 def test_dispatcher_queue_emails_bad_data(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.queue_emails()
     self.assertEqual(d.state, 104)
Beispiel #24
0
 def test_check_for_recent_none(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     limit = pytz.utc.localize(datetime.utcnow() - timedelta(minutes=5))
     d.update(created_at=limit)
     self.assertEqual(d.check_for_recent(d.campaign_id), False)
Beispiel #25
0
 def test_dispatcher_current_state(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     self.assertEqual(d.current_state(), 'Pending')
     d.update(state=10)
     self.assertEqual(d.current_state(), 'Complete')
Beispiel #26
0
def dispatcher(campaign_id):
    campaign = Campaign.find_by_id(campaign_id) if campaign_id else None
    if not campaign:
        return abort(404)

    if not current_user.accepted_terms:
        flash("You must accept the terms of this site before you can send emails.", 'warning')
        return redirect(url_for('user.terms'))

    if not current_user.account.active:
        flash("Your account is inactive, please contact support.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))

    if not current_user.account.api_key:
        flash("Your account does not have an api key, please contact support.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))

    # Must have list and emails
    if not campaign.list_id or not campaign.emails:
        flash("A campaign must have an associated list and at least one email.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))

    # List must have data
    if not campaign.list_.total_send_count() > 0:
        flash("List contains no data, cannot begin send process.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))

    # Must have footer
    if not current_user.account.footer_html:
        flash("This account does not have a footer, please add one before you initiate a send.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))

    # Don't allow dup sends
    if Dispatcher.check_for_recent(campaign.id):
        flash("To safeguard from duplication, a campaign may not be sent more than once in a 2 minute span.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))

    if campaign.selector_missing():
        flash("There is more than one email, but no selector. Please choose a selector or have only one email.", 'warning')
        return redirect(url_for('campaign.edit', campaign_id=campaign.id))


    valid_keys, bad_key = campaign.check_email_keys()
    selector_dups,bad_selector = campaign.determiner_duplicates()
    form = DispatcherForm()

    email_list = print_list(campaign.emails, 'name')

    d = datetime.utcnow() - timedelta(hours=7) # localize utc
    d_value = d.strftime(DT_FORMAT)
    d_min = d_value
    d_max = (d + timedelta(days=7)).strftime(DT_FORMAT)

    return render_template('campaign/dispatcher.html',
        form=form,
        campaign=campaign,
        valid_keys=valid_keys,
        bad_key=bad_key,
        selector_dups=selector_dups,
        bad_selector=bad_selector,
        email_list=email_list,
        d_min=d_min,
        d_max=d_max,
        d_value=d_value
    )
Beispiel #27
0
 def test_set_adjusted_data(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     self.assertEqual(d.adjusted_data, None)
     d.set_adjusted_data({'dog':'Nova'})
     self.assertEqual(d.adjusted_data, {'dog':'Nova'})
Beispiel #28
0
 def test_dispatcher_queue_emails_bad_no_hash(self):
     stack = helpers.create_stack()
     d = Dispatcher.find_by_id_anon(stack['dispatcher_id'])
     d.update(adjusted_data=d.import_data)
     d.queue_emails()
     self.assertEqual(d.state, 103)