5
5
import logging
6
6
7
7
from common .djangoapps .course_action_state .models import CourseRerunState
8
+ from django .core .exceptions import ValidationError
8
9
from django .db .models .signals import post_save
9
10
from django .dispatch import receiver
10
11
from ol_openedx_course_sync .constants import COURSE_RERUN_STATE_SUCCEEDED
@@ -29,12 +30,15 @@ def listen_for_course_publish(
29
30
30
31
course_sync_map = CourseSyncMap .objects .filter (source_course = course_key ).first ()
31
32
if not (course_sync_map and course_sync_map .target_courses ):
32
- log .info ("No mapping found for course %s. Skipping copy." , course_key . id )
33
+ log .info ("No mapping found for course %s. Skipping copy." , str ( course_key ) )
33
34
return
34
35
35
36
source_course = str (course_sync_map .source_course )
36
37
user_id = None
37
- for target_course_key in course_sync_map .target_courses .split ("," ):
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 :
38
42
log .info (
39
43
"Initializing async course content sync from %s to %s" ,
40
44
source_course ,
@@ -57,30 +61,31 @@ def listen_for_course_rerun_state_post_save(sender, instance, **kwargs): # noqa
57
61
).exists ():
58
62
return
59
63
60
- # If source course is a target/child of any other course, we won't make it
61
- # a source/parent to avoid loops in course sync
62
- if CourseSyncMap . objects . filter (
63
- target_courses__contains = str ( instance . source_course_key )
64
- ). exists () :
65
- log .warning (
66
- "Course %s is already a target course. Skipping. " ,
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 " ,
67
71
instance .source_course_key ,
68
72
)
69
73
return
70
74
71
- course_sync_map , _ = CourseSyncMap .objects .get_or_create (
72
- source_course = instance .source_course_key
73
- )
74
- target_courses = (
75
- course_sync_map .target_courses .split ("," )
76
- if course_sync_map .target_courses
77
- else []
78
- )
75
+ target_courses = course_sync_map .target_course_keys
79
76
target_courses .append (str (instance .course_key ))
80
77
course_sync_map .target_courses = "," .join (target_courses )
81
- course_sync_map .save ()
82
- log .info (
83
- "Added course %s to target courses for %s" ,
84
- instance .course_key ,
85
- instance .source_course_key ,
86
- )
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