# Path: protector/internals.py
# DEFAULT_ROLE = 1
# 
# ADD_PERMISSION_PERMISSION = 'add_permission'
# 
# VIEW_RESTRICTED_OBJECTS = 'view_restricted_objects'
# 
# VIEW_PERMISSION_NAME = 'protector.{0}'.format(VIEW_RESTRICTED_OBJECTS)
# 
# VIEW_GENERIC_GROUP_HISTORY = 'view_generic_group_history'
# 
# VIEW_OWNER_TO_PERM_HISTORY = 'view_owner_to_perm_history'
# 
# def get_user_ctype():
#     return ContentType.objects.get_for_model(get_user_model())
# 
# Path: protector/helpers.py
# def get_view_permission():
#     codename = VIEW_RESTRICTED_OBJECTS
#     global _view_perm
#     OwnerToPermission = apps.get_model('protector', 'OwnerToPermission')
#     if _view_perm is None:
#         ctype = ContentType.objects.get_for_model(OwnerToPermission)
#         _view_perm = Permission.objects.get(
#             codename=codename, content_type=ctype
#         )
#     return _view_perm
# 
# def check_responsible_reason(func):
#     @wraps(func)
#     def wrapper(*args, **kwargs):
#         responsible = kwargs.get('responsible')
#         reason = kwargs.get('reason') or (len(args) > 2 and args[2])
#         if responsible is not None and not isinstance(responsible, get_user_model()):
#             raise ImproperResponsibleInstancePassed
#         if not isinstance(reason, basestring) or not len(reason):
#             raise NoReasonSpecified
# 
#         return func(*args, **kwargs)
#     return wrapper
# 
# Path: protector/managers.py
# class GenericUserToGroupManager(
#     models.Manager.from_queryset(GenericUserToGroupQuerySet)
# ):
# class OwnerToPermissionManager(models.Manager.from_queryset(OwnerToPermissionQuerySet)):
# class UserGroupManager(models.Manager):
# class GroupUserManager(models.Manager):
# class OwnerPermissionManager(models.Manager):
#     def __init__(self, instance):
#     def add(self, groups, reason, roles=None, responsible=None):
#     def remove(self, group, reason, roles=None, responsible=None):
#     def get_queryset(self, group_type=None):
#     def by_ctype(self, group_type, roles=None):
#     def __init__(self, instance):
#     def get_queryset(self):
#     def add(self, users, reason, roles=None, responsible=None):
#     def remove(self, user, reason, roles=None, responsible=None):
#     def by_role(self, roles):
#     def __init__(self, instance):
#     def get_queryset(self):
#     def add(self, perm, reason, obj=None, roles=None, responsible=None):
#     def remove(self, perm, reason, obj=None, roles=None, responsible=None):
# 
# Path: protector/reserved_reasons.py
# def MEMBER_FK_UPDATE_REASON(field):
#     return 'User was added to group due to {} foreign key update'.format(field)
# 
# SELF_GROUP_ROLE_UPDATED = 'Self-role added for user own group'


from django.db import models, IntegrityError
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.contrib import auth
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
from django.utils.translation import ugettext_lazy as _
from mptt.models import MPTTModel, TreeForeignKey
from protector.internals import (
    DEFAULT_ROLE,
    ADD_PERMISSION_PERMISSION,
    VIEW_RESTRICTED_OBJECTS,
    VIEW_PERMISSION_NAME,
    VIEW_GENERIC_GROUP_HISTORY,
    VIEW_OWNER_TO_PERM_HISTORY,
    get_user_ctype,
)
from protector.helpers import get_view_permission, check_responsible_reason
from protector.managers import (
    GenericUserToGroupManager,
    OwnerToPermissionManager,
    OwnerPermissionManager,
    UserGroupManager,
    GroupUserManager,
    RestrictedManager,
    GenericGroupManager,
)
from protector.reserved_reasons import MEMBER_FK_UPDATE_REASON, SELF_GROUP_ROLE_UPDATED
                group_name=self.group_content_type,
                group_id=self.group_id,
            )

    objects = models.Manager()


