|
| 1 | +""" |
| 2 | +Signal handlers for ol-openedx-course-sync plugin |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +from common.djangoapps.course_action_state.models import CourseRerunState |
| 8 | +from django.core.exceptions import ValidationError |
| 9 | +from django.db.models.signals import post_save |
| 10 | +from django.dispatch import receiver |
| 11 | +from ol_openedx_course_sync.constants import COURSE_RERUN_STATE_SUCCEEDED |
| 12 | +from ol_openedx_course_sync.models import CourseSyncMap, CourseSyncParentOrg |
| 13 | +from ol_openedx_course_sync.tasks import async_course_sync |
| 14 | +from xmodule.modulestore.django import SignalHandler |
| 15 | + |
| 16 | +log = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +@receiver(SignalHandler.course_published) |
| 20 | +def listen_for_course_publish( |
| 21 | + sender, # noqa: ARG001 |
| 22 | + course_key, |
| 23 | + **kwargs, # noqa: ARG001 |
| 24 | +): |
| 25 | + """ |
| 26 | + Listen for course publish signal and trigger course sync task |
| 27 | + """ |
| 28 | + if not CourseSyncParentOrg.objects.filter(organization=course_key.org).exists(): |
| 29 | + return |
| 30 | + |
| 31 | + course_sync_map = CourseSyncMap.objects.filter(source_course=course_key).first() |
| 32 | + if not (course_sync_map and course_sync_map.target_courses): |
| 33 | + log.info("No mapping found for course %s. Skipping copy.", str(course_key)) |
| 34 | + return |
| 35 | + |
| 36 | + source_course = str(course_sync_map.source_course) |
| 37 | + user_id = None |
| 38 | + target_keys = [ |
| 39 | + key for key in course_sync_map.target_courses.strip().split(",") if key |
| 40 | + ] |
| 41 | + for target_course_key in target_keys: |
| 42 | + log.info( |
| 43 | + "Initializing async course content sync from %s to %s", |
| 44 | + source_course, |
| 45 | + target_course_key, |
| 46 | + ) |
| 47 | + # Call the async task to copy the course content |
| 48 | + async_course_sync.delay(user_id, source_course, target_course_key) |
| 49 | + |
| 50 | + |
| 51 | +@receiver(post_save, sender=CourseRerunState) |
| 52 | +def listen_for_course_rerun_state_post_save(sender, instance, **kwargs): # noqa: ARG001 |
| 53 | + """ |
| 54 | + Listen for `CourseRerunState` post_save and update target courses in `CourseSyncMap` |
| 55 | + """ |
| 56 | + if instance.state != COURSE_RERUN_STATE_SUCCEEDED: |
| 57 | + return |
| 58 | + |
| 59 | + if not CourseSyncParentOrg.objects.filter( |
| 60 | + organization=instance.source_course_key.org |
| 61 | + ).exists(): |
| 62 | + return |
| 63 | + |
| 64 | + try: |
| 65 | + course_sync_map, _ = CourseSyncMap.objects.get_or_create( |
| 66 | + source_course=instance.source_course_key |
| 67 | + ) |
| 68 | + except ValidationError: |
| 69 | + log.exception( |
| 70 | + "Failed to create CourseSyncMap for %s", |
| 71 | + instance.source_course_key, |
| 72 | + ) |
| 73 | + return |
| 74 | + |
| 75 | + target_courses = course_sync_map.target_course_keys |
| 76 | + target_courses.append(str(instance.course_key)) |
| 77 | + course_sync_map.target_courses = ",".join(target_courses) |
| 78 | + |
| 79 | + try: |
| 80 | + course_sync_map.save() |
| 81 | + except ValidationError: |
| 82 | + log.exception( |
| 83 | + "Failed to update CourseSyncMap for %s", |
| 84 | + instance.source_course_key, |
| 85 | + ) |
| 86 | + else: |
| 87 | + log.info( |
| 88 | + "Added course %s to target courses for %s", |
| 89 | + instance.course_key, |
| 90 | + instance.source_course_key, |
| 91 | + ) |
0 commit comments