Simplify permission lookup method. Add refresh class method to make sure

stored permissions are in sync with proxy permission.
Add __repr__ method.
This commit is contained in:
Roberto Rosario
2017-01-15 23:55:16 -04:00
parent ef2f4f929b
commit 23ffecdd30
6 changed files with 32 additions and 23 deletions

View File

@@ -13,7 +13,7 @@ from common.views import (
AssignRemoveView, SingleObjectCreateView, SingleObjectDeleteView,
SingleObjectListView
)
from permissions import PermissionNamespace
from permissions import PermissionNamespace, Permission
from permissions.models import StoredPermission
from .classes import ModelPermission
@@ -228,6 +228,7 @@ class ACLPermissionsView(AssignRemoveView):
return None
def left_list(self):
Permission.refresh()
return ACLPermissionsView.generate_choices(self.get_available_list())
def remove(self, item):

View File

@@ -45,12 +45,15 @@ class PermissionNamespace(object):
class Permission(object):
_stored_permissions_cache = {}
_permissions = {}
_stored_permissions_cache = {}
@classmethod
def invalidate_cache(cls):
cls._stored_permissions_cache = {}
def all(cls):
# Return sorted permisions by namespace.name
return sorted(
cls._permissions.values(), key=lambda x: x.namespace.name
)
@classmethod
def check_permissions(cls, requester, permissions):
@@ -67,6 +70,13 @@ class Permission(object):
raise PermissionDenied(_('Insufficient permissions.'))
@classmethod
def get(cls, pk, proxy_only=False):
if proxy_only:
return cls._permissions[pk]
else:
return cls._permissions[pk].stored_permission
@classmethod
def get_for_holder(cls, holder):
StoredPermission = apps.get_model(
@@ -76,19 +86,13 @@ class Permission(object):
return StoredPermission.get_for_holder(holder)
@classmethod
def all(cls):
# Return sorted permisions by namespace.name
return sorted(
cls._permissions.values(), key=lambda x: x.namespace.name
)
def invalidate_cache(cls):
cls._stored_permissions_cache = {}
@classmethod
def get(cls, get_dict, proxy_only=False):
if 'pk' in get_dict:
if proxy_only:
return cls._permissions[get_dict['pk']]
else:
return cls._permissions[get_dict['pk']].stored_permission
def refresh(cls):
for permission in cls.all():
permission.stored_permission
def __init__(self, namespace, name, label):
self.namespace = namespace
@@ -97,16 +101,15 @@ class Permission(object):
self.pk = self.uuid
self.__class__._permissions[self.uuid] = self
def __repr__(self):
return self.pk
def __unicode__(self):
return unicode(self.label)
def __str__(self):
return str(self.__unicode__())
@property
def uuid(self):
return '%s.%s' % (self.namespace.name, self.name)
@property
def stored_permission(self):
StoredPermission = apps.get_model(
@@ -125,3 +128,7 @@ class Permission(object):
self.uuid
] = stored_permission
return stored_permission
@property
def uuid(self):
return '%s.%s' % (self.namespace.name, self.name)

View File

@@ -13,7 +13,7 @@ class Command(BaseCommand):
for permission in StoredPermission.objects.all():
try:
Permission.get(
{'pk': '%s.%s' % (permission.namespace, permission.name)},
pk='{}.{}'.format(permission.namespace, permission.name),
proxy_only=True
)
except KeyError:

View File

@@ -26,7 +26,7 @@ class StoredPermission(models.Model):
super(StoredPermission, self).__init__(*args, **kwargs)
try:
self.volatile_permission = Permission.get(
{'pk': '%s.%s' % (self.namespace, self.name)},
pk='{}.{}'.format(self.namespace, self.name),
proxy_only=True
)
except KeyError:

View File

@@ -63,8 +63,7 @@ class RoleNewPermissionSerializer(serializers.Serializer):
try:
for pk in validated_data['permission_pk_list'].split(','):
get_dict = {'pk': pk}
stored_permission = Permission.get(get_dict=get_dict)
stored_permission = Permission.get(pk=pk)
validated_data['role'].permissions.add(stored_permission)
except KeyError as exception:

View File

@@ -97,7 +97,9 @@ class SetupRolePermissionsView(AssignRemoveView):
return get_object_or_404(Role, pk=self.kwargs['pk'])
def left_list(self):
Permission.refresh()
results = []
for namespace, permissions in itertools.groupby(StoredPermission.objects.exclude(id__in=self.get_object().permissions.values_list('pk', flat=True)), lambda entry: entry.namespace):
permission_options = [
(unicode(permission.pk), permission) for permission in permissions