Skip to content

Commit

Permalink
fix: Update url capture regex for course id
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Dec 19, 2024
1 parent 7f80c1a commit 64e4e40
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion openedx/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
# Note: these intentionally greedily grab all chars up to the next slash includingny pluses
# DHM: I really wanted to ensure the separators were the same (+ or /) but all patts tried had
# too many inadvertent side effects :-(

COURSE_KEY_PATTERN = r'(?P<course_key_string>[^/+]+(/|\+)[^/+]+(/|\+)[^/?]+)'
COURSE_ID_PATTERN = COURSE_KEY_PATTERN.replace('course_key_string', 'course_id')
COURSE_KEY_REGEX = COURSE_KEY_PATTERN.replace('P<course_key_string>', ':')

# This constant can be extended to take into account future urls to negate with the following pattern
# (?=\/(enroll|unenroll|other_patterns)$)
POSTFIXED_PATTERNS_TO_NEGATE = r'(?=\/enroll)$'
CAPTURED_CLEAN_COURSE_ID_PATTERN = r'^(?P<course_id>.*?)'

COURSE_PUBLISHED = 'published'
COURSE_UNPUBLISHED = 'unpublished'
11 changes: 10 additions & 1 deletion openedx/core/lib/request_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
from opaque_keys.edx.keys import CourseKey
from rest_framework.views import exception_handler

from openedx.core.constants import CAPTURED_CLEAN_COURSE_ID_PATTERN, POSTFIXED_PATTERNS_TO_NEGATE
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers

# accommodates course api urls, excluding any course api routes that do not fall under v*/courses, such as v1/blocks.
COURSE_REGEX = re.compile(fr'^(.*?/course(s)?/)(?!v[0-9]+/[^/]+){settings.COURSE_ID_PATTERN}')
CLEANED_COURSE_ID_REGEX = re.compile(f'{CAPTURED_CLEAN_COURSE_ID_PATTERN}{POSTFIXED_PATTERNS_TO_NEGATE}')

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -77,15 +79,22 @@ def course_id_from_url(url):

if match is None:
return None

course_id = match.group('course_id')

if course_id is None:
return None

cleaned_course_id = CLEANED_COURSE_ID_REGEX.match(course_id)
if cleaned_course_id:
cleaned_course_id = cleaned_course_id.group('course_id')
if cleaned_course_id:
course_id = cleaned_course_id
print(course_id, 'course_id_from_url 1')
try:
print(course_id, 'course_id_from_url 2')
return CourseKey.from_string(course_id)
except InvalidKeyError:
print(course_id, 'course_id_from_url 3')
return None


Expand Down
8 changes: 8 additions & 0 deletions openedx/core/lib/tests/test_request_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@ def test_course_id_from_url(self):
course_id = course_id_from_url('/api/courses/v1/courses/edX/maths/2020')
self.assertCourseIdFieldsMatch(course_id=course_id, org='edX', course='maths', run='2020')

course_id = course_id_from_url('/enterprise/5d566680-12a8-4b85-89d8-d9eacbf0f9eb/course/edX+math/enroll/')
print(course_id)
self.assertCourseIdFieldsMatch(course_id=course_id, org='edX', course='maths', run=None)

course_id = course_id_from_url('/enterprise/5d566680-12a8-4b85-89d8-d9eacbf0f9eb/course/edX+math+2020/enroll/')
self.assertCourseIdFieldsMatch(course_id=course_id, org='edX', course='maths', run='2020')

def assertCourseIdFieldsMatch(self, course_id, org, course, run):
""" Asserts that the passed-in course id matches the specified fields"""
print(course_id, org, course, run)
assert course_id.org == org
assert course_id.course == course
assert course_id.run == run
Expand Down

0 comments on commit 64e4e40

Please sign in to comment.