# -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-09-16 16:45


from django.conf import settings
from django.db import migrations, models
# import django.db.models.deletion
# import django_localflavor_us.models
# import nadine.models.core
# import taggit.managers

# Change the 'member' content-type to be 'userprofile'
# sql = """UPDATE django_content_type
#                  SET model = 'userprofile'
#                  WHERE model = 'member' AND
#                        app_label = 'nadine';"""

# Change the 'member' content-type back
# reverse_sql = """UPDATE django_content_type
#          SET model = 'member'
#          WHERE model = 'userprofile' AND
#                app_label = 'nadine';"""

def update_content_types(apps, schema_editor):
    ContentType = apps.get_model("contenttypes", "ContentType")
    Tag = apps.get_model("taggit", "Tag")
    TaggedItem = apps.get_model("taggit", "TaggedItem")
    UserProfile = apps.get_model("nadine", "UserProfile")

    member_content_type = ContentType.objects.filter(app_label="nadine", model="member").first()
    if not member_content_type:
        # Nothing to do here
        return

    profile_content_type = ContentType.objects.get_for_model(UserProfile)
    if not profile_content_type:
        raise Exception("Can't find 'UserProfile' ContentType")

    for ti in TaggedItem.objects.all():
        if ti.content_type == member_content_type:
            # print(".", end="")
            ti.content_type = profile_content_type
            ti.save()
        # else:
            # print("x", end="")


class Migration(migrations.Migration):

    dependencies = [
        ('taggit', '0002_auto_20150616_2121'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('nadine', '0014_remove_member_refs'),
    ]

    operations = [
        migrations.RenameModel(
            old_name='Member',
            new_name='UserProfile',
        ),
        migrations.RunPython(update_content_types),
    ]
