new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.10.5 on 2017-02-06 09:17
  from __future__ import unicode_literals
  
  from django.conf import settings
  from django.db import migrations
  from django.urls import reverse
  from temba.channels.models import Channel
  from temba.orgs.models import NEXMO_KEY, NEXMO_SECRET, NEXMO_UUID, NEXMO_APP_ID
  
  
  def update_nexmo_config(Org):
      if settings.IS_PROD:
          nexmo_orgs = Org.objects.filter(config__icontains='NEXMO_KEY')
  
          updated_orgs = set()
          failed_orgs = set()
  
          for org in nexmo_orgs:
              try:
                  config = org.config_json()
                  nexmo_api_key = config.get(NEXMO_KEY, None)
                  nexmo_secret = config.get(NEXMO_SECRET, None)
  
                  org.connect_nexmo(org, nexmo_api_key, nexmo_secret, org.created_by)
                  org.refresh_from_db()
  
                  config = org.config_json()
                  app_id = config.get(NEXMO_APP_ID, None)
                  org_uuid = config.get(NEXMO_UUID)
  
                  nexmo_client = org.get_nexmo_client()
  
                  org_nexmo_channels = org.channels.filter(channel_type='NX')
  
                  for channel in org_nexmo_channels:
                      mo_path = reverse('handlers.nexmo_handler', args=['receive', org_uuid])
  
                      nexmo_client.update_nexmo_number(channel.country, channel.address,
                                                       'http://%s%s' % (settings.TEMBA_HOST, mo_path),
                                                       app_id)
  
                      nexmo_phones = nexmo_client.get_numbers(channel.address)
                      features = [elt.upper() for elt in nexmo_phones[0]['features']]
                      role = ''
                      if 'SMS' in features:
                          role += Channel.ROLE_SEND + Channel.ROLE_RECEIVE
  
                      if 'VOICE' in features:
                          role += Channel.ROLE_ANSWER + Channel.ROLE_CALL
  
                      channel.role = role
                      channel.save()
  
                  updated_orgs.add(org.pk)
                  print("Migrations successfully updated nexmo config for Org %d" % org.pk)
  
              except Exception as e:
                  print("Migrations failed to update nexmo config for org %d with error %s" % (org.pk, e.message))
                  failed_orgs.add(org.pk)
  
          print("Migrations finished updating nexmo config UPDATED: %d orgs , FAILED: %d orgs" % (len(updated_orgs), len(failed_orgs)))
          print("=" * 80)
          print("Updated orgs: %s" % ", ".join(list(updated_orgs)))
          print("Failed orgs: %s" % ", ".join(list(failed_orgs)))
  
  
  def apply_as_migration(apps, schema_editor):
      from temba.orgs.models import Org
  
      update_nexmo_config(Org)
  
  
  def apply_manual():
      from temba.orgs.models import Org
  
      update_nexmo_config(Org)
  
  
  def noop(apps, schema_editor):
      pass
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('orgs', '0031_is_squashed'),
      ]
  
      operations = [
          migrations.RunPython(apply_as_migration, noop)
      ]
  
subject: Add data migrations to update org config to connect to Nexmo