# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import sys
from django.db import models, migrations


def _forwards(apps, schema_editor):
    """
    Make sure that the MarkupItem model actually points
    to the correct proxy model, that implements the given language.
    """
    # Need to work on the actual models here.
    from fluent_contents.plugins.markup.models import LANGUAGE_MODEL_CLASSES
    from fluent_contents.plugins.markup.models import MarkupItem
    from django.contrib.contenttypes.models import ContentType

    ctype = ContentType.objects.get_for_model(MarkupItem)

    for language, proxy_model in LANGUAGE_MODEL_CLASSES.items():
        proxy_ctype = ContentType.objects.get_for_model(proxy_model, for_concrete_model=False)
        MarkupItem.objects.filter(
            polymorphic_ctype=ctype, language=language
        ).update(
            polymorphic_ctype=proxy_ctype
        )


def _backwards(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('fluent_contents', '0001_initial'),
        ('markup', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(_forwards, _backwards),
    ]

Make sure MarkupItem is stored under it's proxy class type ID
Make sure MarkupItem is stored under it's proxy class type ID

This is needed for proper formset saving/retrieval,
in combination with django-polymorphic 1.4-git
