|
2 | 2 |
|
3 | 3 |
|
4 | 4 | def migrate_slots(apps, schema_editor, forward=True): |
| 5 | + from djangocms_alias.models import AliasContent as AliasContentModelClass |
| 6 | + |
5 | 7 | AliasContent = apps.get_model("djangocms_alias", "AliasContent") |
| 8 | + ContentType = apps.get_model("contenttypes", "ContentType") |
| 9 | + Placeholder = apps.get_model("cms", "Placeholder") |
| 10 | + |
| 11 | + default_slot_name = AliasContentModelClass.placeholder_slotname |
6 | 12 |
|
7 | 13 | db_alias = schema_editor.connection.alias |
8 | | - qs = AliasContent.objects.using(db_alias).prefetch_related("alias").exclude(alias__static_code="") |
| 14 | + content_type = ContentType.objects.filter(app_label="djangocms_alias", model="aliascontent").first() |
| 15 | + if content_type is None: |
| 16 | + return |
| 17 | + placeholder_qs = Placeholder.objects.using(db_alias).filter(content_type=content_type) |
| 18 | + qs = AliasContent._default_manager.using(db_alias).prefetch_related("alias").exclude(alias__static_code="") |
9 | 19 |
|
10 | 20 | with transaction.atomic(using=db_alias): |
11 | 21 | for alias_content in qs: |
12 | | - slots = list(alias_content.placeholders.all()) |
| 22 | + slots = list(placeholder_qs.filter(object_id=alias_content.pk)) |
13 | 23 | if len(slots) == 1: |
14 | 24 | placeholder = slots[0] |
15 | 25 | # Ensure the placeholder exists with the correct slot name |
16 | | - if forward and placeholder.slot == alias_content.placeholder_slotname: |
| 26 | + if forward and placeholder.slot == default_slot_name: |
17 | 27 | # If migrating forward, we use the static code or the placeholder slot name |
18 | | - placeholder.slot = alias_content.alias.static_code or alias_content.placeholder_slotname |
| 28 | + placeholder.slot = alias_content.alias.static_code or default_slot_name |
19 | 29 | placeholder.save() |
20 | | - elif placeholder.slot != alias_content.placeholder_slotname: |
| 30 | + elif placeholder.slot != default_slot_name: |
21 | 31 | # If migrating backward, we revert to the original placeholder slot name |
22 | | - placeholder.slot = alias_content.placeholder_slotname |
| 32 | + placeholder.slot = default_slot_name |
23 | 33 | placeholder.save() |
24 | 34 | elif len(slots) > 1: |
25 | 35 | print( |
|
0 commit comments