# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2016-11-10 15:21
from __future__ import unicode_literals

from django.db import migrations
import itertools


def copy_conversations(apps, schema_editor):
    Article = apps.get_model('content.Article')
    ContentType = apps.get_model('contenttypes', 'ContentType')
    Conversation = apps.get_model('conversations.Conversation')
    Text = apps.get_model('texts.Text')

    for a in Article.objects.filter(public=False):
        conv = Conversation.objects.create(subject=a.title)
        for c in itertools.chain([a], a.comments.all()):
            Text.objects.create(
                    container_type=ContentType.objects.get_for_model(conv),
                    container_id=conv.id,
                    author=c.author, time_created=c.date_created, text=c.text)


class Migration(migrations.Migration):

    dependencies = [
        ('conversations', '0001_initial'),
        ('texts', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(copy_conversations),
    ]

Copy conversations