# Generated by Django 2.0.2 on 2018-08-03 09:37

from django.db import migrations


def recreate_objects(apps, schema_editor):
    # We removed the old definition of these objects, need to create
    # a new object with a pointer back to that object, the underlying
    # data structure was unaffected
    ContentType = apps.get_model('contenttypes.ContentType')


    for model_name, new_model_name in [
            ('FundPage', 'BaseApplicationPage'),
    ]:
        content_type, _ = ContentType.objects.get_or_create(model=model_name.lower(), app_label='public_funds')

        model = apps.get_model('public_funds', model_name)
        new_model = apps.get_model('public_funds', new_model_name)
        for obj in new_model.objects.all():
            field_values = {}
            for field in obj._meta.fields:
                if field.name not in ['page_ptr']:
                    field_values[field.name] = getattr(obj, field.name)

            kwargs = {
                f'{new_model_name.lower()}_ptr': obj,
                'draft_title': obj.draft_title,
                'content_type': content_type,
            }
            field_values.update(**kwargs)
            new_obj = model(**field_values)
            new_obj.save()



class Migration(migrations.Migration):

    dependencies = [
        ('public_funds', '0007_add_rfp_models_for_public'),
    ]

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