# Generated by Django 3.2.7 on 2022-03-29 21:33

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


def apply_license(apps, schema_editor):
    Contribution = apps.get_model("programme", "contribution")
    License = apps.get_model("uploaded_material", "License")
    for contribution in Contribution.objects.filter(can_be_published=True):
        contribution.license = License.objects.get(identifier="CC-BY-4.0")
        contribution.save()


def unapply_license(apps, schema_editor):
    """Unapply the license for contributions."""
    Contribution = apps.get_model("programme", "contribution")
    License = apps.get_model("uploaded_material", "License")
    cc_by = License.objects.get(identifier="CC-BY-4.0")
    for contribution in Contribution.objects.all():
        contribution.can_be_published = contribution.license == cc_by
        contribution.save()


class Migration(migrations.Migration):

    dependencies = [
        ("uploaded_material", "0003_auto_20220325_1513"),
        ("programme", "0014_alter_contribution_can_be_published"),
    ]

    operations = [
        migrations.AddField(
            model_name="contribution",
            name="license",
            field=models.ForeignKey(
                default=1,
                help_text="Select a license under which the abstract shall be made available.",
                on_delete=django.db.models.deletion.PROTECT,
                to="uploaded_material.license",
            ),
            preserve_default=False,
        ),
        migrations.RunPython(apply_license, unapply_license),
        migrations.RemoveField(
            model_name="contribution",
            name="can_be_published",
        ),
    ]
