def get_groups_with_model_perms(model_cls, attach_perms=False):
    """
    Returns queryset of all ``Group`` objects with *any* model permissions for
    the given ``model_cls``.

    :param model_cls: persisted Django's ``Model`` class

    :param attach_perms: Default: ``False``. If set to ``True`` result would be
      dictionary of ``Group`` instances with permissions' codenames list as
      values. This would fetch groups eagerly!

    Example::

        >>> from django.contrib.flatpages.models import FlatPage
        >>> from guardian.shortcuts import assign_perm, get_groups_with_perms
        >>> from guardian.models import Group
        >>>
        >>> page = FlatPage.objects.create(title='Some page', path='/some/page/')
        >>> admins = Group.objects.create(name='Admins')
        >>> assign_perm('change_flatpage', admins, page)
        >>>
        >>> get_groups_with_perms(page)
        [<Group: admins>]
        >>>
        >>> get_groups_with_perms(page, attach_perms=True)
        {<Group: admins>: [u'change_flatpage']}

    """
    ctype = ContentType.objects.get_for_model(model_cls)
    if not attach_perms:
        # It's much easier without attached perms so we do it first if that is
        # the case
        group_filters = {
            'permissions__content_type': ctype
        }
        return Group.objects.filter(**group_filters).distinct()
    else:
        # TODO: Do not hit db for each group!
        groups = {}
        for group in get_groups_with_model_perms(model_cls):
            if group not in groups:
                groups[group] = sorted([perm.codename for perm in
                                        group.permissions.filter(content_type=ctype)])
        return groups
dataset/ETHPy150Open stackdio/stackdio/stackdio/core/shortcuts.py/get_groups_with_model_perms/original