|
4 | 4 | These handlers ensure proper cleanup and consistency when models are deleted. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +from __future__ import annotations |
| 8 | + |
7 | 9 | import logging |
8 | 10 |
|
9 | 11 | from casbin_adapter.models import CasbinRule |
10 | | -from django.db.models.signals import post_delete |
| 12 | +from django.conf import settings |
| 13 | +from django.db.models.signals import post_delete, post_save |
11 | 14 | from django.dispatch import receiver |
12 | 15 |
|
13 | 16 | from openedx_authz.api.users import unassign_all_roles_from_user |
| 17 | +from openedx_authz.engine.utils import run_course_authoring_migration |
14 | 18 | from openedx_authz.models.core import ExtendedCasbinRule |
| 19 | +from openedx_authz.models.migrations import MigrationType, ScopeType |
| 20 | +from openedx_authz.models.subjects import UserSubject |
15 | 21 |
|
16 | 22 | try: |
| 23 | + from common.djangoapps.student.models import CourseAccessRole |
17 | 24 | from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_LMS_CRITICAL |
| 25 | + from openedx.core.djangoapps.waffle_utils.models import WaffleFlagCourseOverrideModel, WaffleFlagOrgOverrideModel |
| 26 | + from openedx.core.toggles import AUTHZ_COURSE_AUTHORING_FLAG |
18 | 27 | except ImportError: |
19 | 28 | USER_RETIRE_LMS_CRITICAL = None |
| 29 | + WaffleFlagCourseOverrideModel = None |
| 30 | + WaffleFlagOrgOverrideModel = None |
| 31 | + AUTHZ_COURSE_AUTHORING_FLAG = None |
| 32 | + CourseAccessRole = None |
| 33 | + |
20 | 34 |
|
21 | 35 | logger = logging.getLogger(__name__) |
22 | 36 |
|
@@ -82,3 +96,99 @@ def unassign_roles_on_user_retirement(sender, user, **kwargs): # pylint: disabl |
82 | 96 | # Only register the handler if the signal is available (i.e., running in Open edX) |
83 | 97 | if USER_RETIRE_LMS_CRITICAL is not None: |
84 | 98 | USER_RETIRE_LMS_CRITICAL.connect(unassign_roles_on_user_retirement) |
| 99 | + |
| 100 | + |
| 101 | +def handle_course_waffle_flag_change(sender, instance, **kwargs) -> None: |
| 102 | + """ |
| 103 | + Handle changes to course-level waffle flags. |
| 104 | +
|
| 105 | + When the authz.enable_course_authoring flag is changed for a course, |
| 106 | + trigger the appropriate migration run. Only trigger if automatic migration |
| 107 | + is enabled in the settings. |
| 108 | +
|
| 109 | + Args: |
| 110 | + sender: The model class (WaffleFlagCourseOverrideModel) |
| 111 | + instance: The flag override instance being saved |
| 112 | + **kwargs: Additional keyword arguments from the signal |
| 113 | + """ |
| 114 | + trigger_course_authoring_migration(sender=sender, instance=instance, scope_key=str(instance.course_id)) |
| 115 | + |
| 116 | + |
| 117 | +def handle_org_waffle_flag_change(sender, instance, **kwargs) -> None: |
| 118 | + """ |
| 119 | + Handle changes to organization-level waffle flags. |
| 120 | +
|
| 121 | + When the authz.enable_course_authoring flag is changed for an organization, |
| 122 | + trigger the appropriate migration run. Only trigger if automatic migration |
| 123 | + is enabled in the settings. |
| 124 | +
|
| 125 | + Args: |
| 126 | + sender: The model class (WaffleFlagOrgOverrideModel) |
| 127 | + instance: The flag override instance being saved |
| 128 | + **kwargs: Additional keyword arguments from the signal |
| 129 | + """ |
| 130 | + trigger_course_authoring_migration(sender=sender, instance=instance, scope_key=str(instance.org)) |
| 131 | + |
| 132 | + |
| 133 | +# Only register the handlers if the models are available (i.e., running in Open edX) |
| 134 | +if WaffleFlagCourseOverrideModel is not None: |
| 135 | + post_save.connect(handle_course_waffle_flag_change, sender=WaffleFlagCourseOverrideModel) |
| 136 | + |
| 137 | +if WaffleFlagOrgOverrideModel is not None: |
| 138 | + post_save.connect(handle_org_waffle_flag_change, sender=WaffleFlagOrgOverrideModel) |
| 139 | + |
| 140 | + |
| 141 | +def trigger_course_authoring_migration( |
| 142 | + sender: type[WaffleFlagCourseOverrideModel | WaffleFlagOrgOverrideModel], |
| 143 | + instance: WaffleFlagCourseOverrideModel | WaffleFlagOrgOverrideModel, |
| 144 | + scope_key: str, |
| 145 | +) -> None: |
| 146 | + """ |
| 147 | + Trigger a migration run in response to a waffle flag change. |
| 148 | +
|
| 149 | + Determines the migration direction from the flag state, guards against |
| 150 | + no-op saves, and delegates execution to ``run_course_authoring_migration`` |
| 151 | + which handles tracking and concurrent-run protection. |
| 152 | +
|
| 153 | + Args: |
| 154 | + sender: The model class (WaffleFlagCourseOverrideModel or WaffleFlagOrgOverrideModel). |
| 155 | + instance: The waffle flag instance that triggered the migration. |
| 156 | + scope_key (str): Course ID or organization name. |
| 157 | + """ |
| 158 | + if not settings.ENABLE_AUTOMATIC_AUTHZ_COURSE_AUTHORING_MIGRATION: |
| 159 | + logger.info("ENABLE_AUTOMATIC_AUTHZ_COURSE_AUTHORING_MIGRATION is set to False, skipping migration") |
| 160 | + return |
| 161 | + |
| 162 | + if instance.waffle_flag != AUTHZ_COURSE_AUTHORING_FLAG.name: |
| 163 | + return |
| 164 | + |
| 165 | + course_id_list, org_id, scope_type = None, None, None |
| 166 | + filter_kwargs = {"waffle_flag": AUTHZ_COURSE_AUTHORING_FLAG.name} |
| 167 | + if isinstance(instance, WaffleFlagCourseOverrideModel): |
| 168 | + filter_kwargs["course_id"] = instance.course_id |
| 169 | + course_id_list = [scope_key] |
| 170 | + scope_type = ScopeType.COURSE |
| 171 | + elif isinstance(instance, WaffleFlagOrgOverrideModel): |
| 172 | + filter_kwargs["org"] = instance.org |
| 173 | + org_id = scope_key |
| 174 | + scope_type = ScopeType.ORG |
| 175 | + |
| 176 | + prev_record = sender.objects.filter(**filter_kwargs).exclude(id=instance.id).order_by("-change_date").first() |
| 177 | + |
| 178 | + if prev_record and prev_record.enabled == instance.enabled: |
| 179 | + logger.info("No change in waffle flag, skipping course migration") |
| 180 | + return |
| 181 | + |
| 182 | + migration_type = MigrationType.FORWARD if instance.enabled else MigrationType.ROLLBACK |
| 183 | + |
| 184 | + logger.info("Triggering %s migration for %s:%s due to waffle flag change", migration_type, scope_type, scope_key) |
| 185 | + |
| 186 | + run_course_authoring_migration( |
| 187 | + migration_type=migration_type, |
| 188 | + scope_type=scope_type, |
| 189 | + scope_key=scope_key, |
| 190 | + course_access_role_model=CourseAccessRole, |
| 191 | + user_subject_model=UserSubject, |
| 192 | + course_id_list=course_id_list, |
| 193 | + org_id=org_id, |
| 194 | + ) |
0 commit comments