# Generated by Django 4.0.10 on 2023-05-07 09:39

# Django
import django.db.models.deletion
import django.utils.timezone
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models


def remove_none_title(apps, _schema_editor):
    ApplicationTitle = apps.get_model("membertools", "ApplicationTitle")
    Member = apps.get_model("membertools", "Member")
    Characer = apps.get_model("membertools", "Character")

    try:
        none_title = ApplicationTitle.objects.get(name="None")
    except ObjectDoesNotExist:
        # Nothing to do if the None title doesn't exist.
        return

    Member.objects.filter(awarded_title=none_title).update(awarded_title=None)
    Characer.objects.filter(applied_title=none_title).update(applied_title=None)


def restore_none_title(apps, _schema_editor):
    ApplicationTitle = apps.get_model("membertools", "ApplicationTitle")
    Member = apps.get_model("membertools", "Member")
    Characer = apps.get_model("membertools", "Character")

    none_title, _ = ApplicationTitle.objects.get_or_create(name="None", priority=0)

    Member.objects.filter(awarded_title=None).update(awarded_title=none_title)
    Characer.objects.filter(applied_title=None).update(applied_title=none_title)


class Migration(migrations.Migration):
    dependencies = [
        ("membertools", "0002_titlefilter"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="titlefilter",
            options={
                "verbose_name": "Smart Filter: Has Titles",
                "verbose_name_plural": "Smart Filter: Has Titles",
            },
        ),
        migrations.AddField(
            model_name="applicationform",
            name="require_applied",
            field=models.BooleanField(
                default=False, verbose_name="Require Applied Titles"
            ),
        ),
        migrations.AddField(
            model_name="applicationform",
            name="require_awarded",
            field=models.BooleanField(
                default=False, verbose_name="Require Awarded Titles"
            ),
        ),
        migrations.AlterField(
            model_name="applicationform",
            name="allow_applied",
            field=models.ManyToManyField(
                blank=True,
                related_name="+",
                to="membertools.applicationtitle",
                verbose_name="Applied Titles",
            ),
        ),
        migrations.AlterField(
            model_name="applicationform",
            name="allow_awarded",
            field=models.ManyToManyField(
                blank=True,
                related_name="+",
                to="membertools.applicationtitle",
                verbose_name="Awarded Titles",
            ),
        ),
        migrations.AlterField(
            model_name="applicationaction",
            name="action_on",
            field=models.DateTimeField(default=django.utils.timezone.now),
        ),
        migrations.AlterField(
            model_name="character",
            name="applied_title",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to="membertools.applicationtitle",
            ),
        ),
        migrations.AlterField(
            model_name="member",
            name="awarded_title",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to="membertools.applicationtitle",
            ),
        ),
        migrations.RunPython(
            code=remove_none_title,
            reverse_code=restore_none_title,
        ),
    ]
