from __future__ import unicode_literals

from django.db import migrations
from temba.utils import chunk_list


def do_populate_send_all(Broadcast):
    broadcast_ids = Broadcast.objects.all().values_list('id', flat=True)

    for chunk in chunk_list(broadcast_ids, 1000):
        Broadcast.objects.filter(pk__in=chunk).update(send_all=False)


def apply_as_migration(apps, schema_editor):
    Broadcast = apps.get_model('msgs', 'Broadcast')
    do_populate_send_all(Broadcast)


def apply_manual():
    from temba.msgs.models import Broadcast
    do_populate_send_all(Broadcast)


class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0086_broadcast_send_all'),
    ]

    operations = [
        migrations.RunPython(apply_as_migration)
    ]

from __future__ import unicode_literals

from django.db import migrations
from temba.utils import chunk_list


def do_populate_send_all(Broadcast):
    broadcast_ids = Broadcast.objects.all().values_list('id', flat=True)

    broadcast_count = len(broadcast_ids)
    print('Starting to update %d broadcasts send all field...' % broadcast_count)

    updated = 0
    for chunk in chunk_list(broadcast_ids, 1000):
        Broadcast.objects.filter(pk__in=chunk).update(send_all=False)
        print("Updated %d of %d broadcasts" % (updated + len(chunk), broadcast_count))


def apply_as_migration(apps, schema_editor):
    Broadcast = apps.get_model('msgs', 'Broadcast')
    do_populate_send_all(Broadcast)


def apply_manual():
    from temba.msgs.models import Broadcast
    do_populate_send_all(Broadcast)


class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0086_broadcast_send_all'),
    ]

    operations = [
        migrations.RunPython(apply_as_migration)
    ]

Print progress of data migration
Print progress of data migration

  from __future__ import unicode_literals
  
  from django.db import migrations
  from temba.utils import chunk_list
  
  
  def do_populate_send_all(Broadcast):
      broadcast_ids = Broadcast.objects.all().values_list('id', flat=True)
  
+     broadcast_count = len(broadcast_ids)
+     print('Starting to update %d broadcasts send all field...' % broadcast_count)
+ 
+     updated = 0
      for chunk in chunk_list(broadcast_ids, 1000):
          Broadcast.objects.filter(pk__in=chunk).update(send_all=False)
+         print("Updated %d of %d broadcasts" % (updated + len(chunk), broadcast_count))
  
  
  def apply_as_migration(apps, schema_editor):
      Broadcast = apps.get_model('msgs', 'Broadcast')
      do_populate_send_all(Broadcast)
  
  
  def apply_manual():
      from temba.msgs.models import Broadcast
      do_populate_send_all(Broadcast)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('msgs', '0086_broadcast_send_all'),
      ]
  
      operations = [
          migrations.RunPython(apply_as_migration)
      ]
  
Print progress of data migration
## Code Before:
from __future__ import unicode_literals

from django.db import migrations
from temba.utils import chunk_list


def do_populate_send_all(Broadcast):
    broadcast_ids = Broadcast.objects.all().values_list('id', flat=True)

    for chunk in chunk_list(broadcast_ids, 1000):
        Broadcast.objects.filter(pk__in=chunk).update(send_all=False)


def apply_as_migration(apps, schema_editor):
    Broadcast = apps.get_model('msgs', 'Broadcast')
    do_populate_send_all(Broadcast)


def apply_manual():
    from temba.msgs.models import Broadcast
    do_populate_send_all(Broadcast)


class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0086_broadcast_send_all'),
    ]

    operations = [
        migrations.RunPython(apply_as_migration)
    ]

## Instruction:
Print progress of data migration
## Code After:
from __future__ import unicode_literals

from django.db import migrations
from temba.utils import chunk_list


def do_populate_send_all(Broadcast):
    broadcast_ids = Broadcast.objects.all().values_list('id', flat=True)

    broadcast_count = len(broadcast_ids)
    print('Starting to update %d broadcasts send all field...' % broadcast_count)

    updated = 0
    for chunk in chunk_list(broadcast_ids, 1000):
        Broadcast.objects.filter(pk__in=chunk).update(send_all=False)
        print("Updated %d of %d broadcasts" % (updated + len(chunk), broadcast_count))


def apply_as_migration(apps, schema_editor):
    Broadcast = apps.get_model('msgs', 'Broadcast')
    do_populate_send_all(Broadcast)


def apply_manual():
    from temba.msgs.models import Broadcast
    do_populate_send_all(Broadcast)


class Migration(migrations.Migration):

    dependencies = [
        ('msgs', '0086_broadcast_send_all'),
    ]

    operations = [
        migrations.RunPython(apply_as_migration)
    ]
