# Generated by Django 2.2.13 on 2021-03-18 10:05
import datetime

from django.db import migrations
from django.utils import timezone

from base.models.enums.academic_calendar_type import AcademicCalendarTypes


def update_learning_unit_force_majeur_summary_edition_calendar(apps, shema_editor):
    AcademicYear = apps.get_model('base', 'academicyear')
    AcademicCalendar = apps.get_model('base', 'academiccalendar')

    now = timezone.now()
    current_academic_year = AcademicYear.objects.filter(start_date__lte=now, end_date__gte=now).last()
    if current_academic_year:
        qs = AcademicYear.objects.filter(year__gte=2022, year__lte=current_academic_year.year + 6)
        for targeted_academic_year in qs:
            AcademicCalendar.objects.update_or_create(
                reference=AcademicCalendarTypes.SUMMARY_COURSE_SUBMISSION_FORCE_MAJEURE.name,
                data_year=targeted_academic_year,
                defaults={
                    "title": "Modification fiches descriptives (cas de force majeure)",
                    "start_date": datetime.date(2020, 1, 1),
                    "end_date": datetime.date(2020, 1, 1),
                }
            )


class Migration(migrations.Migration):

    dependencies = [
        ('learning_unit', '0011_auto_20210317_1559'),
    ]

    operations = [
        migrations.RunPython(update_learning_unit_force_majeur_summary_edition_calendar, migrations.RunPython.noop, elidable=True),
    ]
