<reponame>terrameijar/bluebottle<filename>bluebottle/bb_follow/migrations/0003_auto_20180530_1621.py
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-05-30 14:21
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)
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-05-30 14:21
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)
    ]
# -*- coding: utf-8 -*- # Generated by Django 1.10.8 on 2018-05-30 14:21 # A Follow object should link the project to the user, not the # donation and the user