# Generated by Django 2.0.9 on 2018-10-21 13:13

from django.db import migrations


def merge_tags(apps, schema_editor):
    Tag = apps.get_model('taggit', 'Tag')
    ProjectTechnology = apps.get_model('home', 'ProjectTechnology')

    for tag in Tag.objects.all():
        if tag.name == tag.name.lower():
            # look for non lowe dupes
            dupes = Tag.objects.filter(name__iexact=tag.name).exclude(pk=tag.pk)
            if dupes:
                for dupe in dupes:
                    techs = ProjectTechnology.objects.filter(tag=dupe)
                    if techs:
                        techs.update(tag=tag)
                        assert ProjectTechnology.objects.filter(tag=dupe).count() == 0
                        dupe.delete()
    for tag in Tag.objects.all():
        tag.name = tag.name.lower()
        tag.save(update_fields=['name'])


class Migration(migrations.Migration):
    dependencies = [
        ('home', '0026_projectpage_reference_letter'),
        ('taggit', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(merge_tags),
    ]
