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

# Generated by Django 4.1.5 on 2023-02-08 09:02

from django.db import migrations


def cleanup_deleted_users(apps, schema_editor):
    User = apps.get_model("weblate_auth", "User")
    Token = apps.get_model("authtoken", "Token")
    db_alias = schema_editor.connection.alias
    for user in (
        User.objects.using(db_alias)
        .filter(is_active=False, full_name="Deleted User")
        .select_related("profile")
    ):
        user.subscription_set.all().delete()
        user.profile.watched.clear()
        Token.objects.using(db_alias).filter(user=user).delete()


class Migration(migrations.Migration):
    dependencies = [
        ("accounts", "0022_alter_profile_linkedin"),
        ("authtoken", "0002_auto_20160226_1747"),
    ]

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