@@ -33,6 +33,16 @@ def handle_course_discussion_config_update(sender, configuration: CourseDiscussi
3333def update_course_discussion_config (configuration : CourseDiscussionConfigurationData ):
3434 """
3535 Update the database version of the configuration if it changes in the course structure.
36+
37+ This function accepts a discussion configuration object that represents the current
38+ configuration and applies that state to the database. It will go over the list of topic
39+ links in the configuration, find the corresponding topic link in the database and apply
40+ any changes if needed. If a new topic link has been introduced it will create an entry.
41+ If a topic has been removed, it will deactivate the entry.
42+
43+ When this runs on a new course it will create a new DiscussionConfiguration entry for
44+ the course.
45+
3646 Args:
3747 configuration (CourseDiscussionConfigurationData): configuration data for the course
3848 """
@@ -43,8 +53,6 @@ def update_course_discussion_config(configuration: CourseDiscussionConfiguration
4353 for topic_context in configuration .contexts
4454 }
4555 with transaction .atomic ():
46- # Go over existing topics in the database and update them if they have been changed.
47- # i.e. in case a unit display name has changed, or a unit has been removed, or otherwise made inaccessible
4856 log .info (f"Updating existing discussion topic links for { course_key } " )
4957 for topic_link in DiscussionTopicLink .objects .filter (
5058 context_key = course_key , provider_id = provider_id ,
@@ -54,16 +62,14 @@ def update_course_discussion_config(configuration: CourseDiscussionConfiguration
5462 # TODO: handle deleting topics that are no longer in use
5563 # currently this will simply not work for course-wide topics since deleting the link will
5664 # remove access to all posts in the topic.
57- # If the topic exists in the db, but not in the new configuration, it has been removed or otherwise
58- # made inaccessible.
5965 if topic_context is None :
6066 topic_link .enabled_in_context = False
6167 else :
6268 topic_link .enabled_in_context = True
6369 topic_link .title = topic_context .title
6470 topic_link .save ()
6571 log .info (f"Creating new discussion topic links for { course_key } " )
66- # If there are any topic contexts left here, they are new or not present in DiscussionTopicLink
72+
6773 DiscussionTopicLink .objects .bulk_create ([
6874 DiscussionTopicLink (
6975 context_key = course_key ,
@@ -76,7 +82,6 @@ def update_course_discussion_config(configuration: CourseDiscussionConfiguration
7682 for topic_context in new_topic_map .values ()
7783 ])
7884
79- # If this is a new course save changes to the model.
8085 if not DiscussionsConfiguration .objects .filter (context_key = course_key ).exists ():
8186 log .info (f"Course { course_key } doesn't have discussion configuration model yet. Creating a new one." )
8287 DiscussionsConfiguration (
@@ -87,7 +92,6 @@ def update_course_discussion_config(configuration: CourseDiscussionConfiguration
8792 enable_graded_units = configuration .enable_graded_units ,
8893 unit_level_visibility = configuration .unit_level_visibility ,
8994 ).save ()
90- # If the model already exists, then it's already up-to-date.
9195
9296
9397COURSE_DISCUSSIONS_UPDATED .connect (handle_course_discussion_config_update )
0 commit comments