Skip to content

Commit

Permalink
refactor: move modulestore code to the serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
viadanna committed Sep 4, 2024
1 parent b6ccc42 commit b06f103
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
10 changes: 10 additions & 0 deletions openedx/core/djangoapps/discussions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from rest_framework import serializers
from xmodule.modulestore.django import modulestore

from opaque_keys.edx.keys import CourseKey

from openedx.core.djangoapps.discussions.tasks import update_discussions_settings_from_course_task
from openedx.core.djangoapps.django_comment_common.models import CourseDiscussionSettings
from openedx.core.lib.courses import get_course_by_id
Expand Down Expand Up @@ -265,6 +267,14 @@ def update(self, instance: DiscussionsConfiguration, validated_data: dict) -> Di
course_overview_id=instance.context_key,
type='discussion'
).update(is_hidden=not instance.enabled)
# do the same for the modulestore representation
store = modulestore()
course = store.get_course(instance.context_key)
for tab in course.tabs:
if tab.tab_id == 'discussion':
tab.is_hidden = not instance.enabled
store.update_item(course, self.context['user_id'])
break
update_discussions_settings_from_course_task.delay(str(instance.context_key))
return instance

Expand Down
13 changes: 0 additions & 13 deletions openedx/core/djangoapps/discussions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,3 @@ def get_course_division_scheme(course_discussion_settings: CourseDiscussionSetti
):
division_scheme = CourseDiscussionSettings.NONE
return division_scheme


def show_discussion_tab(course_key, user_id: int) -> None:
"""
Toggle the visibility of the discussion tab in the course.
"""
store = modulestore()
course = store.get_course(course_key)
for tab in course.tabs:
if tab.tab_id == 'discussion':
tab.is_hidden = False
store.update_item(course, user_id)
break
4 changes: 1 addition & 3 deletions openedx/core/djangoapps/discussions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
DiscussionsConfigurationSerializer,
DiscussionsProvidersSerializer,
)
from .utils import show_discussion_tab


class DiscussionsConfigurationSettingsView(APIView):
Expand Down Expand Up @@ -121,9 +120,8 @@ def update_configuration_data(request, course_key_string):
new_provider_type = serializer.validated_data.get('provider_type', None)
if new_provider_type is not None and new_provider_type != configuration.provider_type:
check_course_permissions(course, request.user, 'change_provider')

serializer.save()
if configuration.is_enabled(course_key):
show_discussion_tab(course_key, request.user.id)
return serializer.data


Expand Down

0 comments on commit b06f103

Please sign in to comment.