def send_email_from_data(self, data): ''' Given a row of data, make sure this hash_id doesn't exist, and has email. If so, create a send and process it. ''' hash_id = data.get('hash_id') # no dups, this way its idempotent send = Send.find_by_hash(hash_id) if send: # This is used for retry_for_lost_tasks and 102's. (Counts should have been expired.) # If half the sends are already sent then we mark those here, but we want send.process() to mark actual send. if send.attempts > 0: self.incr_sent( ) # else: it will handle itself below when calling process() else: email = self.campaign.email_determiner(data) if not email: self.incr_skipped() else: send = Send.create(email_id=email.id, dispatcher_id=self.id, account_id=self.account_id, data=data, hash_id=hash_id) if send: # If there is an email and the send was created... send.process() else: current_app.logger.info( "\n send_email_from_data: send failed to create: %s \n" % hash_id)
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()))
def prep_data(self): ''' Add a hash_id to every row. Thats it. ''' self.update(state=1) new_data = [] for index, customer in enumerate(self.import_data): if not customer.get('hash_id'): hash_id = os.urandom(32).encode('hex') while Send.find_by_hash(hash_id): hash_id = os.urandom(32).encode('hex') customer['hash_id'] = hash_id new_data.append(customer) self.set_adjusted_data(new_data) queue_emails_task.delay(self.id)
def unsubscribe(hash_id=None): found = send = unsubscribed = False send = Send.find_by_hash(hash_id) if send and send.message: email = send.message.get('to')[0].get('email') if email: Unsubscribe.create(email=email, account_id=send.account_id) found = True if not found: current_app.logger.info("Email Not Found for hash: %s " % hash_id) return render_template('pf/unsubscribe.html', found=found, unsubscribed=unsubscribed)
def vib(hash_id): redis.incr('vib_counter') redis_result = redis.get(hash_id) if redis_result: return redis_result send = Send.find_by_hash(hash_id) if send: data = send.message response = data['html'] if response: soup = BeautifulSoup(response) if soup.find('span', 'preheader'): soup.find('span', 'preheader').extract() response = unicode(soup) if check_redis(): set_cache(hash_id, response, 86400) # 1 day return response abort(404)
def send_details(hash_id=None, id=None): if hash_id: send = Send.find_by_hash(hash_id) else: send = Send.find_by_id_anon(id) return render_template('admin/send_detail.html', send=send)
def retry_send(send_id): from liaison.models.send import Send send = Send.find_by_id_anon(send_id) if send: logger.info("retry_send is resending: %s" % send.id) send.process()
def test_find_by_hash(self): stack = helpers.create_stack() self.assertEqual(Send.find_by_hash('azyx').id, stack['send_id'])
def test_current_state(self): s = Send(state=9) self.assertEqual(s.current_state(), 'unknown')