from __future__ import unicode_literals

from django.db import migrations
from osf.models import Collection
from django.contrib.contenttypes.models import ContentType


def reverse_func(state, schema):
    preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
    collections = Collection.objects.filter(collected_types__in=[preprint_content_type.id])
    for collection in collections:
        collection.collected_types.remove(preprint_content_type)


def add_preprint_type_to_collections(state, schema):
    preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
    collections = Collection.objects.exclude(collected_types__in=[preprint_content_type.id])
    for collection in collections:
        collection.collected_types.add(preprint_content_type)


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_merge_20181113_1420'),
    ]

    operations = [
        migrations.RunPython(add_preprint_type_to_collections, reverse_func)
    ]

from __future__ import unicode_literals

from django.db import migrations
from osf.models import Collection
from django.contrib.contenttypes.models import ContentType


def reverse_func(state, schema):
    preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
    ThroughModel = Collection.collected_types.through
    ThroughModel.objects.filter(contenttype_id=preprint_content_type.id).delete()


def add_preprint_type_to_collections(state, schema):
    ThroughModel = Collection.collected_types.through
    preprint_ct_id = ContentType.objects.get(app_label='osf', model='preprint').id

    through_objects = []
    collections = Collection.objects.exclude(collected_types__in=[preprint_ct_id])
    for collection in collections:
        through_objects.append(ThroughModel(collection_id=collection.id, contenttype_id=preprint_ct_id))

    ThroughModel.objects.bulk_create(through_objects)


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_merge_20181113_1420'),
    ]

    operations = [
        migrations.RunPython(add_preprint_type_to_collections, reverse_func)
    ]

Use the Collection.collected_types through table to bulk add preprints as a valid collected_type for existing collections.
Use the Collection.collected_types through table to bulk add preprints as a valid collected_type for existing collections.

- Use the Collection - collected_types through table to remove preprints from collected_types.

  from __future__ import unicode_literals
  
  from django.db import migrations
  from osf.models import Collection
  from django.contrib.contenttypes.models import ContentType
  
  
  def reverse_func(state, schema):
      preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
+     ThroughModel = Collection.collected_types.through
+     ThroughModel.objects.filter(contenttype_id=preprint_content_type.id).delete()
-     collections = Collection.objects.filter(collected_types__in=[preprint_content_type.id])
-     for collection in collections:
-         collection.collected_types.remove(preprint_content_type)
  
  
  def add_preprint_type_to_collections(state, schema):
+     ThroughModel = Collection.collected_types.through
-     preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
+     preprint_ct_id = ContentType.objects.get(app_label='osf', model='preprint').id
+ 
+     through_objects = []
-     collections = Collection.objects.exclude(collected_types__in=[preprint_content_type.id])
+     collections = Collection.objects.exclude(collected_types__in=[preprint_ct_id])
      for collection in collections:
-         collection.collected_types.add(preprint_content_type)
+         through_objects.append(ThroughModel(collection_id=collection.id, contenttype_id=preprint_ct_id))
+ 
+     ThroughModel.objects.bulk_create(through_objects)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0144_merge_20181113_1420'),
      ]
  
      operations = [
          migrations.RunPython(add_preprint_type_to_collections, reverse_func)
      ]
  
Use the Collection.collected_types through table to bulk add preprints as a valid collected_type for existing collections.
## Code Before:
from __future__ import unicode_literals

from django.db import migrations
from osf.models import Collection
from django.contrib.contenttypes.models import ContentType


def reverse_func(state, schema):
    preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
    collections = Collection.objects.filter(collected_types__in=[preprint_content_type.id])
    for collection in collections:
        collection.collected_types.remove(preprint_content_type)


def add_preprint_type_to_collections(state, schema):
    preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
    collections = Collection.objects.exclude(collected_types__in=[preprint_content_type.id])
    for collection in collections:
        collection.collected_types.add(preprint_content_type)


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_merge_20181113_1420'),
    ]

    operations = [
        migrations.RunPython(add_preprint_type_to_collections, reverse_func)
    ]

## Instruction:
Use the Collection.collected_types through table to bulk add preprints as a valid collected_type for existing collections.
## Code After:
from __future__ import unicode_literals

from django.db import migrations
from osf.models import Collection
from django.contrib.contenttypes.models import ContentType


def reverse_func(state, schema):
    preprint_content_type = ContentType.objects.get(app_label='osf', model='preprint')
    ThroughModel = Collection.collected_types.through
    ThroughModel.objects.filter(contenttype_id=preprint_content_type.id).delete()


def add_preprint_type_to_collections(state, schema):
    ThroughModel = Collection.collected_types.through
    preprint_ct_id = ContentType.objects.get(app_label='osf', model='preprint').id

    through_objects = []
    collections = Collection.objects.exclude(collected_types__in=[preprint_ct_id])
    for collection in collections:
        through_objects.append(ThroughModel(collection_id=collection.id, contenttype_id=preprint_ct_id))

    ThroughModel.objects.bulk_create(through_objects)


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0144_merge_20181113_1420'),
    ]

    operations = [
        migrations.RunPython(add_preprint_type_to_collections, reverse_func)
    ]
