<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.core.urlresolvers import reverse
from django.conf import settings
from twilio.rest import TwilioRestClient
import json


def get_twilio_client(org):
    config = org.config
    if config:
        config = json.loads(config)
        account_sid = config.get('ACCOUNT_SID', None)
        auth_token = config.get('ACCOUNT_TOKEN', None)
        if account_sid and auth_token:
            return TwilioRestClient(account_sid, auth_token)
    return None


def fix_twilio_twiml_apps(Org):
    twilio_orgs = Org.objects.filter(config__icontains='APPLICATION_SID')
    if twilio_orgs:
        print 'Updating %s orgs with twilio connections..' % len(twilio_orgs)

    for idx, twilio_org in enumerate(twilio_orgs):
        print '%d: Updating org %d' % ((idx + 1), twilio_org.pk)
        client = get_twilio_client(twilio_org)
        app_name = "%s/%d" % (settings.TEMBA_HOST.lower(), twilio_org.pk)
        app_url = "https://" + settings.TEMBA_HOST + "%s" % reverse('handlers.twilio_handler')
        apps = client.applications.list(friendly_name=app_name)
        if apps:
            for app in apps:
                app.update(voice_url=app_url, sms_url=app_url, status_callback=app_url, status_callback_method='POST')


def apply_as_migration(apps, schema_editor):
    Org = apps.get_model('orgs', 'Org')
    fix_twilio_twiml_apps(Org)


def apply_manual():
    from temba.orgs.models import Org
    fix_twilio_twiml_apps(Org)


class Migration(migrations.Migration):
    dependencies = [
        ('ivr', '0013_reset_1'),
    ]

    operations = [
        migrations.RunPython(apply_as_migration)
    ]<commit_msg>Some error handling and better spam for twiml app updates<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations
from django.core.urlresolvers import reverse
from django.conf import settings
from twilio.rest import TwilioRestClient, TwilioException
import json


def get_twilio_client(org):
    config = org.config
    if config:
        config = json.loads(config)
        account_sid = config.get('ACCOUNT_SID', None)
        auth_token = config.get('ACCOUNT_TOKEN', None)
        if account_sid and auth_token:
            return TwilioRestClient(account_sid, auth_token)
    return None


def fix_twilio_twiml_apps(Org):
    twilio_orgs = Org.objects.filter(config__icontains='APPLICATION_SID').order_by('created_on')
    if twilio_orgs:
        print 'Updating %s orgs with twilio connections..' % len(twilio_orgs)

    for idx, twilio_org in enumerate(twilio_orgs):
        print '%d: Updating %s (%d)' % ((idx + 1), twilio_org.name, twilio_org.id)
        client = get_twilio_client(twilio_org)

        if client:
            app_name = "%s/%d" % (settings.TEMBA_HOST.lower(), twilio_org.pk)
            app_url = "https://" + settings.TEMBA_HOST + "%s" % reverse('handlers.twilio_handler')

            try:
                apps = client.applications.list(friendly_name=app_name)

                if apps:
                    for app in apps:
                        print '     Updating %s (Last: %s, Voice: %s Callback: %s)' % (app.friendly_name, app.date_updated, app.voice_url, app.status_callback)
                        app.update(voice_url=app_url, sms_url=app_url, status_callback=app_url, status_callback_method='POST')
                else:
                    print '     No apps found for %s' % twilio_org.name
            except TwilioException:
                print '     Connection failed for %s, skipping..' % twilio_org.name
        else:
            print "     Couldn't get TwilioClient for %s" % twilio_org.name


def apply_as_migration(apps, schema_editor):
    Org = apps.get_model('orgs', 'Org')
    fix_twilio_twiml_apps(Org)


def apply_manual():
    from temba.orgs.models import Org
    fix_twilio_twiml_apps(Org)


class Migration(migrations.Migration):
    dependencies = [
        ('ivr', '0013_reset_1'),
    ]

    operations = [
        migrations.RunPython(apply_as_migration)
    ]
{'fraction_non_alphanumeric': 0.10167435088570735, 'fraction_numerical': 0.003397233681145353, 'mean_word_length': 2.9596541786743518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4551698', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1316, 'n_words': 290}