def queue_msg_tasks_for_new_user(recipient_number): catfact_message_sequence = generate_cat_facts_message_sequence() for message_index, catfact_message in enumerate(catfact_message_sequence): delay_amount = 9*message_index # send a message every 9 hours (because cats have 9 lives!) Meow! time = datetime.datetime.utcnow() + datetime.timedelta(hours=delay_amount) send_sms.apply_async((recipient_number, SENDING_NUMBER, catfact_message), eta=time) print recipient_number, SENDING_NUMBER, catfact_message, time
def send_sms_pipe(data): ''' check that the ip is not blacklisted, or that the tlf has already voted, and finally set the return value NOTE: Requires that the pipeline has executed generate_token or similar. ''' from app import db from models import Voter, Message from toolbox import hash_token from tasks import send_sms ip_addr = data['ip_addr'] # disable older registration attempts for this tlf curr_eid = current_app.config.get("CURRENT_ELECTION_ID", 0) voter = data['requested_voter'] old_voters = db.session.query(Voter)\ .filter(Voter.election_id == curr_eid, Voter.tlf == data["tlf"], Voter.is_active == True, Voter.id != voter.id) for ov in old_voters: ov.is_active = False db.session.add(ov) # create the message to be sent. note, that we ignore spaces in the token token_hash = hash_token(data['token'].replace(" ", "")) msg = Message( tlf=data["tlf"], ip=ip_addr, lang_code=current_app.config.get("BABEL_DEFAULT_LOCALE", "en"), token=token_hash, status=Message.STATUS_QUEUED, ) # set voter to created and send SMS voter.message = msg voter.status = Voter.STATUS_CREATED db.session.add(voter) db.session.add(msg) db.session.commit() send_sms.apply_async( kwargs=dict(msg_id=msg.id, token=data['token'], is_audio=data['is_audio']), countdown=current_app.config.get('SMS_DELAY', 1), expires=current_app.config.get('SMS_EXPIRE_SECS', 120), ) return make_response("", 200)
def send_sms_pipe(data): ''' check that the ip is not blacklisted, or that the tlf has already voted, and finally set the return value NOTE: Requires that the pipeline has executed generate_token or similar. ''' from app import db from models import Voter, Message from toolbox import hash_token from tasks import send_sms ip_addr = data['ip_addr'] # disable older registration attempts for this tlf curr_eid = current_app.config.get("CURRENT_ELECTION_ID", 0) voter = data['requested_voter'] old_voters = db.session.query(Voter)\ .filter(Voter.election_id == curr_eid, Voter.tlf == data["tlf"], Voter.is_active == True, Voter.id != voter.id) for ov in old_voters: ov.is_active = False db.session.add(ov) # create the message to be sent. note, that we ignore spaces in the token token_hash = hash_token(data['token'].replace(" ", "")) msg = Message( tlf=data["tlf"], ip=ip_addr, lang_code=current_app.config.get("BABEL_DEFAULT_LOCALE", "en"), token=token_hash, status=Message.STATUS_QUEUED, ) # set voter to created and send SMS voter.message=msg voter.status=Voter.STATUS_CREATED db.session.add(voter) db.session.add(msg) db.session.commit() send_sms.apply_async(kwargs=dict( msg_id=msg.id, token=data['token'], is_audio=data['is_audio']), countdown=current_app.config.get('SMS_DELAY', 1), expires=current_app.config.get('SMS_EXPIRE_SECS', 120),) return make_response("", 200)
def schedule_sms(receiver, content, tz=DEFAULT_TZ): from tasks import send_sms now = datetime.now(tz) current_time = now.time() if any(imap(lambda x: x[0] <= current_time and current_time <= x[1], INTERVALS)): send_sms.delay(receiver, content) return availables = filter(lambda x: current_time <= x, imap(lambda x: x[0], INTERVALS)) if availables: # send it at the beginning of next interval eta = datetime.combine(now.date(), min(availables)) else: # send it tomorrow eta = datetime.combine((now + timedelta(days=1)).date(), min(imap(lambda x: x[0], INTERVALS))) eta = tz.localize(eta) send_sms.apply_async(args=[receiver, content], eta=eta)
def schedule_sms(receiver, content, tz=DEFAULT_TZ): from tasks import send_sms now = datetime.now(tz) current_time = now.time() if any( imap(lambda x: x[0] <= current_time and current_time <= x[1], INTERVALS)): send_sms.delay(receiver, content) return availables = filter(lambda x: current_time <= x, imap(lambda x: x[0], INTERVALS)) if availables: # send it at the beginning of next interval eta = datetime.combine(now.date(), min(availables)) else: # send it tomorrow eta = datetime.combine((now + timedelta(days=1)).date(), min(imap(lambda x: x[0], INTERVALS))) eta = tz.localize(eta) send_sms.apply_async(args=[receiver, content], eta=eta)