Implement 'from_string_or_404' util and its example usage.
This commit is contained in:
@@ -90,6 +90,7 @@ from util.organizations_helpers import (
|
|||||||
organizations_enabled,
|
organizations_enabled,
|
||||||
)
|
)
|
||||||
from util.string_utils import _has_non_ascii_characters
|
from util.string_utils import _has_non_ascii_characters
|
||||||
|
from util.course_key_utils import from_string_or_404
|
||||||
from xmodule.contentstore.content import StaticContent
|
from xmodule.contentstore.content import StaticContent
|
||||||
from xmodule.course_module import CourseFields
|
from xmodule.course_module import CourseFields
|
||||||
from xmodule.course_module import DEFAULT_START_DATE
|
from xmodule.course_module import DEFAULT_START_DATE
|
||||||
@@ -868,10 +869,7 @@ def course_info_handler(request, course_key_string):
|
|||||||
GET
|
GET
|
||||||
html: return html for editing the course info handouts and updates.
|
html: return html for editing the course info handouts and updates.
|
||||||
"""
|
"""
|
||||||
try:
|
course_key = from_string_or_404(course_key_string)
|
||||||
course_key = CourseKey.from_string(course_key_string)
|
|
||||||
except InvalidKeyError:
|
|
||||||
raise Http404
|
|
||||||
|
|
||||||
with modulestore().bulk_operations(course_key):
|
with modulestore().bulk_operations(course_key):
|
||||||
course_module = get_course_and_check_access(course_key, request.user)
|
course_module = get_course_and_check_access(course_key, request.user)
|
||||||
|
|||||||
30
common/djangoapps/util/course_key_utils.py
Normal file
30
common/djangoapps/util/course_key_utils.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
"""
|
||||||
|
Convenience methods for working with course objects
|
||||||
|
"""
|
||||||
|
from django.http import Http404
|
||||||
|
from opaque_keys import InvalidKeyError
|
||||||
|
from opaque_keys.edx.keys import CourseKey
|
||||||
|
|
||||||
|
|
||||||
|
def from_string_or_404(course_key_string):
|
||||||
|
"""
|
||||||
|
Gets CourseKey from the string passed as parameter.
|
||||||
|
|
||||||
|
Parses course key from string(containing course key) or raises 404 if the string's format is invalid.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
course_key_string(str): It is string containing the course key
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
CourseKey: A key that uniquely identifies a course
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
HTTP404: A 404 not found exception will be thrown if course_key_string's format is invalid
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
course_key = CourseKey.from_string(course_key_string)
|
||||||
|
except InvalidKeyError:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
return course_key
|
||||||
32
common/djangoapps/util/tests/test_course_key_utils.py
Normal file
32
common/djangoapps/util/tests/test_course_key_utils.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
"""
|
||||||
|
Tests for util.course_key_utils
|
||||||
|
"""
|
||||||
|
from nose.tools import assert_equals, assert_raises # pylint: disable=no-name-in-module
|
||||||
|
from util.course_key_utils import from_string_or_404
|
||||||
|
from opaque_keys.edx.keys import CourseKey
|
||||||
|
from django.http import Http404
|
||||||
|
|
||||||
|
|
||||||
|
def test_from_string_or_404():
|
||||||
|
|
||||||
|
#testing with split style course keys
|
||||||
|
assert_raises(
|
||||||
|
Http404,
|
||||||
|
from_string_or_404,
|
||||||
|
"/some.invalid.key/course-v1:TTT+CS01+2015_T0"
|
||||||
|
)
|
||||||
|
assert_equals(
|
||||||
|
CourseKey.from_string("course-v1:TTT+CS01+2015_T0"),
|
||||||
|
from_string_or_404("course-v1:TTT+CS01+2015_T0")
|
||||||
|
)
|
||||||
|
|
||||||
|
#testing with mongo style course keys
|
||||||
|
assert_raises(
|
||||||
|
Http404,
|
||||||
|
from_string_or_404,
|
||||||
|
"/some.invalid.key/TTT/CS01/2015_T0"
|
||||||
|
)
|
||||||
|
assert_equals(
|
||||||
|
CourseKey.from_string("TTT/CS01/2015_T0"),
|
||||||
|
from_string_or_404("TTT/CS01/2015_T0")
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user