# Generated by Django 3.2.7 on 2022-01-11 08:01


# Disclaimer
# ----------
#
# Copyright (C) 2021 Helmholtz-Zentrum Hereon
# Copyright (C) 2020-2021 Helmholtz-Zentrum Geesthacht
#
# This file is part of django-academic-community and is released under the
# EUPL-1.2 license.
# See LICENSE in the root of the repository for full licensing details.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the EUROPEAN UNION PUBLIC LICENCE v. 1.2 or later
# as published by the European Commission.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# EUPL-1.2 license for more details.
#
# You should have received a copy of the EUPL-1.2 license along with this
# program. If not, see https://www.eupl.eu/.


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


def create_clm_assembly_21(apps, schema_editor):
    """Create the CLM Assembly 21 event if necessary."""
    import datetime as dt

    from django.forms.utils import from_current_timezone
    from psycopg2.extras import DateTimeTZRange

    from academic_community.models import PermissionModels

    Session = apps.get_model("programme", "Session")
    Event = apps.get_model("events", "Event")

    if Session.objects.all():
        Event.objects.get_or_create(
            name="CLM Assembly 2021",
            slug="clm-assembly-21",
            submission_closed=True,
            registration_closed=True,
            view_permission=PermissionModels.members,
            event_view_permission=PermissionModels.members,
            time_range=DateTimeTZRange(
                from_current_timezone(dt.datetime(2021, 9, 20, 8)),
                from_current_timezone(dt.datetime(2021, 9, 24, 18)),
            ),
        )


def reverse_clm_assembly_21(apps, schema_editor):
    """Dummy reverse function."""
    Event = apps.get_model("events", "Event")
    try:
        event = Event.objects.get(slug="clm-assembly-21")
    except Event.ObjectDoesNotExist:
        pass
    else:
        if event.orga_group:
            orga_group = event.orga_group
            event.orga_group = None
            event.save()
            orga_group.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("events", "0001_initial"),
        ("programme", "0008_alter_sessionmaterial_upload_material"),
    ]

    operations = [
        migrations.RunPython(create_clm_assembly_21, reverse_clm_assembly_21),
        migrations.AlterModelOptions(
            name="session",
            options={
                "ordering": ["start"],
                "permissions": (
                    (
                        "schedule_slots",
                        "Can set time and date for the session slots",
                    ),
                ),
            },
        ),
        migrations.AddField(
            model_name="contribution",
            name="event",
            field=models.ForeignKey(
                default=1,
                help_text="The corresponding community event",
                on_delete=django.db.models.deletion.CASCADE,
                to="events.event",
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="meetingroom",
            name="event",
            field=models.ForeignKey(
                default=1,
                help_text="The corresponding community event",
                on_delete=django.db.models.deletion.CASCADE,
                to="events.event",
            ),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name="session",
            name="event",
            field=models.ForeignKey(
                default=1,
                help_text="The corresponding community event",
                on_delete=django.db.models.deletion.CASCADE,
                to="events.event",
            ),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name="contribution",
            name="can_be_published",
            field=models.BooleanField(
                choices=[(True, "Yes"), (False, "No")],
                help_text="Can the abstract be made publicly available for non-communitymembers after the assembly under a <a href='https://creativecommons.org/licenses/by/4.0/'>CC BY 4.0</a> license?",
            ),
        ),
    ]
