Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
dc7b2f07
Commit
dc7b2f07
authored
Jun 06, 2014
by
Calen Pennington
Committed by
Sarina Canelake
Jun 09, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't create model objects to check the roles cache
parent
c8082770
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
10 deletions
+27
-10
common/djangoapps/student/roles.py
+27
-10
No files found.
common/djangoapps/student/roles.py
View file @
dc7b2f07
...
@@ -10,6 +10,27 @@ from student.models import CourseAccessRole
...
@@ -10,6 +10,27 @@ from student.models import CourseAccessRole
from
xmodule_django.models
import
CourseKeyField
from
xmodule_django.models
import
CourseKeyField
class
RoleCache
(
object
):
"""
A cache of the CourseAccessRoles held by a particular user
"""
def
__init__
(
self
,
user
):
self
.
_roles
=
set
(
CourseAccessRole
.
objects
.
filter
(
user
=
user
)
.
all
()
)
def
has_role
(
self
,
role
,
course_id
,
org
):
"""
Return whether this RoleCache contains a role with the specified role, course_id, and org
"""
return
any
(
access_role
.
role
==
role
and
access_role
.
course_id
==
course_id
and
access_role
.
org
==
org
for
access_role
in
self
.
_roles
)
class
AccessRole
(
object
):
class
AccessRole
(
object
):
"""
"""
Object representing a role with particular access to a resource
Object representing a role with particular access to a resource
...
@@ -94,12 +115,11 @@ class RoleBase(AccessRole):
...
@@ -94,12 +115,11 @@ class RoleBase(AccessRole):
# pylint: disable=protected-access
# pylint: disable=protected-access
if
not
hasattr
(
user
,
'_roles'
):
if
not
hasattr
(
user
,
'_roles'
):
user
.
_roles
=
set
(
# Cache a list of tuples identifying the particular roles that a user has
CourseAccessRole
.
objects
.
filter
(
user
=
user
)
.
all
()
# Stored as tuples, rather than django models, to make it cheaper to construct objects for comparison
)
user
.
_roles
=
RoleCache
(
user
)
role
=
CourseAccessRole
(
user
=
user
,
role
=
self
.
_role_name
,
course_id
=
self
.
course_key
,
org
=
self
.
org
)
return
user
.
_roles
.
has_role
(
self
.
_role_name
,
self
.
course_key
,
self
.
org
)
return
role
in
user
.
_roles
def
add_users
(
self
,
*
users
):
def
add_users
(
self
,
*
users
):
"""
"""
...
@@ -232,12 +252,9 @@ class UserBasedRole(object):
...
@@ -232,12 +252,9 @@ class UserBasedRole(object):
# pylint: disable=protected-access
# pylint: disable=protected-access
if
not
hasattr
(
self
.
user
,
'_roles'
):
if
not
hasattr
(
self
.
user
,
'_roles'
):
self
.
user
.
_roles
=
list
(
self
.
user
.
_roles
=
RoleCache
(
self
.
user
)
CourseAccessRole
.
objects
.
filter
(
user
=
self
.
user
)
.
all
()
)
role
=
CourseAccessRole
(
user
=
self
.
user
,
role
=
self
.
role
,
course_id
=
course_key
,
org
=
course_key
.
org
)
return
self
.
user
.
_roles
.
has_role
(
self
.
role
,
course_key
,
course_key
.
org
)
return
role
in
self
.
user
.
_roles
def
add_course
(
self
,
*
course_keys
):
def
add_course
(
self
,
*
course_keys
):
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment