Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions openedx/core/djangoapps/discussions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from openedx_events.learning.data import CourseDiscussionConfigurationData
from openedx_events.learning.signals import COURSE_DISCUSSIONS_CHANGED

from openedx.core.djangoapps.course_apps.models import CourseAppStatus
from openedx.core.djangoapps.discussions.models import (
DiscussionsConfiguration,
DiscussionTopicLink,
Expand Down Expand Up @@ -108,5 +109,14 @@ def update_course_discussion_config(configuration: CourseDiscussionConfiguration
context_key=course_key,
).update(enabled=configuration.enabled)

# Also update CourseAppStatus to keep the Pages & Resources UI in sync.
# The update_course_apps_status task may run before this handler due to
# the COURSE_PUBLISH_TASK_DELAY countdown, caching a stale enabled value.
CourseAppStatus.update_status_for_course_app(
course_key=course_key,
app_id="discussion",
enabled=configuration.enabled,
)

Comment on lines +112 to +120
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kdmccormick, you’re absolutely right. Here’s what’s actually happening:

When a course is published (including after an import), two independent Celery tasks are triggered by the course_published signal. The first is update_discussions_settings_from_course_task, which reads the course tabs from the modulestore, derives enabled = not tab.is_hidden for the discussion tab, and passes it through CourseDiscussionConfigurationData. The COURSE_DISCUSSIONS_CHANGED event fires, and the handler updates DiscussionsConfiguration.enabled in the database.

The second is update_course_apps_status, which iterates over all available course apps and calls each app's is_enabled() method. For the discussion app, DiscussionCourseApp.is_enabled() calls DiscussionsConfiguration.is_enabled(course_key), reading directly from the DiscussionsConfiguration model. It then writes the result into CourseAppStatus via update_status_for_course_app().

The Pages & Resources MFE calls GET /api/course_apps/v1/apps/{course_id}. The serializer first checks CourseAppStatus (bulk-loaded via get_all_app_status_data_for_course). If no row exists, it falls back to is_course_app_enabled(), which tries CourseAppStatus.objects.get(course_key, app_id) and returns its enabled value if found, otherwise calls DiscussionCourseApp.is_enabled() which reads from DiscussionsConfiguration.is_enabled().

Locally, this was working because CELERY_ALWAYS_EAGER=True. However, in production with Celery workers, update_discussions_settings_from_course_task is triggered after update_course_apps_status due to the COURSE_PUBLISH_TASK_DELAY. As a result, update_course_apps_status ends up using stale values from DiscussionsConfiguration, so the data doesn’t get synced as expected.

I have added the code here. We have 2 scenarios:

New course: No CourseAppStatus row exists yet. The handler creates one with the correct enabled value.
Old course: A CourseAppStatus row already exists (created by update_course_apps_status on a previous publish, or by initialize_course_app_status on first MFE page load). The handler updates it.


COURSE_DISCUSSIONS_CHANGED.connect(handle_course_discussion_config_update)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Sync DiscussionsConfiguration.enabled and CourseAppStatus.enabled with
CourseOverviewTab.is_hidden.

When a course is imported, the discussion tab's is_hidden value is carried over
from the source course. However, DiscussionsConfiguration.enabled and
CourseAppStatus.enabled default to True and are not updated from the imported
tab state, causing a desync.

This migration reads each discussion tab from CourseOverviewTab (a DB cache of
course tabs) and sets both DiscussionsConfiguration.enabled and
CourseAppStatus.enabled to NOT is_hidden.
"""

from django.db import migrations


def sync_enabled_from_course_overview_tab(apps, schema_editor):
CourseOverviewTab = apps.get_model("course_overviews", "CourseOverviewTab")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad that the CourseOverviewTab model works! Thanks for the suggestion, Dave.

DiscussionsConfiguration = apps.get_model("discussions", "DiscussionsConfiguration")
CourseAppStatus = apps.get_model("course_apps", "CourseAppStatus")

discussion_tabs = CourseOverviewTab.objects.filter(tab_id="discussion").select_related("course_overview")

for tab in discussion_tabs.iterator():
course_key = tab.course_overview_id
expected_enabled = not tab.is_hidden
DiscussionsConfiguration.objects.filter(
context_key=course_key,
).exclude(
enabled=expected_enabled,
).update(enabled=expected_enabled)
CourseAppStatus.objects.filter(
course_key=course_key,
app_id="discussion",
).exclude(
enabled=expected_enabled,
).update(enabled=expected_enabled)
Comment on lines +33 to +38
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CourseAppStatus model is what the Pages & Resources MFE actually reads to determine whether the Discussion app shows as "Enabled" or "Disabled". When the API endpoint /api/course_apps/v1/apps/{course_id} is called, it first checks CourseAppStatus.enabled for the "discussion" app and only falls back to DiscussionsConfiguration.is_enabled()( if no row exists. So updating DiscussionsConfiguration.enabled alone is not enough; if a CourseAppStatus row already exists with enabled=True, the MFE will still show the discussion tab as enabled regardless of what DiscussionsConfiguration says. This is why we update both models in the migration to ensure the UI reflects the actual tab visibility from the course structure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, there are so many layers 😵

@Anas12091101 When a course is imported, does CourseAppStatus.enabled get updated from the modulestore tab's is_hidden state, like you did for DiscussionConfiguration.enabled? (If not, then am I correct to understand that this migration would only be a one-time fix for existing CourseAppStatuses, and the issue would continue for future imports?)



class Migration(migrations.Migration):

dependencies = [
("discussions", "0018_auto_20230904_1054"),
("course_overviews", "0030_backfill_new_catalog_courseruns"),
("course_apps", "0002_alter_historicalcourseappstatus_options"),
]

operations = [
migrations.RunPython(sync_enabled_from_course_overview_tab, migrations.RunPython.noop),
]
Loading