Skip to content

Commit cf6a629

Browse files
fix: discussion tab should be None if discussion tab is disabled (#33861)
1 parent 299cafa commit cf6a629

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

lms/djangoapps/mobile_api/users/serializers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def to_representation(self, course_overview): # lint-amnesty, pylint: disable=a
2323
request = self.context.get('request')
2424
api_version = self.context.get('api_version')
2525
enrollment = CourseEnrollment.get_enrollment(user=self.context.get('request').user, course_key=course_id)
26-
2726
return {
2827
# identifiers
2928
'id': course_id,
@@ -74,7 +73,7 @@ def to_representation(self, course_overview): # lint-amnesty, pylint: disable=a
7473
'discussion_course',
7574
kwargs={'course_id': course_id},
7675
request=request,
77-
) if course_overview.is_discussion_tab_enabled() else None,
76+
) if course_overview.is_discussion_tab_enabled(request.user) else None,
7877

7978
# This is an old API that was removed as part of DEPR-4. We keep the
8079
# field present in case API parsers expect it, but this API is now

lms/djangoapps/mobile_api/users/tests.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
)
3737
from lms.djangoapps.mobile_api.utils import API_V1, API_V05, API_V2, API_V3
3838
from openedx.core.lib.courses import course_image_url
39+
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration
3940
from openedx.features.course_duration_limits.models import CourseDurationLimitConfig
4041
from openedx.features.course_experience.tests.views.helpers import add_course_mode
4142
from xmodule.course_block import DEFAULT_START_DATE # lint-amnesty, pylint: disable=wrong-import-order
@@ -713,3 +714,51 @@ def test_with_display_overrides(self, api_version):
713714
assert serialized['course']['number'] == self.course.display_coursenumber
714715
assert serialized['course']['org'] == self.course.display_organization
715716
self._expiration_in_response(serialized, api_version)
717+
718+
719+
@ddt.ddt
720+
class TestDiscussionCourseEnrollmentSerializer(UrlResetMixin, MobileAPITestCase, MilestonesTestCaseMixin):
721+
"""
722+
Tests discussion data in course enrollment serializer
723+
"""
724+
725+
def setUp(self):
726+
"""
727+
Setup data for test
728+
"""
729+
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_DISCUSSION_SERVICE': True}):
730+
super().setUp()
731+
self.login_and_enroll()
732+
self.request = RequestFactory().get('/')
733+
self.request.user = self.user
734+
735+
def get_serialized_data(self, api_version):
736+
"""
737+
Return data from CourseEnrollmentSerializer
738+
"""
739+
if api_version == API_V05:
740+
serializer = CourseEnrollmentSerializerv05
741+
else:
742+
serializer = CourseEnrollmentSerializer
743+
744+
return serializer(
745+
CourseEnrollment.enrollments_for_user(self.user)[0],
746+
context={'request': self.request, 'api_version': api_version},
747+
).data
748+
749+
@ddt.data(True, False)
750+
def test_discussion_tab_url(self, discussion_tab_enabled):
751+
"""
752+
Tests discussion tab url is None if tab is disabled
753+
"""
754+
config, _ = DiscussionsConfiguration.objects.get_or_create(context_key=self.course.id)
755+
config.enabled = discussion_tab_enabled
756+
config.save()
757+
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_DISCUSSION_SERVICE': True}):
758+
serialized = self.get_serialized_data(API_V2)
759+
discussion_url = serialized["course"]["discussion_url"]
760+
if discussion_tab_enabled:
761+
assert discussion_url is not None
762+
assert isinstance(discussion_url, str)
763+
else:
764+
assert discussion_url is None

openedx/core/djangoapps/content/course_overviews/models.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from simple_history.models import HistoricalRecords
2525

2626
from lms.djangoapps.courseware.model_data import FieldDataCache
27-
from lms.djangoapps.discussion import django_comment_client
2827
from openedx.core.djangoapps.catalog.models import CatalogIntegration
2928
from openedx.core.djangoapps.lang_pref.api import get_closest_released_language
3029
from openedx.core.djangoapps.models.course_details import CourseDetails
@@ -708,15 +707,17 @@ def get_all_course_keys(cls):
708707
"""
709708
return CourseOverview.objects.values_list('id', flat=True)
710709

711-
def is_discussion_tab_enabled(self):
710+
def is_discussion_tab_enabled(self, user=None):
712711
"""
713712
Returns True if course has discussion tab and is enabled
714713
"""
714+
# Importing here to avoid circular import
715+
from lms.djangoapps.discussion.plugins import DiscussionTab
715716
tabs = self.tab_set.all()
716717
# creates circular import; hence explicitly referenced is_discussion_enabled
717718
for tab in tabs:
718-
if tab.tab_id == "discussion" and django_comment_client.utils.is_discussion_enabled(self.id):
719-
return True
719+
if tab.tab_id == "discussion":
720+
return DiscussionTab.is_enabled(self, user)
720721
return False
721722

722723
@property

0 commit comments

Comments
 (0)