Use set comprehension instead of converting lists to sets

This patch takes advantage of python set comprehensions
syntax instead of constructing a list and converting
it to set later.
Also takes advantage of .isdisjoint function,
that returns True if two sets have a null intersection.
Should slightly improve performance and readability.

Closes-Bug: #1506925
Change-Id: Ia3d8b47efcf1b2280d7570e782fd196ce716ac8a
This commit is contained in:
Kirill Zaitsev 2015-08-25 01:02:28 +03:00 committed by Kirill Zaitsev
parent a4496c8cb7
commit 885cdd96c6
2 changed files with 8 additions and 8 deletions

View File

@ -223,8 +223,8 @@ class KeystoneBackend(object):
return set()
# TODO(gabrielhurley): Integrate policy-driven RBAC
# when supported by Keystone.
role_perms = set(["openstack.roles.%s" % role['name'].lower()
for role in user.roles])
role_perms = {"openstack.roles.%s" % role['name'].lower()
for role in user.roles}
services = []
for service in user.service_catalog:
@ -236,8 +236,8 @@ class KeystoneBackend(object):
in service.get('endpoints', [])]
if user.services_region in service_regions:
services.append(service_type.lower())
service_perms = set(["openstack.services.%s" % service
for service in services])
service_perms = {"openstack.services.%s" % service
for service in services}
return role_perms | service_perms
def has_perm(self, user, perm, obj=None):

View File

@ -298,12 +298,12 @@ class User(models.AbstractBaseUser, models.AnonymousUser):
Returns ``True`` or ``False``.
"""
admin_roles = [role.lower() for role in getattr(
admin_roles = {role.lower() for role in getattr(
settings,
'OPENSTACK_KEYSTONE_ADMIN_ROLES',
['admin'])]
user_roles = [role['name'].lower() for role in self.roles]
return True if set(admin_roles).intersection(user_roles) else False
['admin'])}
user_roles = {role['name'].lower() for role in self.roles}
return not admin_roles.isdisjoint(user_roles)
@property
def authorized_tenants(self):