from celery.task import task
from celery.task.sets import TaskSet
from vumiclient.client import Client
from django.conf import settings
from django.core.mail import send_mail,  EmailMessage
from jmboarticles.models import Article
from django.db.models import Q


@task(ignore_result=True)
def send_password_reset(msisdn, new_password):
    message = 'Ummeli on YAL :) Your new password is: %s' % new_password
    client = Client(settings.VUMI_USERNAME, settings.VUMI_PASSWORD)
    resp = client.send_sms(to_msisdn = msisdn,
        from_msisdn = '1',
        message = message)


@task(ignore_result=True)
def send_email(username, message):
    email = EmailMessage('Blocked User: %s' % username, message,
                                            settings.SEND_FROM_EMAIL_ADDRESS,
                                            [settings.UMMELI_SUPPORT])
    email.send(fail_silently=False)


def send_sms(msisdn, message):
    client = Client(settings.VUMI_USERNAME, settings.VUMI_PASSWORD)
    client.send_sms(to_msisdn = msisdn, from_msisdn = '1', message = message)


def disable_commenting():
    Article.objects.filter(comments_enabled=True, can_comment=False)\
                   .update(can_comment=True)
    Article.objects.filter(comments_enabled=True, can_comment=True)\
                   .update(comments_enabled=False)


def enable_commenting():
    qs = Article.published_objects.filter(can_comment=True)

    #on homepage
    qs.filter(on_homepage=True).update(comments_enabled=True)

    #latest 3 articles
    featured = qs.filter(on_homepage=False)[:3]

    #disable other articles
    Article.published_objects.filter(comments_enabled=True, can_comment=True,
                                     on_homepage=False)\
                             .exclude(pk__in=[a.pk for a in featured])\
                             .update(comments_enabled=False)

    for a in featured:
        a.comments_enabled = True
        a.save()


@task(ignore_result=True)
def disable_comments_scheduler():
    from datetime import datetime, time
    now = datetime.now().time()
    if now > time(21, 0, 0) or now < time(11, 0, 0):
        disable_commenting()
    else:
        enable_commenting()

@@ -1,24 +1,52 @@
+import json%0Aimport requests%0A
 from celery.task import 
@@ -54,82 +54,8 @@
 ask%0A
-from celery.task.sets import TaskSet%0Afrom vumiclient.client import Client%0A
 from
@@ -116,20 +116,8 @@
 ort 
-send_mail,  
 Emai
@@ -169,39 +169,8 @@
 cle%0A
-from django.db.models import Q%0A
 %0A%0A@t
@@ -321,163 +321,24 @@
     
-client = Client(settings.VUMI_USERNAME, settings.VUMI_PASSWORD)%0A    resp = client.send_sms(to_msisdn = msisdn,%0A        from_msisdn = '1',%0A        message =
+send_sms(msisdn,
  mes
@@ -431,16 +431,25 @@
 Message(
+%0A        
 'Blocked
@@ -462,32 +462,40 @@
  %25s' %25 username,
+%0A       
  message,%0A      
@@ -492,44 +492,8 @@
 ge,%0A
-                                    
     
@@ -534,44 +534,8 @@
 SS,%0A
-                                    
     
@@ -563,16 +563,21 @@
 SUPPORT%5D
+%0A    
 )%0A    em
@@ -613,32 +613,35 @@
 %0A%0A%0Adef send_sms(
+to_
 msisdn, message)
@@ -637,161 +637,384 @@
 n, m
-essage
+sg
 ):%0A    
-client = Client(settings.VUMI_USERNAME, settings.VUMI_PASSWORD)%0A    client.send_sms(to_msisdn = msisdn, from_msisdn = '1', message = message
+base_url = 'https://go.vumi.org/api/v1'%0A    url = '%25s/go/http_api_nostream/%25s/messages.json' %25 (%0A        base_url,%0A        settings.VUMI_SMS_CONVERSATION_KEY%0A    )%0A    data = %7B%0A        %22content%22: msg,%0A        %22to_addr%22: to_msisdn%0A    %7D%0A    requests.put(%0A        url,%0A        json.dumps(data),%0A        auth=(settings.VUMI_ACCOUNT_KEY, settings.VUMI_ACCESS_TOKEN),%0A    
 )%0A%0A%0A

use vumi go instead of vumi.client