from datetime import datetime
from time import sleep

from django.conf import settings

from django_rq import job

from smsfly import SMSFlyAPI

from .models import Alphaname


DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'


@job('default')
def addNewCampaignTask(task_id):
    sleep(0.4)
    print('Ran {}'.format(task_id))
    return 'Ran {}'.format(task_id)


@job('high')
def sendMessagesInstantly(message='Hi man!', send_as='Alpha',
                          description='Test task', to=('380971234567',),
                          task_id=100500):
    api = SMSFlyAPI(account_id=settings.SMS_FLY['login'],
                    account_pass=settings.SMS_FLY['password'])
    return api.send_sms_to_recipients(
        start_time=datetime.now().strftime(DATETIME_FORMAT), end_time='AUTO',
        lifetime=24, rate='AUTO', desc=description,
        source=send_as, body=message, recipients=to
    )


@job('high')
def submitAlphanameInstantly(name):
    api = SMSFlyAPI(account_id=settings.SMS_FLY['login'],
                    account_pass=settings.SMS_FLY['password'])
    print('Submitting register request for alphaname `{}`...'.format(name))

    api_response_status = api.add_alphaname(alphaname=name).state.attrs['status']
    alphaname = Alphaname.objects.get(name=name)
    alphaname.text_status = api_response_status
    alphaname.save()

    print('Saved')
    print('Got status `{}`...'.format(api_response_status))

    return api_response_status

@@ -23,16 +23,46 @@
 atetime%0A
+from itertools import starmap%0A
 from tim
@@ -197,16 +197,42 @@
 lphaname
+, Task, Campaign, Follower
 %0A%0A%0ADATET
@@ -724,230 +724,1308 @@
     
-return api.send_sms_to_recipients(%0A        start_time=datetime.now().strftime(DATETIME_FORMAT), end_time='AUTO',%0A        lifetime=24, rate='AUTO', desc=description,%0A        source=send_as, body=message, recipients=to%0A    )
+task = Task.objects.get(pk=task_id)%0A%0A    recipients_filter = task.recipients_filter_json%0A    recipients = Follower.objects.get(**recipients_filter)  # Filter out recipients from external DB%0A%0A    message_pairs = starmap(lambda r: (r.cellphone,  # Generate tuple containing cell number and message body%0A                                       message.format(  # substitute template tags with personalized values:%0A                                           firstname=r.firstname, lastname=r.lastname,%0A                                           middlename=r.middlename, cellphone=r.cellphone,%0A                                           address=r.address,%0A                                       )),%0A                            recipients)%0A%0A    api_result = api.send_sms_pairs(%0A        start_time=datetime.now().strftime(DATETIME_FORMAT), end_time='AUTO',%0A        lifetime=24, rate='AUTO', desc=description,%0A        source=send_as, message_pairs=message_pairs%0A    )%0A%0A    res_state = api_result.message.state%0A    Campaign.objects.create(task=task, code=res_state.attrs%5B'code'%5D,%0A                            datetime_sent=datetime.strptime(res_state.attrs%5B'date'%5D, DATETIME_FORMAT),%0A                            state=res_state.text, smsfly_campaign_id=int(res_state.attrs%5B'campaignID'%5D))%0A%0A    return api_result
 %0A%0A%0A@

Improve campaign sending task