from django.db import migrations


def grant_admin_permissions(apps, schema_editor):
    User = apps.get_model('api', 'User')
    User.objects.filter(is_admin=True).update(
        can_measure=True,
        can_manually_measure=True,
        can_manage_entities=True,
        can_edit_documents=True,
    )
    # Also cover accounts created via createsuperuser (is_superuser=True but is_admin not set)
    User.objects.filter(is_superuser=True).update(
        is_admin=True,
        can_measure=True,
        can_manually_measure=True,
        can_manage_entities=True,
        can_edit_documents=True,
    )


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0004_user_can_edit_documents_user_can_manage_entities_and_more'),
    ]

    operations = [
        migrations.RunPython(grant_admin_permissions, migrations.RunPython.noop),
    ]
