Commits

schacki committed 9966729

Added some meta attributes to models and updates some tests

Comments (0)

Files changed (2)

permissions/models.py

 from django.db import models
 from django.contrib.auth.models import User
 from django.contrib.auth.models import Group
+from django.contrib.auth.models import Permission
 from django.contrib.contenttypes import generic
 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import ugettext_lazy as _
 from permissions.settings import ROLES_ENABLED
 from permissions.exceptions import RolesNotEnabled,MultiplePrincipalsAssigned
 
-
 class Permission(models.Model):
     """A permission which can be granted to users/groups and objects.
 
 
     def __unicode__(self):
         return "%s (%s)" % (self.name, self.codename)
-
+        
+    class Meta:
+        unique_together = ['name','codename']
+        verbose_name = _('permission')
+        verbose_name_plural = _('permissions')        
 
 class ObjectPermission(models.Model):
     """Grants permission for a role and an content object (optional).
             raise MultiplePrincipalsAssigned
         return super(ObjectPermission,self).save(*args,**kwargs)
 
+    class Meta:
+        unique_together = ['role','user','group','permission', 'content_type', 'content_id']
+        verbose_name = _('object permission')
+        verbose_name_plural = _('object permissions')
+     
+        
+
 class ObjectPermissionInheritanceBlock(models.Model):
     """Blocks the inheritance for specific permission and object.
 
     def __unicode__(self):
         return "%s / %s - %s" % (self.permission, self.content_type, self.content_id)
 
+    class Meta:
+        unique_together = ['permission', 'content_type', 'content_id']
+        verbose_name = _('object permission inheritance block')
+
+
 class Role(models.Model):
     """A role gets permissions to do something. Principals (users and groups)
     can only get permissions via roles.
                 content_id=None, content_type=None).exclude(user=None)
 
         return [prr.user for prr in prrs]
+        
+    class Meta:
+        unique_together = ['name']
+        verbose_name = _('role')
+        verbose_name_plural = _('roles')        
 
 class PrincipalRoleRelation(models.Model):
     """A role given to a principal (user or group). If a content object is
             self.group = principal
 
     principal = property(get_principal, set_principal)
+    
+    class Meta:
+        unique_together = ['role','user','group', 'content_type', 'content_id']
+        verbose_name = _('principal role relation')
+        verbose_name_plural = _('principal role relations')    

permissions/tests.py

         self.group=Group.objects.create(name='group 1')          
         self.user_2.groups.add(self.group)
         self.view_2 = permissions.utils.register_permission("View_2", "view_2")        
+        self.view_3 = permissions.utils.register_permission("View_3", "view_3")            
 
     def test_add_permissions(self):
         """
             creator_group = Group.objects.create(name="creator_group")            
             creator.groups.add(creator_group)
             
-            result = permissions.utils.has_permission(self.page_1, creator, "view_2")
+            result = permissions.utils.has_permission(self.page_1, creator, "view_3")
             self.assertEqual(result, False)
 
-            permissions.utils.grant_permission(self.page_1, creator_group, "view_2")
+            permissions.utils.grant_permission(self.page_1, creator_group, "view_3")
             
-            result = permissions.utils.has_permission(self.page_1, creator, "view_2")
+            result = permissions.utils.has_permission(self.page_1, creator, "view_3")
             self.assertEqual(result, True)                      
 
     def test_local_role(self):
             self.assertEqual(result, True)      
             
             
-            result = permissions.utils.has_permission(self.page_1, self.user_2, "view_2")
+            result = permissions.utils.has_permission(self.page_1, self.user_2, "view_3")
             self.assertEqual(result, False)
 
-            permissions.utils.grant_permission(self.page_1, self.group, 'view_2')
+            permissions.utils.grant_permission(self.page_1, self.group, 'view_3')
 
-            result = permissions.utils.has_permission(self.page_1, self.user_2, "view_2")
+            result = permissions.utils.has_permission(self.page_1, self.user_2, "view_3")
             self.assertEqual(result, True)                      
 
     def test_ineritance(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.