# Generated by Django 3.2.22 on 2023-11-29 13:49

import django.core.validators
from NEMO_publications.utils import parse_bibtex, parse_month_from_metadata
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("NEMO_publications", "0001_initial"),
    ]

    def set_default_month_forward(apps, schema_editor):
        PublicationMetadata = apps.get_model("NEMO_publications", "PublicationMetadata")
        for metadata in PublicationMetadata.objects.all():
            if metadata.bibtex:
                publication_parsed_metadata = parse_bibtex(metadata.bibtex)
                month = parse_month_from_metadata(publication_parsed_metadata.get("month"))
                if month:
                    metadata.month = month
                    metadata.save()

    def set_default_month_reverse(apps, schema_editor):
        pass

    operations = [
        migrations.AddField(
            model_name="publicationmetadata",
            name="month",
            field=models.PositiveIntegerField(
                blank=True,
                choices=[
                    (1, "JANUARY"),
                    (2, "FEBRUARY"),
                    (3, "MARCH"),
                    (4, "APRIL"),
                    (5, "MAY"),
                    (6, "JUNE"),
                    (7, "JULY"),
                    (8, "AUGUST"),
                    (9, "SEPTEMBER"),
                    (10, "OCTOBER"),
                    (11, "NOVEMBER"),
                    (12, "DECEMBER"),
                ],
                help_text="Publication month.",
                null=True,
            ),
        ),
        migrations.RunPython(set_default_month_forward, set_default_month_reverse),
        migrations.AlterField(
            model_name="publicationmetadata",
            name="year",
            field=models.PositiveIntegerField(
                help_text="Publication year.",
                validators=[
                    django.core.validators.MinValueValidator(1900),
                    django.core.validators.MaxValueValidator(9999),
                ],
            ),
        ),
    ]
