3939
4040def _create_site_category (site , language ):
4141 category_name = f"{ site .name } -Migrated-{ language } "
42- category = Category .objects .language (language ).create (
43- name = category_name ,
42+ category = (
43+ Category .objects .language (language )
44+ .filter (translations__name = category_name )
45+ .first ()
4446 )
45- category .save ()
47+ if not category :
48+ category = Category .objects .language (language ).create (
49+ name = category_name ,
50+ )
4651 return category
4752
4853
4954def create_new_alias_for_source_plugins (old_plugin , new_alias_grouper ):
50-
5155 global src_alias_count
5256
5357 try :
54- page_details = PageData .objects .get (page_id = old_plugin .placeholder .source .page .id , language = old_plugin .language )
58+ page_details = PageData .objects .get (
59+ page_id = old_plugin .placeholder .source .page .id , language = old_plugin .language
60+ )
5561
5662 # get published/draft pages for the old Src plugin
5763 src_plugin_pagecontents = PageContent ._base_manager .filter (
58- page_id = page_details .page_id ,
59- language = old_plugin .language
64+ page_id = page_details .page_id , language = old_plugin .language
6065 )
6166
6267 for src_plugin_pagecontent in src_plugin_pagecontents :
@@ -65,14 +70,18 @@ def create_new_alias_for_source_plugins(old_plugin, new_alias_grouper):
6570 # Get the placeholder with slot name from the published
6671 src_plugin_placeholder = Placeholder .objects .get (
6772 slot = old_plugin .placeholder .slot ,
68- content_type = ContentType .objects .get (app_label = 'cms' , model = 'pagecontent' ),
69- object_id = src_plugin_pagecontent .id
73+ content_type = ContentType .objects .get (
74+ app_label = "cms" , model = "pagecontent"
75+ ),
76+ object_id = src_plugin_pagecontent .id ,
7077 )
7178
7279 except ObjectDoesNotExist :
7380 logger .error (
74- "Error placeholder for the plugin {} Doesn't exist in published pagecontent {}"
75- .format (old_plugin .id , src_plugin_pagecontent .id ))
81+ "Error placeholder for the plugin {} Doesn't exist in published pagecontent {}" .format (
82+ old_plugin .id , src_plugin_pagecontent .id
83+ )
84+ )
7685 return
7786 # check if the source plugin exists at the same position in the published/draft page
7887 try :
@@ -84,18 +93,24 @@ def create_new_alias_for_source_plugins(old_plugin, new_alias_grouper):
8493 )
8594 except ObjectDoesNotExist :
8695 logger .error (
87- "Error plugin {} Doesn't exist at the position in placeholder {}"
88- .format (old_plugin .id , src_plugin_placeholder .id ))
96+ "Error plugin {} Doesn't exist at the position in placeholder {}" .format (
97+ old_plugin .id , src_plugin_placeholder .id
98+ )
99+ )
89100
90101 # Add the count to check total source plugins traversed to migrate to cms4
91102 src_alias_count += 1
92103 if src_plugin :
93104 # if the plugin of the same plugin type is found in the page and in same placeholder holder
94105 # at the same position create the cms4 Alias plugin
95- create_new_alias_plugin (src_plugin , new_alias_grouper , is_src_plugin = True )
106+ create_new_alias_plugin (
107+ src_plugin , new_alias_grouper , is_src_plugin = True
108+ )
96109
97110 except ObjectDoesNotExist :
98- logger .error ("pagedata object doesn;t exist for plugin {}" .format (old_plugin .id ))
111+ logger .error (
112+ "pagedata object doesn;t exist for plugin {}" .format (old_plugin .id )
113+ )
99114 return
100115
101116
@@ -118,7 +133,7 @@ def create_new_alias_plugin(old_plugin, new_alias_grouper, is_src_plugin=False):
118133 # Create a new cms4 alias plugin in place of the old cms 3 plugin
119134 new_plugin = add_plugin (
120135 old_plugin .placeholder ,
121- ' Alias' ,
136+ " Alias" ,
122137 language = old_plugin .language ,
123138 alias = new_alias_grouper ,
124139 )
@@ -141,19 +156,25 @@ def create_new_alias_plugin(old_plugin, new_alias_grouper, is_src_plugin=False):
141156 new_plugin .position = old_plugin_position
142157 new_plugin .save ()
143158
144- logger .info ("Creating cms4 alias plugin: {}-{} for cms3plugin {}" .format (
145- new_plugin .id ,
146- new_plugin .plugin_type ,
147- old_plugin_id ,
148- ))
159+ logger .info (
160+ "Creating cms4 alias plugin: {}-{} for cms3plugin {}" .format (
161+ new_plugin .id ,
162+ new_plugin .plugin_type ,
163+ old_plugin_id ,
164+ )
165+ )
149166
150167
151168def create_reference_alias_plugins (old_source_plugin , new_alias_grouper ):
152169 """
153170 Create cms4 Alias plugin for cms3 alias references
154171 """
155- for old_alias_reference in AliasPluginModel .objects .filter (plugin_id = old_source_plugin ):
156- reference_plugin = CMSPlugin .objects .get (id = old_alias_reference .cmsplugin_ptr_id )
172+ for old_alias_reference in AliasPluginModel .objects .filter (
173+ plugin_id = old_source_plugin
174+ ):
175+ reference_plugin = CMSPlugin .objects .get (
176+ id = old_alias_reference .cmsplugin_ptr_id
177+ )
157178 # Create Alias plugin for the reference plugin at the reference plugin location and delete the reference plugin
158179 create_new_alias_plugin (reference_plugin , new_alias_grouper )
159180
@@ -166,9 +187,7 @@ def get_child_plugins(plugin):
166187 :param plugin: CMSPlugin Object
167188 :return: Queryset for Child plugin objects for this plugin
168189 """
169- child_plugin_queryset = CMSPlugin .objects .filter (
170- parent_id = plugin .id
171- )
190+ child_plugin_queryset = CMSPlugin .objects .filter (parent_id = plugin .id )
172191 return child_plugin_queryset
173192
174193
@@ -207,9 +226,14 @@ def process_old_alias_sources(site, language, site_plugin_queryset):
207226
208227 if is_versioning_enabled ():
209228 from djangocms_versioning .models import Version
229+
210230 # Create version
211- changed_by = User .objects .get (** {User .USERNAME_FIELD : old_plugin .placeholder .source .changed_by })
212- version = Version .objects .create (content = alias_content , created_by = changed_by )
231+ changed_by = User .objects .get (
232+ ** {User .USERNAME_FIELD : old_plugin .placeholder .source .changed_by }
233+ )
234+ version = Version .objects .create (
235+ content = alias_content , created_by = changed_by
236+ )
213237 version .publish (changed_by )
214238
215239 # create csm4 alias plugins for cms3 alias references
@@ -223,7 +247,9 @@ def _process_sites(plugin_id_list):
223247
224248 sites_placeholders = [
225249 placeholders .pk
226- for pagecontent in PageContent ._base_manager .filter (page__node__site_id = site .id )
250+ for pagecontent in PageContent ._base_manager .filter (
251+ page__node__site_id = site .id
252+ )
227253 for placeholders in pagecontent .get_placeholders ()
228254 ]
229255 for language in get_language_list (site .id ):
@@ -233,9 +259,11 @@ def _process_sites(plugin_id_list):
233259 pk__in = plugin_id_list ,
234260 language = language ,
235261 )
236- logger .info ("Processing Language: {} Plugin count: {}" .format (
237- language , site_plugin_queryset .count ()
238- ))
262+ logger .info (
263+ "Processing Language: {} Plugin count: {}" .format (
264+ language , site_plugin_queryset .count ()
265+ )
266+ )
239267
240268 process_old_alias_sources (site , language , site_plugin_queryset )
241269
@@ -248,43 +276,49 @@ class Command(BaseCommand):
248276 Terms: Alias Reference, Alias Source
249277 """
250278
251- help = ' Run after migrations are applied'
279+ help = " Run after migrations are applied"
252280
253281 def handle (self , * args , ** options ):
254282 with transaction .atomic ():
255283 # Alias source plugin list
256- plugin_id_list = list (AliasPluginModel .objects .values_list ('plugin_id' , flat = True ).order_by ('plugin_id' ))
284+ plugin_id_list = list (
285+ AliasPluginModel .objects .values_list ("plugin_id" , flat = True ).order_by (
286+ "plugin_id"
287+ )
288+ )
257289 alias_source_total = len (plugin_id_list )
258290 # Alias references list count
259291 alias_reference_total = AliasPluginModel .objects .count ()
260292 old_stats = {
261- ' old_alias_source_count' : alias_source_total ,
293+ " old_alias_source_count" : alias_source_total ,
262294 # 'old_alias_reference_count': alias_reference_total,
263- ' old_alias_target_count' : alias_reference_total + alias_source_total ,
264- ' src_alias_count' : alias_source_total ,
265- ' reference_alias_count' : alias_reference_total ,
295+ " old_alias_target_count" : alias_reference_total + alias_source_total ,
296+ " src_alias_count" : alias_source_total ,
297+ " reference_alias_count" : alias_reference_total ,
266298 }
267299 new_stats = {
268300 # AliasModel list count should match old_alias_source_count
269- ' new_alias_source_count' : 0 ,
301+ " new_alias_source_count" : 0 ,
270302 # CMS4AliasPluginModel list count should match old_alias_target_count
271- ' new_alias_plugin_target_count' : 0 ,
272- ' src_alias_count' : 0 ,
273- ' reference_alias_count' : 0 ,
274- ' new_src_alias_plugins_count' : 0 ,
275- ' new_ref_alias_plugins_count' : 0 ,
303+ " new_alias_plugin_target_count" : 0 ,
304+ " src_alias_count" : 0 ,
305+ " reference_alias_count" : 0 ,
306+ " new_src_alias_plugins_count" : 0 ,
307+ " new_ref_alias_plugins_count" : 0 ,
276308 }
277309 # Do the work
278310 _process_sites (plugin_id_list )
279311 # Finalise stats
280- new_stats [' new_alias_source_count' ] = AliasContent .objects .count ()
312+ new_stats [" new_alias_source_count" ] = AliasContent .objects .count ()
281313 # CMS4 Alias Plugin count is compared with all source and reference plugins because
282314 # in the CMS 3 implementation the source is a plugin that is then references to.
283315 # In CMS 4 the source is the Alias instance not a plugin on a page.
284- new_stats ['new_alias_plugin_target_count' ] = CMS4AliasPluginModel .objects .count ()
285- new_stats ['src_alias_count' ] = src_alias_count
286- new_stats ['reference_alias_count' ] = reference_alias_count
287- new_stats ['new_ref_alias_plugins_count' ] = new_ref_alias_plugins_count
288- new_stats ['new_src_alias_plugins_count' ] = new_src_alias_plugins_count
289- logger .info ('old stats: {}' .format (old_stats ))
290- logger .info ('new stats: {}' .format (new_stats ))
316+ new_stats ["new_alias_plugin_target_count" ] = (
317+ CMS4AliasPluginModel .objects .count ()
318+ )
319+ new_stats ["src_alias_count" ] = src_alias_count
320+ new_stats ["reference_alias_count" ] = reference_alias_count
321+ new_stats ["new_ref_alias_plugins_count" ] = new_ref_alias_plugins_count
322+ new_stats ["new_src_alias_plugins_count" ] = new_src_alias_plugins_count
323+ logger .info ("old stats: {}" .format (old_stats ))
324+ logger .info ("new stats: {}" .format (new_stats ))
0 commit comments