Skip to content

Commit 377bb07

Browse files
authored
Merge pull request #590 from open-craft/maxim/fixing-navigation-from-wiki-back-to-course-opencraft-palm
feat: set course for wiki based on the wiki_slug
2 parents b92baaa + 78eb0e7 commit 377bb07

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lms/djangoapps/course_wiki/middleware.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from openedx.features.enterprise_support.api import get_enterprise_consent_url
1616
from common.djangoapps.student.models import CourseEnrollment
1717

18+
from xmodule import modulestore
19+
1820

1921
class WikiAccessMiddleware(MiddlewareMixin):
2022
"""
@@ -55,6 +57,21 @@ def process_view(self, request, view_func, view_args, view_kwargs): # lint-amne
5557
course_id = course_id_from_url(request.path)
5658
wiki_path = request.path.partition('/wiki/')[2]
5759

60+
# if no wiki_path, can't get wiki_slug, so no point trying to look up
61+
# course_id by wiki_slug
62+
if not course_id and wiki_path:
63+
# wiki path always starts with wiki_slug
64+
wiki_slug = wiki_path.split('/')[0]
65+
66+
modstore = modulestore.django.modulestore()
67+
course_ids = modstore.get_courses_for_wiki(wiki_slug)
68+
# the above can return multiple courses, and to avoid ambiguity and
69+
# avoid pointing to wrong courses, we only set course_id if we've
70+
# got an exact match, i.e. only one course was returned for a
71+
# wiki_slug
72+
if len(course_ids) == 1:
73+
course_id = course_ids[0]
74+
5875
if course_id:
5976
# This is a /courses/org/name/run/wiki request
6077
course_path = f"/courses/{str(course_id)}"

lms/djangoapps/course_wiki/tests/test_middleware.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ def test_url_tranform(self):
3434
response = self.client.get('/courses/edx/math101/2014/wiki/math101/')
3535
self.assertContains(response, '/courses/edx/math101/2014/wiki/math101/_edit/')
3636
self.assertContains(response, '/courses/edx/math101/2014/wiki/math101/_settings/')
37+
38+
def test_finds_course_by_wiki_slug(self):
39+
"""Test that finds course by wiki slug, if course id is not present in the url."""
40+
response = self.client.get('/wiki/math101/')
41+
request = response.wsgi_request
42+
self.assertTrue(hasattr(request, 'course'))
43+
self.assertEqual(request.course.id, self.course_math101.id)

0 commit comments

Comments
 (0)