Skip to content

Commit 71e9c9d

Browse files
fix: Refactor enrollment eligibility checks
Refactor enrollment processing logic to use a dedicated function for eligibility checks, improving code clarity and maintainability.
1 parent a0dd0db commit 71e9c9d

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

lms/djangoapps/experiments/audit_expiry_urgency.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
def _get_configured_target_course_id_strings():
5454
"""Return configured target course run IDs as a list of strings."""
5555
default_from_settings = getattr(settings, SITE_CONFIG_KEY_TARGET_COURSES, [])
56+
site_config_enabled = configuration_helpers.is_site_configuration_enabled()
5657
configured = configuration_helpers.get_value(SITE_CONFIG_KEY_TARGET_COURSES, default=default_from_settings)
5758

5859
if configured is None:
@@ -209,42 +210,49 @@ def compute_audit_expiry_at(enrollment, variant, access_duration):
209210
return content_availability_date + access_duration
210211

211212

213+
def should_process_enrollment(enrollment):
214+
"""Return True if enrollment is eligible for this experiment."""
215+
if not AUDIT_EXPIRY_URGENCY_V1_ENABLED.is_enabled():
216+
return False
217+
if not enrollment or not enrollment.user_id:
218+
log.warning('Audit expiry urgency: skipped (missing enrollment or user)')
219+
return False
220+
if not enrollment.is_active:
221+
return False
222+
if enrollment.mode != CourseMode.AUDIT:
223+
return False
224+
if not enrollment.course_overview:
225+
log.warning('Audit expiry urgency: skipped (missing course_overview)')
226+
return False
227+
if not is_target_course(enrollment.course_id):
228+
return False
229+
# Only apply if Course Duration Limits would normally apply.
230+
access_duration = get_user_course_duration(enrollment.user, enrollment.course_overview)
231+
if access_duration is None:
232+
return False
233+
return access_duration is not None
234+
235+
212236
def maybe_persist_audit_expiry_urgency_attributes(enrollment):
213237
"""Persist variant and audit_expiry_at for an eligible enrollment.
214238
215239
Safe + idempotent: if audit_expiry_at already exists, this is a no-op.
216240
"""
217-
missing_enrollment_or_user = (not enrollment or not getattr(enrollment, 'user_id', None))
218-
missing_course_overview = (not missing_enrollment_or_user and not enrollment.course_overview)
219-
220-
ineligible = any((
221-
not AUDIT_EXPIRY_URGENCY_V1_ENABLED.is_enabled(),
222-
missing_enrollment_or_user,
223-
not enrollment.is_active,
224-
enrollment.mode != CourseMode.AUDIT,
225-
missing_course_overview,
226-
not is_target_course(enrollment.course_id),
227-
))
228-
229-
if ineligible:
230-
if missing_enrollment_or_user:
231-
log.warning('Audit expiry urgency: skipped (missing enrollment or user)')
232-
elif missing_course_overview:
233-
log.warning('Audit expiry urgency: skipped (missing course_overview)')
241+
if not should_process_enrollment(enrollment):
234242
return
235243

236244
# Idempotency: do not overwrite once set.
237245
if get_persisted_audit_expiry_at(enrollment):
238246
return
239247

248+
target_course_ids = _get_configured_target_course_id_strings()
249+
variant = choose_variant(enrollment.user, target_course_ids)
250+
240251
access_duration = get_user_course_duration(enrollment.user, enrollment.course_overview)
241252
if access_duration is None:
242-
# Only apply if Course Duration Limits would normally apply.
253+
# Defensive: should_process_enrollment already checked this.
243254
return
244255

245-
target_course_ids = _get_configured_target_course_id_strings()
246-
variant = choose_variant(enrollment.user, target_course_ids)
247-
248256
audit_expiry_at = compute_audit_expiry_at(enrollment, variant, access_duration)
249257
if timezone.is_naive(audit_expiry_at):
250258
audit_expiry_at = timezone.make_aware(audit_expiry_at, timezone=timezone.utc)

0 commit comments

Comments
 (0)