# -*- coding: utf-8 -*-
# Generated by Django 1.11.22 on 2019-08-05 17:09
from __future__ import unicode_literals

from django.db import migrations


def migrate_collections_navigation(apps, schema_editor):
    Journal = apps.get_model("journal", "Journal")
    IssueType = apps.get_model("journal", "IssueType")
    NavigationItem = apps.get_model("cms", "NavigationItem")
    ContentType = apps.get_model("contenttypes", "ContentType")
    for journal in Journal.objects.all():
        if journal.nav_collections:
            issue_type = IssueType.objects.get(
                journal=journal,
                code="collection",
            )
            content_type = ContentType.objects.get(app_label="journal", model="Journal")
            defaults = {
                "link_name": issue_type.pretty_name + "s",
                "link": "/collections/%s" % (issue_type.code),
            }
            nav, created = NavigationItem.objects.get_or_create(
                content_type=content_type,
                object_id=issue_type.pk,
                defaults=defaults,
            )


class Migration(migrations.Migration):
    dependencies = [
        ("journal", "0034_migrate_issue_types"),
        ("cms", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(
            migrate_collections_navigation, reverse_code=migrations.RunPython.noop
        ),
    ]
