# Path: access/signals.py

# 
# Path: access/models.py
# class UserAccess(models.Model):
# 
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, limit_choices_to=get_rule_ct_filter)
#     access_rule = GenericForeignKey('content_type', 'object_id')
#     character = models.ForeignKey(EVECharacter, on_delete=models.CASCADE)
# 
#     def __unicode__(self):
#         output = '%s access by rule for %s applying to %s' % (self.user, self.access_rule, self.character)
#         return output.encode('utf-8')
# 
#     class Meta:
#         permissions = (("site_access", "User has access to site."), ("manage_access", "User can manage site access."), ("audit_access", "User can view access granted per rule."))
#         verbose_name_plural = "User access"
# 
#     def set_rule(self, object):
#         if ContentType.objects.get_for_model(object) in RULE_CONTENT_TYPES:
#             self.object_id = object.pk
#             self.access_rule = object
#         else:
#             raise TypeError("Access rule must be of type %s" % RULE_CONTENT_TYPES)
# 
#     def get_rule(self):
#         return self.access_rule
# 
#     def verify(self):
#         self.save()


from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver, Signal
from models import ExtendedGroup, GroupApplication, AutoGroup
from access.signals import user_loses_access
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from tasks import assign_auto_group, assign_user_auto_group
from access.models import UserAccess
import logging
                break
            else:
                logger.debug("Group %s admin %s unsuitable owner replacement because lacks permission %s" % (g, a, perm))
        else:
            logger.info("Group %s owner %s has lost access and no suitable replacements found. Deleting." % (g, user))
            g.delete()
    for g in user.admin_groups.all():
        logger.info("Removing user %s from group %s administrators - user has lost access." % (user, g))
        g.admins.remove(user)
    logger.info("Removing all groups from user %s as user has lost access." % user)
    user.groups.clear()

@receiver(post_save, sender=AutoGroup)
def post_save_autogroup(sender, instance, *args, **kwargs):
    logger.debug("Received post_save signal from %s" % instance)
    assign_auto_group(instance)

@receiver(post_delete, sender=AutoGroup)
def post_delete_autogroup(sender, instance, *args, **kwargs):
    logger.debug("Received post_delete signal from %s" % instance)
    logger.info("Cascading %s deletion to base group %s" % (instance, instance.group))
    instance.group.delete()

@receiver(post_save, sender=UserAccess)
def post_save_useraccess(sender, instance, *args, **kwargs):
    logger.debug("Received post_save signal from %s" % instance)
    if AutoGroup.objects.filter(content_type=instance.content_type).filter(object_id=instance.object_id).exists():
        for ag in AutoGroup.objects.filter(content_type=instance.content_type).filter(object_id=instance.object_id):
            assign_user_auto_group(instance.user, ag)


# Path: access/signals.py

# 
# Path: access/models.py
# class UserAccess(models.Model):
# 
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, limit_choices_to=get_rule_ct_filter)
#     access_rule = GenericForeignKey('content_type', 'object_id')
#     character = models.ForeignKey(EVECharacter, on_delete=models.CASCADE)
# 
#     def __unicode__(self):
#         output = '%s access by rule for %s applying to %s' % (self.user, self.access_rule, self.character)
#         return output.encode('utf-8')
# 
#     class Meta:
#         permissions = (("site_access", "User has access to site."), ("manage_access", "User can manage site access."), ("audit_access", "User can view access granted per rule."))
#         verbose_name_plural = "User access"
# 
#     def set_rule(self, object):
#         if ContentType.objects.get_for_model(object) in RULE_CONTENT_TYPES:
#             self.object_id = object.pk
#             self.access_rule = object
#         else:
#             raise TypeError("Access rule must be of type %s" % RULE_CONTENT_TYPES)
# 
#     def get_rule(self):
#         return self.access_rule
# 
#     def verify(self):
#         self.save()


# Path: groupmanagement/signals.py
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver, Signal
from models import ExtendedGroup, GroupApplication, AutoGroup
from access.signals import user_loses_access
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from tasks import assign_auto_group, assign_user_auto_group
from access.models import UserAccess
import logging

                break
            else:
                logger.debug("Group %s admin %s unsuitable owner replacement because lacks permission %s" % (g, a, perm))
        else:
            logger.info("Group %s owner %s has lost access and no suitable replacements found. Deleting." % (g, user))
            g.delete()
    for g in user.admin_groups.all():
        logger.info("Removing user %s from group %s administrators - user has lost access." % (user, g))
        g.admins.remove(user)
    logger.info("Removing all groups from user %s as user has lost access." % user)
    user.groups.clear()

@receiver(post_save, sender=AutoGroup)
def post_save_autogroup(sender, instance, *args, **kwargs):
    logger.debug("Received post_save signal from %s" % instance)
    assign_auto_group(instance)

@receiver(post_delete, sender=AutoGroup)
def post_delete_autogroup(sender, instance, *args, **kwargs):
    logger.debug("Received post_delete signal from %s" % instance)
    logger.info("Cascading %s deletion to base group %s" % (instance, instance.group))
    instance.group.delete()

@receiver(post_save, sender=UserAccess)
def post_save_useraccess(sender, instance, *args, **kwargs):
    logger.debug("Received post_save signal from %s" % instance)
    if AutoGroup.objects.filter(content_type=instance.content_type).filter(object_id=instance.object_id).exists():
        for ag in AutoGroup.objects.filter(content_type=instance.content_type).filter(object_id=instance.object_id):
            assign_user_auto_group(instance.user, ag)


@receiver(post_delete, sender=UserAccess)