|
53 | 53 | def _get_configured_target_course_id_strings(): |
54 | 54 | """Return configured target course run IDs as a list of strings.""" |
55 | 55 | default_from_settings = getattr(settings, SITE_CONFIG_KEY_TARGET_COURSES, []) |
| 56 | + site_config_enabled = configuration_helpers.is_site_configuration_enabled() |
56 | 57 | configured = configuration_helpers.get_value(SITE_CONFIG_KEY_TARGET_COURSES, default=default_from_settings) |
57 | 58 |
|
58 | 59 | if configured is None: |
@@ -209,42 +210,49 @@ def compute_audit_expiry_at(enrollment, variant, access_duration): |
209 | 210 | return content_availability_date + access_duration |
210 | 211 |
|
211 | 212 |
|
| 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 | + |
212 | 236 | def maybe_persist_audit_expiry_urgency_attributes(enrollment): |
213 | 237 | """Persist variant and audit_expiry_at for an eligible enrollment. |
214 | 238 |
|
215 | 239 | Safe + idempotent: if audit_expiry_at already exists, this is a no-op. |
216 | 240 | """ |
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): |
234 | 242 | return |
235 | 243 |
|
236 | 244 | # Idempotency: do not overwrite once set. |
237 | 245 | if get_persisted_audit_expiry_at(enrollment): |
238 | 246 | return |
239 | 247 |
|
| 248 | + target_course_ids = _get_configured_target_course_id_strings() |
| 249 | + variant = choose_variant(enrollment.user, target_course_ids) |
| 250 | + |
240 | 251 | access_duration = get_user_course_duration(enrollment.user, enrollment.course_overview) |
241 | 252 | if access_duration is None: |
242 | | - # Only apply if Course Duration Limits would normally apply. |
| 253 | + # Defensive: should_process_enrollment already checked this. |
243 | 254 | return |
244 | 255 |
|
245 | | - target_course_ids = _get_configured_target_course_id_strings() |
246 | | - variant = choose_variant(enrollment.user, target_course_ids) |
247 | | - |
248 | 256 | audit_expiry_at = compute_audit_expiry_at(enrollment, variant, access_duration) |
249 | 257 | if timezone.is_naive(audit_expiry_at): |
250 | 258 | audit_expiry_at = timezone.make_aware(audit_expiry_at, timezone=timezone.utc) |
|
0 commit comments