Commits

d3f3nd3r  committed a54630e

add UserPermissionList and GroupPermissionList, rename stuff

  • Participants
  • Parent commits d4488ec

Comments (0)

Files changed (4)

File djangotoolbox/contrib/auth/backends.py

 from django.db import connection
 from django.contrib.auth.models import User, Permission, Group
 from django.contrib.contenttypes.models import ContentType
-from djangotoolbox.contrib.auth.models import PermissionList
+from djangotoolbox.contrib.auth.models import UserPermissionList, GroupPermissionList, GroupList
 
 
 class ModelBackend(object):
             return set()
         if not hasattr(user_obj, '_perm_cache'):
             try:
-                pl = PermissionList.objects.get(user=user_obj)
+                pl = UserPermissionList.objects.get(user=user_obj)
                 user_obj._perm_cache = set([u"%s.%s" % (p.content_type.app_label, p.codename) for p in pl.permission_list])
-            except PermissionList.DoesNotExist:
+            except UserPermissionList.DoesNotExist:
                 user_obj._perm_cache = list()
                 pass
             #user_obj._perm_cache.update(self.get_group_permissions(user_obj))

File djangotoolbox/contrib/auth/models.py

 from djangotoolbox.fields import ListField
 
         
-class PermissionList(models.Model):
+class UserPermissionList(models.Model):
     user = models.ForeignKey(User)
     _permission_list = ListField(models.ForeignKey(Permission))
 
         return self._permissions_cache
     permission_list = property(_get_permissions)
 
+class GroupPermissionList(models.Model):
+    user = models.ForeignKey(Group)
+    _permission_list = ListField(models.ForeignKey(Permission))
+
+    def _get_permissions(self):
+        if not hasattr(self, '_permissions_cache'):
+            perm_ids = self._permission_list
+            permissions = set()
+            if len(perm_ids) > 0:
+                # order_by() has to be used to override invalid default Permission filter
+                permissions.update(Permission.objects.filter(id__in=perm_ids).order_by('name'))
+            setattr(self, '_permissions_cache', permissions)
+            
+        return self._permissions_cache
+    permission_list = property(_get_permissions)
+
 class GroupList(models.Model):
     """
     GroupLists are used to map a list of groups to a user

File djangotoolbox/contrib/auth/tests.py

 from django.contrib.contenttypes.models import ContentType
 from django.test import TestCase
 
-from djangotoolbox.contrib.auth.models import PermissionList
+from djangotoolbox.contrib.auth.models import UserPermissionList
 from djangotoolbox.contrib.auth.utils import add_permission_to_user
 
 
         self.assertEqual(user.has_perm('auth.test'), False)
         user = User.objects.get(username='test')
         add_permission_to_user(user, perm)
-        self.assertEqual(PermissionList.objects.count(), 1)
-        pl = PermissionList.objects.all()[0]
+        self.assertEqual(UserPermissionList.objects.count(), 1)
+        pl = UserPermissionList.objects.all()[0]
         self.assertEqual(pl.permission_list , set([perm]))
         self.assertEqual(user.has_perm('auth.test'), True)
         

File djangotoolbox/contrib/auth/utils.py

 from django.contrib.auth.models import User, Permission
-from djangotoolbox.contrib.auth.models import PermissionList
+from djangotoolbox.contrib.auth.models import UserPermissionList, GroupPermissionList
 
 def add_permission_to_user(user, perm):
     try:
-        perm_list = PermissionList.objects.get(user=user)
-    except PermissionList.DoesNotExist:
-        perm_list = PermissionList.objects.create(user=user)
+        perm_list = UserPermissionList.objects.get(user=user)
+    except UserPermissionList.DoesNotExist:
+        perm_list = UserPermissionList.objects.create(user=user)
 
     perm_list._permission_list.append(perm.id)
     perm_list.save()