@@ -51,6 +51,8 @@ def run(self):
5151 self .mapped_channel .public = self .public
5252 self .mapped_channel .save_base (raw = True )
5353 annotate_label_bitmasks (self .mapped_root .get_descendants (include_self = True ))
54+ # Rather than set the ancestors fields after mapping, like it is done in Kolibri
55+ # here we set it during mapping as we are already recursing through the tree.
5456 set_channel_metadata_fields (self .mapped_channel .id , public = self .public )
5557
5658 def _map_model (self , source , Model ):
@@ -69,22 +71,30 @@ def _map_and_bulk_create_model(self, sources, Model):
6971
7072 Model .objects .bulk_create (cloned_sources , ignore_conflicts = True )
7173
72- def _map_node (self , source ):
73- return self ._map_model (source , kolibri_public_models .ContentNode )
74+ def _map_node (self , source , ancestors ):
75+ node = self ._map_model (source , kolibri_public_models .ContentNode )
76+ node .ancestors = ancestors
77+ return node
78+
79+ def _extend_ancestors (self , ancestors , new_ancestor ):
80+ return ancestors + [{"id" : new_ancestor .id , "title" : new_ancestor .title .replace ('"' , '\\ "' )}]
7481
7582 def _recurse_to_create_tree (
7683 self ,
7784 source ,
7885 nodes_by_parent ,
86+ ancestors ,
7987 ):
80- nodes_to_create = [self ._map_node (source )]
88+ nodes_to_create = [self ._map_node (source , ancestors )]
8189
8290 if source .kind == content_kinds .TOPIC and source .id in nodes_by_parent :
8391 children = sorted (nodes_by_parent [source .id ], key = lambda x : x .lft )
92+ ancestors = self ._extend_ancestors (ancestors , source )
8493 for child in children :
8594 nodes_to_create .extend (self ._recurse_to_create_tree (
8695 child ,
8796 nodes_by_parent ,
97+ ancestors ,
8898 ))
8999 return nodes_to_create
90100
@@ -110,6 +120,7 @@ def _map(
110120 self ,
111121 node ,
112122 batch_size ,
123+ ancestors = [],
113124 progress_tracker = None ,
114125 ):
115126 """
@@ -118,20 +129,24 @@ def _map(
118129 if node .rght - node .lft < batch_size :
119130 copied_nodes = self ._deep_map (
120131 node ,
132+ ancestors ,
121133 )
122134 if progress_tracker :
123135 progress_tracker .increment (len (copied_nodes ))
124136 return copied_nodes
125137 node_copy = self ._shallow_map (
126138 node ,
139+ ancestors ,
127140 )
141+ ancestors = self ._extend_ancestors (ancestors , node )
128142 if progress_tracker :
129143 progress_tracker .increment ()
130144 children = node .get_children ().order_by ("lft" )
131145 for child in children :
132146 self ._map (
133147 child ,
134148 batch_size ,
149+ ancestors = ancestors ,
135150 progress_tracker = progress_tracker ,
136151 )
137152 return [node_copy ]
@@ -189,8 +204,9 @@ def _copy_associated_objects(self, nodes):
189204 def _shallow_map (
190205 self ,
191206 node ,
207+ ancestors ,
192208 ):
193- mapped_node = self ._map_node (node )
209+ mapped_node = self ._map_node (node , ancestors )
194210
195211 mapped_node .save_base (raw = True )
196212
@@ -200,6 +216,7 @@ def _shallow_map(
200216 def _deep_map (
201217 self ,
202218 node ,
219+ ancestors ,
203220 ):
204221 source_nodes = node .get_descendants (include_self = True )
205222
@@ -212,6 +229,7 @@ def _deep_map(
212229 nodes_to_create = self ._recurse_to_create_tree (
213230 node ,
214231 nodes_by_parent ,
232+ ancestors ,
215233 )
216234
217235 mapped_nodes = kolibri_public_models .ContentNode .objects .bulk_create (nodes_to_create )
0 commit comments