# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-09 14:15
from __future__ import unicode_literals

from django.db import migrations


def copy_texts(apps, schema_editor):
    ContentType = apps.get_model('contenttypes.ContentType')
    Contribution = apps.get_model('contributions.Contribution')
    ContributionText = apps.get_model('contributions.Text')
    ReplyKey = apps.get_model('contributions.ReplyKey')
    Text = apps.get_model('texts.Text')
    id_map = {}
    for text in Text.objects.all():
        ct = ContributionText.objects.create(text=text.text)
        contribution = Contribution.objects.create(
                container_type=text.container_type,
                container_id=text.container_id,
                contribution_id=ct.id,
                contribution_type=ContentType.objects.get_for_model(ct),
                author=text.author)
        contribution.time_created = text.time_created
        contribution.save()
        id_map[text.id] = contribution.id
        for rk in text.replykey_set.all():
            ReplyKey.objects.create(contribution=contribution, gestalt=rk.gestalt, key=rk.key)
    for text in Text.objects.filter(in_reply_to__isnull=False):
        contribution = Contribution.objects.get(id=id_map[text.id])
        contribution.in_reply_to_id = id_map[text.in_reply_to_id]
        contribution.save()


class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('contributions', '0001_initial'),
        ('texts', '0008_text_in_reply_to'),
    ]

    operations = [
        migrations.RunPython(copy_texts),
    ]

Add migration to copy texts to contributions
Add migration to copy texts to contributions
