Commits

glassresistor  committed a13325d

Added clean() function and initial tests

  • Participants
  • Parent commits 9778e34

Comments (0)

Files changed (3)

File perms/backend.py

         p = ObjectPermission.objects.filter(content_type=ct,
                                             object_id=obj.id,
                                             user=user_obj)
-        return p.filter(action='can_%s' % perm).exists()
+        return p.filter(action=perm).exists()

File perms/models.py

 from django.db import models
 from django.contrib.auth.models import User
 from django.contrib.contenttypes.models import ContentType
+from django.core.exceptions import ValidationError
 
 
 PERMISSIONS = {
 
 
 class ObjectPermission(models.Model):
-    ACTION_CHOICES= (
-        # all objects
-        ('can_view', 'Can view'),
-        ('can_change', 'Can change'),
-        ('can_delete', 'Can delete'),
-        
-        
-    )
-    
+  
     user = models.ForeignKey(User)
-    action = models.CharField(max_length=32, choices=ACTION_CHOICES)
+    action = models.CharField(max_length=32,)
     content_type = models.ForeignKey(ContentType)
     object_id = models.PositiveIntegerField()
+    
+    def clean(self):
+        """
+        Test that Perms exist in the PERMISSIONS dictionary for content_type
+        """
+        object_model =  self.content_type.model
+        if PERMISSIONS.has_key(object_model):
+            perms = PERMISSIONS['__all__'] + PERMISSIONS['decision']
+        else:
+            perms = PERMISSIONS['__all__']
+        if not self.action in perms:
+            raise ValidationError(
+                'This permission is not avialable for content type.'
+            )
+        """
+        Test the existance of the object id
+        """
+        obj_model = ContentType.objects.get(model=object_model).model_class()
+        try:
+            obj_model.objects.get(pk=self.object_id)
+        except obj_model.DoesNotExist:
+            raise ValidationError(
+            'No object with this id exists for the model: %s' % object_model
+            )
+        
+        

File perms/tests.py

 from django.contrib.contenttypes.models import ContentType
 
 from perms.models import ObjectPermission
-
+from django.core.exceptions import ValidationError
 
 class SimpleTest(TestCase):
     def setUp(self):
         self.ct = ContentType.objects.get_for_model(self.user)
     
     def add_perm(self, name):
-        return ObjectPermission.objects.create(
+        op = ObjectPermission(
             user=self.user,
-            action='can_%s' % name,
+            action=name,
             content_type=self.ct,
             object_id=self.user.id,
         )
+        op.clean()
+        return op.save()
     
     def test_basic_addition(self):
-        self.assertFalse(self.user.has_perm('can_view', self.user))
+        self.assertFalse(self.user.has_perm('view', self.user))
         self.add_perm('view')
         self.assertTrue(self.user.has_perm('view', self.user))
+        
+    def test_bad_perm_addition(self):
+        self.assertFalse(self.user.has_perm('no_go', self.user))
+        try:
+            self.add_perm('no_go')
+        except ValidationError:
+            pass
+        self.assertFalse(self.user.has_perm('no_go', self.user))
+        
+    def test_bad_objectid_addition(self):
+        op = ObjectPermission(
+            user=self.user,
+            action='view',
+            content_type=self.ct,
+            object_id=9999,
+        )
+        try:
+            op.clean()
+            op.save()
+        except ValidationError:
+            pass
+        self.assertFalse(self.user.has_perm('view', self.user))