# Generated by Django 3.2.7 on 2022-04-07 12:06

import django.db.models.expressions
import djangocms_text_ckeditor.fields
from django.db import migrations, models


def migrate_leaders(apps, schema_editor):
    Activity = apps.get_model("activities", "Activity")

    for activity in Activity.objects.all():
        if activity.leader:
            activity.leaders.add(activity.leader)
        if activity.deputy:
            activity.leaders.add(activity.deputy)
        activity.save()


def undo_migrate_leaders(apps, schema_editor):
    Activity = apps.get_model("activities", "Activity")

    for activity in Activity.objects.all():
        leaders = list(activity.leaders.all())
        if leaders:
            activity.leader = leaders[0]
            if len(leaders) > 1:
                activity.deputy = leaders[1]
            activity.save()


class Migration(migrations.Migration):

    dependencies = [
        ("members", "0007_auto_20220206_2155"),
        ("activities", "0010_activitymaterial"),
    ]

    operations = [
        migrations.AddField(
            model_name="activity",
            name="leaders",
            field=models.ManyToManyField(
                help_text="The community members leading this activity.",
                related_name="activity_leader",
                to="members.CommunityMember",
            ),
        ),
        migrations.RunPython(migrate_leaders, undo_migrate_leaders),
        migrations.AlterModelOptions(
            name="activity",
            options={
                "ordering": [
                    django.db.models.expressions.OrderBy(
                        django.db.models.expressions.F("end_date"),
                        nulls_first=True,
                    )
                ],
                "permissions": (
                    ("change_activity_lead", "Can change activity leader"),
                ),
                "verbose_name": "Working/Project Group",
                "verbose_name_plural": "Working/Project Groups",
            },
        ),
        migrations.RemoveField(
            model_name="activity",
            name="deputy",
        ),
        migrations.RemoveField(
            model_name="activity",
            name="leader",
        ),
        migrations.AlterField(
            model_name="activity",
            name="invitation_only",
            field=models.BooleanField(
                default=False,
                help_text="If set, only the activity leaders are able to add new members. Otherwise, community members can freely join and leave through their profile page or on the activity detail page.",
            ),
        ),
        migrations.RenameField(
            model_name="activity",
            old_name="description",
            new_name="abstract",
        ),
        migrations.AddField(
            model_name="activity",
            name="description",
            field=djangocms_text_ckeditor.fields.HTMLField(
                blank=True,
                help_text="More details about the activity.",
                max_length=10000,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="activity",
            name="show_abstract",
            field=models.BooleanField(
                default=True,
                help_text="Shall the abstract be shown on the detail page of the activity? You can disable this option if the abstract is contained in the description.",
            ),
        ),
        migrations.AlterField(
            model_name="activity",
            name="abstract",
            field=djangocms_text_ckeditor.fields.HTMLField(
                blank=True,
                help_text="An abstract on the activity.",
                max_length=4000,
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="activity",
            name="featured_topics",
            field=models.ManyToManyField(
                blank=True,
                help_text="Featured topics for the activity. The topics that you select here will be highlighted on the detail page of the group.",
                to="topics.Topic",
            ),
        ),
    ]
