Some changes from code review.
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
|
from functools import wraps
|
||||||
|
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
|
|
||||||
from xmodule.course_module import CourseDescriptor
|
from xmodule.course_module import CourseDescriptor
|
||||||
from xmodule.modulestore.django import modulestore
|
from xmodule.modulestore.django import modulestore
|
||||||
|
|
||||||
|
|
||||||
def check_course(course_must_be_open=True, course_required=True):
|
def check_course(course_must_be_open=True, course_required=True):
|
||||||
"""
|
"""
|
||||||
This is a decorator for views that are within a course.
|
This is a decorator for views that are within a course.
|
||||||
@@ -14,11 +17,26 @@ def check_course(course_must_be_open=True, course_required=True):
|
|||||||
If the course has not started, it raises a 404. This check
|
If the course has not started, it raises a 404. This check
|
||||||
can be skipped by setting course_must_be_open to False.
|
can be skipped by setting course_must_be_open to False.
|
||||||
|
|
||||||
If course_required is False, course_id is not required. If
|
Usually, a 404 will be raised if a course is not found. This
|
||||||
|
behavior can be overrided by setting course_required to false.
|
||||||
|
When course_required is False, course_id is not required. If
|
||||||
course_id is still provided, but is None, course will be
|
course_id is still provided, but is None, course will be
|
||||||
set to None.
|
set to None.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
This wrapper would be used on a function that has the following
|
||||||
|
entry in urls.py:
|
||||||
|
|
||||||
|
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/book$', 'staticbook.views.index'),
|
||||||
|
|
||||||
|
Where staticbook.views.index has the following parameters:
|
||||||
|
|
||||||
|
@check_course
|
||||||
|
def index(request, course):
|
||||||
|
# Notice that the parameter is course, not course_id
|
||||||
"""
|
"""
|
||||||
def inner_check_course(function):
|
def inner_check_course(function):
|
||||||
|
@wraps(function)
|
||||||
def wrapped_function(*args, **kwargs):
|
def wrapped_function(*args, **kwargs):
|
||||||
if course_required or 'course_id' in kwargs:
|
if course_required or 'course_id' in kwargs:
|
||||||
course_id = kwargs['course_id']
|
course_id = kwargs['course_id']
|
||||||
@@ -42,7 +60,7 @@ def check_course(course_must_be_open=True, course_required=True):
|
|||||||
|
|
||||||
# If no arguments were passed to the decorator, the function itself
|
# If no arguments were passed to the decorator, the function itself
|
||||||
# will be in course_must_be_open
|
# will be in course_must_be_open
|
||||||
if hasattr(course_must_be_open, '__call__'):
|
if callable(course_must_be_open):
|
||||||
function = course_must_be_open
|
function = course_must_be_open
|
||||||
course_must_be_open = True
|
course_must_be_open = True
|
||||||
return inner_check_course(function)
|
return inner_check_course(function)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from models import StudentModuleCache
|
|||||||
from student.models import UserProfile
|
from student.models import UserProfile
|
||||||
from multicourse import multicourse_settings
|
from multicourse import multicourse_settings
|
||||||
|
|
||||||
#from util.cache import cache #TODO: Where did this go? lib/util/cache no longer exists
|
from util.cache import cache
|
||||||
from student.models import UserTestGroup
|
from student.models import UserTestGroup
|
||||||
from courseware import grades
|
from courseware import grades
|
||||||
from courseware.decorators import check_course
|
from courseware.decorators import check_course
|
||||||
|
|||||||
Reference in New Issue
Block a user