<gh_stars>1-10
# Generated by Django 3.2.5 on 2021-09-23 16:05

from typing import TYPE_CHECKING

from apps.permissions.constants import (
    DEFAULT_INDOK_PERMISSIONS,
    DEFAULT_REGISTERED_USER_PERMISSIONS,
    INDOK,
    REGISTERED_USER,
)
from django.conf import settings
from django.db import migrations
from django.db.models.query_utils import Q

if TYPE_CHECKING:
    from apps.users import models as user_models
    from django.contrib.auth import models as auth_models


def assign_standard_permissions(apps, _):
    User: user_models.User = apps.get_model("users", "User")
    Group: auth_models.Group = apps.get_model("auth", "Group")
    Permission: auth_models.Permission = apps.get_model("auth", "Permission")
    indok_group, _ = Group.objects.get_or_create(name=INDOK)
    registered_user_group, _ = Group.objects.get_or_create(name=REGISTERED_USER)

    indok_query: Q = Q()
    for perm in DEFAULT_INDOK_PERMISSIONS:
        indok_query |= Q(content_type__app_label=perm[0], codename=perm[1])

    registered_query: Q = Q()
    for perm in DEFAULT_REGISTERED_USER_PERMISSIONS:
        registered_query |= Q(content_type__app_label=perm[0], codename=perm[1])

    if DEFAULT_INDOK_PERMISSIONS:
        indok_permissions = Permission.objects.filter(indok_query)
        indok_group.permissions.set(indok_permissions)
    if DEFAULT_REGISTERED_USER_PERMISSIONS:
        registered_permissions = Permission.objects.filter(registered_query)
        registered_user_group.permissions.set(registered_permissions)

    users = User.objects.exclude(username=settings.ANONYMOUS_USER_NAME)
    registered_user_group.user_set.set(users)
    indok_group.user_set.set(users.exclude(is_indok=False))


class Migration(migrations.Migration):

    dependencies = [
        ("users", "0012_user_is_indok"),
    ]

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

# Generated by Django 3.2.5 on 2021-09-23 16:05

from typing import TYPE_CHECKING

from apps.permissions.constants import (
    DEFAULT_INDOK_PERMISSIONS,
    DEFAULT_REGISTERED_USER_PERMISSIONS,
    INDOK,
    REGISTERED_USER,
)
from django.conf import settings
from django.db import migrations
from django.db.models.query_utils import Q

if TYPE_CHECKING:
    from apps.users import models as user_models
    from django.contrib.auth import models as auth_models


def assign_standard_permissions(apps, _):
    User: user_models.User = apps.get_model("users", "User")
    Group: auth_models.Group = apps.get_model("auth", "Group")
    Permission: auth_models.Permission = apps.get_model("auth", "Permission")
    indok_group, _ = Group.objects.get_or_create(name=INDOK)
    registered_user_group, _ = Group.objects.get_or_create(name=REGISTERED_USER)

    indok_query: Q = Q()
    for perm in DEFAULT_INDOK_PERMISSIONS:
        indok_query |= Q(content_type__app_label=perm[0], codename=perm[1])

    registered_query: Q = Q()
    for perm in DEFAULT_REGISTERED_USER_PERMISSIONS:
        registered_query |= Q(content_type__app_label=perm[0], codename=perm[1])

    if DEFAULT_INDOK_PERMISSIONS:
        indok_permissions = Permission.objects.filter(indok_query)
        indok_group.permissions.set(indok_permissions)
    if DEFAULT_REGISTERED_USER_PERMISSIONS:
        registered_permissions = Permission.objects.filter(registered_query)
        registered_user_group.permissions.set(registered_permissions)

    users = User.objects.exclude(username=settings.ANONYMOUS_USER_NAME)
    registered_user_group.user_set.set(users)
    indok_group.user_set.set(users.exclude(is_indok=False))


class Migration(migrations.Migration):

    dependencies = [
        ("users", "0012_user_is_indok"),
    ]

    operations = [migrations.RunPython(assign_standard_permissions, migrations.RunPython.noop)]
# Generated by Django 3.2.5 on 2021-09-23 16:05