from django.db.models.functions import Lower
from django.db.models import Count

from bluebottle.members.models import Member

from bluebottle.clients.models import Client
from bluebottle.clients.utils import LocalTenant

from bluebottle.activities.models import Activity, Contributor
from bluebottle.initiatives.models import Initiative


for client in Client.objects.all():
    with LocalTenant(client):
        duplicate = Member.objects.annotate(
            lower=Lower('email')
        ).values('lower').annotate(count=Count('lower')).filter(count__gt=1)
        for result in duplicate:
            first = Member.objects.filter(email__iexact=result['lower']).order_by('date_joined').first()
            for duplicate in Member.objects.filter(
                email__iexact=result['lower']
            ).exclude(pk=first.pk).order_by('date_joined'):
                for activity in Activity.objects.filter(owner=duplicate):
                    activity.owner = first
                    activity.execute_triggers(send_messages=False)
                    activity.save()

                for contributor in Contributor.objects.filter(user=duplicate):
                    contributor.user = first
                    contributor.execute_triggers(send_messages=False)
                    contributor.save()

                for initiative in Initiative.objects.filter(owner=duplicate):
                    initiative.owner = first
                    initiative.execute_triggers(send_messages=False)
                    initiative.save()

                duplicate.anonymize()

from django.db.models.functions import Lower
from django.db.models import Count

from bluebottle.members.models import Member

from bluebottle.clients.models import Client
from bluebottle.clients.utils import LocalTenant

from bluebottle.activities.models import Activity, Contributor
from bluebottle.initiatives.models import Initiative


for client in Client.objects.all():
    with LocalTenant(client):
        duplicate = Member.objects.annotate(
            lower=Lower('email')
        ).values('lower').annotate(count=Count('lower')).filter(count__gt=1)
        for result in duplicate:
            first = Member.objects.filter(email__iexact=result['lower']).order_by('date_joined').first()
            for duplicate in Member.objects.filter(
                email__iexact=result['lower']
            ).exclude(pk=first.pk).order_by('date_joined'):
                for activity in Activity.objects.filter(owner=duplicate):
                    activity.owner = first
                    activity.execute_triggers(send_messages=False)
                    activity.save()

                for contributor in Contributor.objects.filter(user=duplicate):
                    contributor.user = first
                    contributor.execute_triggers(send_messages=False)
                    contributor.save()

                for initiative in Initiative.objects.filter(owner=duplicate):
                    initiative.owner = first
                    initiative.execute_triggers(send_messages=False)
                    initiative.save()

                duplicate.anonymize()
                duplicate.email = 'merged-{}@example.com'.format(first.pk)
                duplicate.save()

Make sure we remember to which the user was merged
Make sure we remember to which the user was merged
