# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Generated by Django 4.1.5 on 2023-01-31 09:16

from django.db import migrations
from django.db.models import Q

from weblate.auth.data import (
    SELECTION_ALL,
    SELECTION_ALL_PROTECTED,
    SELECTION_ALL_PUBLIC,
)

CLEANUP_SELECTIONS = {SELECTION_ALL, SELECTION_ALL_PROTECTED, SELECTION_ALL_PUBLIC}


def cleanup_selection(apps, schema_editor):
    Group = apps.get_model("weblate_auth", "Group")
    for group in (
        Group.objects.using(schema_editor.connection.alias)
        .filter(
            Q(project_selection__in=CLEANUP_SELECTIONS)
            | Q(language_selection__in=CLEANUP_SELECTIONS)
        )
        .iterator()
    ):
        if group.project_selection in CLEANUP_SELECTIONS:
            group.projects.clear()
        if group.language_selection in CLEANUP_SELECTIONS:
            group.languages.clear()


class Migration(migrations.Migration):
    dependencies = [
        ("weblate_auth", "0025_group_admins"),
    ]

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