"""
Remove the automatically generated site and home page
NOTE: This migration should be faked when converting a site to coop.
"""

from django.db import migrations


def remove_site(apps, schema):
    Site = apps.get_model('wagtailcore', 'site')
    Page = apps.get_model('wagtailcore', 'page')

    try:
        # Check if the site is premade from wagtailcore 0002_initial_data
        page = Page.objects.get(title="Welcome to your new Wagtail site!")

        site = Site.objects.get(root_page=page)
        site.delete()
        page.delete()
        # Fix the root page to show it has no children
        Page.objects.filter(pk=1).update(numchild=0)
    except Page.DoesNotExist:
        return


class Migration(migrations.Migration):

    dependencies = [
        ('wagtailcore', '__latest__'),
    ]

    operations = [
        migrations.RunPython(remove_site, migrations.RunPython.noop)
    ]
