1. Waldemar Kornewald
  2. djangotoolbox

Commits

d3f3nd3r  committed 24d59a4

add more generic get_objs function

  • Participants
  • Parent commits 659f348
  • Branches default

Comments (0)

Files changed (1)

File djangotoolbox/contrib/auth/models.py

View file
 
 from djangotoolbox.fields import ListField
 
-        
+def get_objs(obj_cls, obj_ids):
+    objs = set()
+    if len(obj_ids) > 0:
+        # order_by() has to be used to override invalid default Permission filter
+        objs.update(obj_cls .objects.filter(id__in=obj_ids).order_by('name'))
+    return objs
+
 class UserPermissionList(models.Model):
     user = models.ForeignKey(User)
     _fk_list = ListField(models.ForeignKey(Permission))
 
     def _get_permissions(self):
         if not hasattr(self, '_permissions_cache'):
-            perm_ids = self._fk_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)
-            
+            setattr(self, '_permissions_cache', get_objs(Permission, self._fk_list))            
         return self._permissions_cache
     permission_list = property(_get_permissions)
 
 
     def _get_permissions(self):
         if not hasattr(self, '_permissions_cache'):
-            perm_ids = self._fk_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)
-            
+            setattr(self, '_permissions_cache', get_objs(Permission, self._fk_list))            
         return self._permissions_cache
     permissions = property(_get_permissions)
 
     
     def _get_group_list(self):
         if not hasattr(self, '_groups_cache'):
-            group_ids = self._fk_list
-            groups = set()
-            if len(group_ids) > 0:
-                # order_by() has to be used to override invalid default Permission filter
-                groups.update(Group.objects.filter(id__in=group_ids))
-            setattr(self, '_groups_cache', groups)
-            
+            setattr(self, '_groups_cache', get_objs(Group, self._fk_list))
         return self._groups_cache
     groups = property(_get_group_list)
-
-