class OwnerToPermission(AbstractOwnerToPermission):
    """
        This model is two-way generic many-to-many link from owner_object to owned object
        Multiple links from owner to object is supported i.e. different permissions
    """
    ADD_PERMISSION = ADD_PERMISSION_PERMISSION
    date_issued = models.DateTimeField(verbose_name=_('date issued'), auto_now_add=True)

    FIELDS_TO_IGNORE_FOR_HISTORY = ('id', 'date_issued', 'owner', 'content_object',)

    objects = OwnerToPermissionManager()

    class Meta:
        verbose_name = _('owner to permission link')
        verbose_name_plural = _('owner to permission links')
        index_together = (
            ['owner_content_type', 'owner_object_id'],
            ['content_type', 'object_id', 'permission'],
        )
        unique_together = (
            'content_type', 'object_id', 'owner_content_type', 'owner_object_id', 'permission'
        )
        permissions = (

# Path: protector/internals.py
# DEFAULT_ROLE = 1
# 
# ADD_PERMISSION_PERMISSION = 'add_permission'
# 
# VIEW_RESTRICTED_OBJECTS = 'view_restricted_objects'
# 
# VIEW_PERMISSION_NAME = 'protector.{0}'.format(VIEW_RESTRICTED_OBJECTS)
# 
# VIEW_GENERIC_GROUP_HISTORY = 'view_generic_group_history'
# 
# VIEW_OWNER_TO_PERM_HISTORY = 'view_owner_to_perm_history'
# 
# def get_user_ctype():
#     return ContentType.objects.get_for_model(get_user_model())
# 
# Path: protector/helpers.py
# def get_view_permission():
#     codename = VIEW_RESTRICTED_OBJECTS
#     global _view_perm
#     OwnerToPermission = apps.get_model('protector', 'OwnerToPermission')
#     if _view_perm is None:
#         ctype = ContentType.objects.get_for_model(OwnerToPermission)
#         _view_perm = Permission.objects.get(
#             codename=codename, content_type=ctype
#         )
#     return _view_perm
# 
# def check_responsible_reason(func):
#     @wraps(func)
#     def wrapper(*args, **kwargs):
#         responsible = kwargs.get('responsible')
#         reason = kwargs.get('reason') or (len(args) > 2 and args[2])
#         if responsible is not None and not isinstance(responsible, get_user_model()):
#             raise ImproperResponsibleInstancePassed
#         if not isinstance(reason, basestring) or not len(reason):
#             raise NoReasonSpecified
# 
#         return func(*args, **kwargs)
#     return wrapper
# 
# Path: protector/managers.py
# class GenericUserToGroupManager(
#     models.Manager.from_queryset(GenericUserToGroupQuerySet)
# ):
# class OwnerToPermissionManager(models.Manager.from_queryset(OwnerToPermissionQuerySet)):
# class UserGroupManager(models.Manager):
# class GroupUserManager(models.Manager):
# class OwnerPermissionManager(models.Manager):
#     def __init__(self, instance):
#     def add(self, groups, reason, roles=None, responsible=None):
#     def remove(self, group, reason, roles=None, responsible=None):
#     def get_queryset(self, group_type=None):
#     def by_ctype(self, group_type, roles=None):
#     def __init__(self, instance):
#     def get_queryset(self):
#     def add(self, users, reason, roles=None, responsible=None):
#     def remove(self, user, reason, roles=None, responsible=None):
#     def by_role(self, roles):
#     def __init__(self, instance):
#     def get_queryset(self):
#     def add(self, perm, reason, obj=None, roles=None, responsible=None):
#     def remove(self, perm, reason, obj=None, roles=None, responsible=None):
# 
# Path: protector/reserved_reasons.py
# def MEMBER_FK_UPDATE_REASON(field):
#     return 'User was added to group due to {} foreign key update'.format(field)
# 
# SELF_GROUP_ROLE_UPDATED = 'Self-role added for user own group'


# Path: protector/models.py
from django.db import models, IntegrityError
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.contrib import auth
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
from django.utils.translation import ugettext_lazy as _
from mptt.models import MPTTModel, TreeForeignKey
from protector.internals import (
    DEFAULT_ROLE,
    ADD_PERMISSION_PERMISSION,
    VIEW_RESTRICTED_OBJECTS,
    VIEW_PERMISSION_NAME,
    VIEW_GENERIC_GROUP_HISTORY,
    VIEW_OWNER_TO_PERM_HISTORY,
    get_user_ctype,
)
from protector.helpers import get_view_permission, check_responsible_reason
from protector.managers import (
    GenericUserToGroupManager,
    OwnerToPermissionManager,
    OwnerPermissionManager,
    UserGroupManager,
    GroupUserManager,
    RestrictedManager,
    GenericGroupManager,
)
from protector.reserved_reasons import MEMBER_FK_UPDATE_REASON, SELF_GROUP_ROLE_UPDATED

                group_name=self.group_content_type,
                group_id=self.group_id,
            )

    objects = models.Manager()


class OwnerToPermission(AbstractOwnerToPermission):
    """
        This model is two-way generic many-to-many link from owner_object to owned object
        Multiple links from owner to object is supported i.e. different permissions
    """
    ADD_PERMISSION = ADD_PERMISSION_PERMISSION
    date_issued = models.DateTimeField(verbose_name=_('date issued'), auto_now_add=True)

    FIELDS_TO_IGNORE_FOR_HISTORY = ('id', 'date_issued', 'owner', 'content_object',)

    objects = OwnerToPermissionManager()

    class Meta:
        verbose_name = _('owner to permission link')
        verbose_name_plural = _('owner to permission links')
        index_together = (
            ['owner_content_type', 'owner_object_id'],
            ['content_type', 'object_id', 'permission'],
        )
        unique_together = (
            'content_type', 'object_id', 'owner_content_type', 'owner_object_id', 'permission'
        )
        permissions = (

            (ADD_PERMISSION_PERMISSION, _('add permission')),