Commits

schacki  committed b040271

Added post_delete signal handler that delets object permissions, if underlying object is deleted, plus TestCase with roles disabled

  • Participants
  • Parent commits 1763cc2

Comments (0)

Files changed (3)

File permissions/models.py

 from django.contrib.contenttypes import generic
 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import ugettext_lazy as _
+from django.db.models.signals import pre_save,post_save,pre_delete,post_delete
+from django.dispatch import receiver
+from django.utils.functional import curry
 
 # permissions imports
 import permissions.utils
         verbose_name = _('permission')
         verbose_name_plural = _('permissions')  
         
-from django.db.models.signals import pre_save,post_save,pre_delete
-from django.dispatch import receiver
-from django.utils.functional import curry
-
 if OBJECTS_PERM_CHECK:
     @receiver(pre_delete,sender=Permission)
     def remove_object_permission_checker_delete(sender,**kwargs):
         verbose_name = _('object permission')
         verbose_name_plural = _('object permissions')
      
+@receiver(post_delete)
+def remove_object_permissions(sender,**kwargs):
+    instance=kwargs['instance']
+    try:
+        permission=Objects.get(content=instance)
+    except:
+        return None
+    permission.delete()
         
+                
 
 class ObjectPermissionInheritanceBlock(models.Model):
     """Blocks the inheritance for specific permission and object.

File permissions/tests.py

 
             result = permissions.utils.has_permission(self.page_1, self.user_2, "view_2")
             self.assertEqual(result, False)            
+
+    def test_permission_after_object_delete(self):
+        '''
+        '''
+        
+        if permissions.settings.ROLES_ENABLED:     
+            pass
+        else:        
+            result = permissions.utils.has_permission(self.page_1, self.user, "view_2")
+            self.assertEqual(result, False)
+            
+            permission=Permission.objects.get(codename='view_2')
+            flatpage_type=ContentType.objects.get_for_model(self.page_1)
+            object_permission=ObjectPermission.objects.filter(permission=permission,content_type__pk=flatpage_type.id,content_id=self.page_1.id)
+            self.assertEqual(object_permission.count(),0)
+
+            result = permissions.utils.grant_permission(self.page_1, self.user, 'view_2')
+            self.assertEqual(result, True)
+            
+            object_permission=ObjectPermission.objects.filter(permission=permission,content_type__pk=flatpage_type.id,content_id=self.page_1.id)
+            self.assertEqual(object_permission.count(),1)        
+
+            result = permissions.utils.has_permission(self.page_1, self.user, "view_2")
+            self.assertEqual(result, True)
+
+            result = permissions.utils.remove_permission(self.page_1, self.user, "view_2")
+            self.assertEqual(result, True)
+
+            result = permissions.utils.has_permission(self.page_1, self.user, "view_2")
+            self.assertEqual(result, False)        
+            
+            object_permission=ObjectPermission.objects.filter(permission=permission,content_type__pk=flatpage_type.id,content_id=self.page_1.id)
+            self.assertEqual(object_permission.count(),0)           
             
     def test_has_permission_owner(self):
         """

File permissions/utils.py

         else:
             permissions_filter={'content_type':ctype, 'content_id':obj.id, 'permission__codename':codename}
         p = ObjectPermission.objects.filter(**permissions_filter).values("id")
-
         if len(p) > 0:
             result = True
             break