From 0c41576e1eaef271b532ffab109b3f85eea83da8 Mon Sep 17 00:00:00 2001 From: Bridger Maxwell Date: Mon, 16 Jul 2012 12:00:20 -0400 Subject: [PATCH] Some changes from code review. --- lms/djangoapps/courseware/decorators.py | 22 ++++++++++++++++++++-- lms/djangoapps/courseware/views.py | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lms/djangoapps/courseware/decorators.py b/lms/djangoapps/courseware/decorators.py index e59c349921..c078fa8c68 100644 --- a/lms/djangoapps/courseware/decorators.py +++ b/lms/djangoapps/courseware/decorators.py @@ -1,8 +1,11 @@ +from functools import wraps + from django.http import Http404 from xmodule.course_module import CourseDescriptor from xmodule.modulestore.django import modulestore + def check_course(course_must_be_open=True, course_required=True): """ 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 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 set to None. + + Usage + This wrapper would be used on a function that has the following + entry in urls.py: + + url(r'^courses/(?P[^/]+/[^/]+/[^/]+)/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): + @wraps(function) def wrapped_function(*args, **kwargs): if course_required or 'course_id' in kwargs: 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 # 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 course_must_be_open = True return inner_check_course(function) diff --git a/lms/djangoapps/courseware/views.py b/lms/djangoapps/courseware/views.py index f2a40c1c11..d691ee3b09 100644 --- a/lms/djangoapps/courseware/views.py +++ b/lms/djangoapps/courseware/views.py @@ -17,7 +17,7 @@ from models import StudentModuleCache from student.models import UserProfile 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 courseware import grades from courseware.decorators import check_course