# Generated by Django 3.0.1 on 2019-12-30 13:05

from django.db import migrations


def move_to_datacontent(apps, schema_editor):
    AssignedContent = apps.get_model("spider_base", "AssignedContent")
    LinkContent = apps.get_model("spider_base", "LinkContent")
    TravelProtection = apps.get_model("spider_base", "TravelProtection")
    DataContent = apps.get_model("spider_base", "DataContent")

    for a in AssignedContent.objects.filter(
        ctype__name="Link"
    ):
        d = DataContent(associated=a)
        content = LinkContent.objects.get(id=a.object_id)
        d.free_data["push"] = content.push
        d.save()

    for assigned in AssignedContent.objects.filter(
        ctype__code=TravelProtection._meta.model_name
    ):
        DataContent.objects.create(associated=assigned)
        content = TravelProtection.objects.get(id=assigned.object_id)
        if content.active:
            assigned.info += "active\x1e"

        assigned.info += \
            "travel_protection_type={}\x1e".format(content.login_protection)
        if content.start or content.stop:
            assigned.attachedtimespans.create(
                unique=False,
                name="active",
                start=content.start,
                stop=content.stop
            )
        assigned.protect_contents.set(content.protect_contents.all())
        assigned.protect_components.set(content.protect_components.all())
        assigned.save()


class Migration(migrations.Migration):

    dependencies = [
        ('spider_base', '0010_auto_20191230_1300'),
    ]

    operations = [
        migrations.RunPython(move_to_datacontent),
    ]
