# Generated by Django 3.2.7 on 2022-03-25 14:13

import django.db.models.deletion
from django.db import migrations, models


def create_default_licenses(apps, schema_editor):
    """Create default licenses."""
    License = apps.get_model("uploaded_material", "License")

    default_licenses = [
        ("All rights reserved", None),
        ("Creative Commons Attribution 4.0 International", "CC-BY-4.0"),
        (
            "Creative Commons Attribution Non Commercial 4.0 International",
            "CC-BY-NC-4.0",
        ),
        (
            "Creative Commons Attribution Non Commercial No Derivatives 4.0 International",
            "CC-BY-NC-ND-4.0",
        ),
        (
            "Creative Commons Attribution Non Commercial Share Alike 4.0 International",
            "CC-BY-NC-SA-4.0",
        ),
        (
            "Creative Commons Attribution No Derivatives 4.0 International",
            "CC-BY-ND-4.0",
        ),
        (
            "Creative Commons Attribution Share Alike 4.0 International",
            "CC-BY-SA-4.0",
        ),
        ("Creative Commons Zero v1.0 Universal", "CC0-1.0"),
    ]

    for i, (name, identifier) in enumerate(default_licenses):
        License.objects.get_or_create(
            name=name,
            identifier=identifier,
            public_default=(i == 0),
            internal_default=(i == 0),
        )


class Migration(migrations.Migration):

    dependencies = [
        ("uploaded_material", "0002_material_user"),
        ("programme", "0013_auto_20220324_1737"),
    ]

    operations = [
        migrations.CreateModel(
            name="License",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="The fully qualified name of the license, e.g. <i>Creative Commons Attribution 4.0 International</i>.",
                        max_length=200,
                    ),
                ),
                (
                    "identifier",
                    models.CharField(
                        help_text="The SPDX-Identifier of the license, e.g. <i>CC-BY-4.0</i>",
                        max_length=20,
                        null=True,
                    ),
                ),
                (
                    "public_default",
                    models.BooleanField(
                        default=False,
                        help_text="Is this the default license for public material on this site?",
                    ),
                ),
                (
                    "internal_default",
                    models.BooleanField(
                        default=False,
                        help_text="Is this the default license for internal material on this site?",
                    ),
                ),
                (
                    "active",
                    models.BooleanField(
                        default=True,
                        help_text="Can this license be used for material on the website?",
                    ),
                ),
            ],
        ),
        migrations.RunPython(
            create_default_licenses, migrations.RunPython.noop
        ),
        migrations.AddField(
            model_name="material",
            name="license",
            field=models.ForeignKey(
                default=1,
                help_text="What license shall be used for this material?",
                limit_choices_to={"active": True},
                on_delete=django.db.models.deletion.PROTECT,
                to="uploaded_material.license",
            ),
            preserve_default=False,
        ),
    ]
