from __future__ import unicode_literals

from django.db import migrations


def fix_followers(apps, schema_editor):
    Donation = apps.get_model('donations', 'Donation')
    Follow = apps.get_model('bb_follow', 'Follow')
    ContentType = apps.get_model('contenttypes', 'ContentType')

    for donation in Donation.objects.\
            filter(order__status__in=['success', 'pending']).\
            exclude(order__order_type='recurring').all():
        user = donation.order.user
        followed_object = donation.project
        content_type = ContentType.objects.get_for_model(followed_object)

        # A Follow object should link the project to the user, not the
        # donation and the user
        if user and followed_object and user != followed_object.owner:
            if not Follow.objects.filter(user=user, object_id=followed_object.id, content_type=content_type).count():
                Follow.objects.create(user=user, object_id=followed_object.id, content_type=content_type)


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('bb_follow', '0002_follow_user'),
    ]

    operations = [
        migrations.RunPython(fix_followers, dummy)
    ]

from __future__ import unicode_literals

from django.db import migrations


def fix_followers(apps, schema_editor):
    Donation = apps.get_model('donations', 'Donation')
    Follow = apps.get_model('bb_follow', 'Follow')
    ContentType = apps.get_model('contenttypes', 'ContentType')

    for donation in Donation.objects.\
            filter(order__status__in=['success', 'pending']).\
            exclude(order__order_type='recurring').all():
        user = donation.order.user
        followed_object = donation.project
        content_type = ContentType.objects.get_for_model(followed_object)

        # A Follow object should link the project to the user, not the
        # donation and the user
        if user and followed_object and user != followed_object.owner:
            if not Follow.objects.filter(user=user, object_id=followed_object.id, content_type=content_type).count():
                Follow.objects.create(user=user, object_id=followed_object.id, content_type=content_type)


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('bb_follow', '0002_follow_user'),
        ('donations', '0008_auto_20170927_1021')
    ]

    operations = [
        migrations.RunPython(fix_followers, dummy)
    ]

Add donation migration dependancy in bb_follow
Add donation migration dependancy in bb_follow

  from __future__ import unicode_literals
  
  from django.db import migrations
  
  
  def fix_followers(apps, schema_editor):
      Donation = apps.get_model('donations', 'Donation')
      Follow = apps.get_model('bb_follow', 'Follow')
      ContentType = apps.get_model('contenttypes', 'ContentType')
  
      for donation in Donation.objects.\
              filter(order__status__in=['success', 'pending']).\
              exclude(order__order_type='recurring').all():
          user = donation.order.user
          followed_object = donation.project
          content_type = ContentType.objects.get_for_model(followed_object)
  
          # A Follow object should link the project to the user, not the
          # donation and the user
          if user and followed_object and user != followed_object.owner:
              if not Follow.objects.filter(user=user, object_id=followed_object.id, content_type=content_type).count():
                  Follow.objects.create(user=user, object_id=followed_object.id, content_type=content_type)
  
  
  def dummy(apps, schema_editor):
      pass
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('bb_follow', '0002_follow_user'),
+         ('donations', '0008_auto_20170927_1021')
      ]
  
      operations = [
          migrations.RunPython(fix_followers, dummy)
      ]
  
Add donation migration dependancy in bb_follow
## Code Before:
from __future__ import unicode_literals

from django.db import migrations


def fix_followers(apps, schema_editor):
    Donation = apps.get_model('donations', 'Donation')
    Follow = apps.get_model('bb_follow', 'Follow')
    ContentType = apps.get_model('contenttypes', 'ContentType')

    for donation in Donation.objects.\
            filter(order__status__in=['success', 'pending']).\
            exclude(order__order_type='recurring').all():
        user = donation.order.user
        followed_object = donation.project
        content_type = ContentType.objects.get_for_model(followed_object)

        # A Follow object should link the project to the user, not the
        # donation and the user
        if user and followed_object and user != followed_object.owner:
            if not Follow.objects.filter(user=user, object_id=followed_object.id, content_type=content_type).count():
                Follow.objects.create(user=user, object_id=followed_object.id, content_type=content_type)


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('bb_follow', '0002_follow_user'),
    ]

    operations = [
        migrations.RunPython(fix_followers, dummy)
    ]

## Instruction:
Add donation migration dependancy in bb_follow
## Code After:
from __future__ import unicode_literals

from django.db import migrations


def fix_followers(apps, schema_editor):
    Donation = apps.get_model('donations', 'Donation')
    Follow = apps.get_model('bb_follow', 'Follow')
    ContentType = apps.get_model('contenttypes', 'ContentType')

    for donation in Donation.objects.\
            filter(order__status__in=['success', 'pending']).\
            exclude(order__order_type='recurring').all():
        user = donation.order.user
        followed_object = donation.project
        content_type = ContentType.objects.get_for_model(followed_object)

        # A Follow object should link the project to the user, not the
        # donation and the user
        if user and followed_object and user != followed_object.owner:
            if not Follow.objects.filter(user=user, object_id=followed_object.id, content_type=content_type).count():
                Follow.objects.create(user=user, object_id=followed_object.id, content_type=content_type)


def dummy(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('bb_follow', '0002_follow_user'),
        ('donations', '0008_auto_20170927_1021')
    ]

    operations = [
        migrations.RunPython(fix_followers, dummy)
    ]
