<filename>api/licences/migrations/0014_licence_expiry_and_end_date.py
# Generated by Django 2.2.13 on 2020-07-16 08:30

from django.db import migrations, models

from api.core.helpers import add_months


def migrate(apps, schema_editor):
    Licence = apps.get_model("licences", "Licence")
    licences = Licence.objects.all()

    for licence in licences:
        licence.end_date = add_months(licence.start_date, licence.duration, "%Y-%m-%d")
        licence.save()


def reverse_migrate(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("licences", "0013_auto_20200709_1520"),
    ]

    operations = [
        migrations.AlterField(
            model_name="licence",
            name="status",
            field=models.CharField(
                choices=[
                    ("issued", "Issued"),
                    ("reinstated", "Reinstated"),
                    ("revoked", "Revoked"),
                    ("surrendered", "Surrendered"),
                    ("exhausted", "Exhausted"),
                    ("expired", "Expired"),
                    ("draft", "Draft"),
                    ("cancelled", "Cancelled"),
                ],
                default="draft",
                max_length=32,
            ),
        ),
        migrations.AddField(model_name="licence", name="end_date", field=models.DateField(null=True)),
        migrations.RunPython(migrate, reverse_migrate),
        migrations.AlterField(model_name="licence", name="end_date", field=models.DateField(blank=False, null=False)),
    ]
