Commit 6e2d9883 by David Ormsbee

Make role lookup faster.

parent 403f03e7
...@@ -36,23 +36,25 @@ def register_access_role(cls): ...@@ -36,23 +36,25 @@ def register_access_role(cls):
class RoleCache(object): class RoleCache(object):
""" """
A cache of the CourseAccessRoles held by a particular user A cache of the CourseAccessRoles held by a particular user.
We do everything we can to avoid de-serialization overhead with CourseKeys,
since this is so frequently used.
""" """
def __init__(self, user): def __init__(self, user):
self._roles = set( self._roles = set(
CourseAccessRole.objects.filter(user=user).all() CourseAccessRole.objects.filter(user=user).values_list('role', 'course_id', 'org')
) )
def has_role(self, role, course_id, org): def has_role(self, role, course_id, org):
""" """
Return whether this RoleCache contains a role with the specified role, course_id, and org Return whether this RoleCache contains a role with the specified role, course_id, and org
""" """
return any( # OpaqueKeyField serialization is such that a blank in the DB gets
access_role.role == role and # translated to a None.
access_role.course_id == course_id and if course_id is None:
access_role.org == org course_id = ''
for access_role in self._roles return (role, unicode(course_id), org) in self._roles
)
class AccessRole(object): class AccessRole(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment