from django.contrib.auth.models import User
from django.db.models import Q, QuerySet

from perms.models import Perm


def get_users_by_perm(perm: str) -> QuerySet:
    """Return user list by permission or superuser."""
    _perms = Perm.get_rights(perm)
    return User.objects.filter(Q(user_permissions=_perms) | Q(groups__permissions=_perms) | Q(is_superuser=True)).distinct()
