Skip to content

Commit

Permalink
0044: Move placeholders to post content by manually adjusting content…
Browse files Browse the repository at this point in the history
… type and object id
  • Loading branch information
fsbraun committed May 7, 2024
1 parent 8468fc7 commit 2471e1e
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions djangocms_blog/migrations/0044_copy_plugins.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from cms.utils.placeholder import get_placeholder_from_slot
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.db import migrations

User = get_user_model()
Expand All @@ -17,6 +18,7 @@ def move_plugins_to_blog_content(apps, schema_editor):

Post = apps.get_model("djangocms_blog", "Post")
PostContent = apps.get_model("djangocms_blog", "PostContent")
content_type = ContentType.objects.get(app_label="djangocms_blog", model="postcontent")

for post in Post.objects.all():
if not post.postcontent_set.exists():
Expand Down Expand Up @@ -47,23 +49,19 @@ def move_plugins_to_blog_content(apps, schema_editor):
)
translation.delete()

# Resolve placeholders
content_media = get_placeholder_from_slot(content.placeholders, "media")
content_content = get_placeholder_from_slot(content.placeholders, "content")
content_liveblog = get_placeholder_from_slot(content.placeholders, "liveblog")

# Move plugins to post content placeholders
if post.media:
media_plugins = post.media.cmsplugin_set.filter(language=translation.language_code)
media_plugins.update(placeholder=content_media)
post.media.content_type = content_type
post.media.object_id = content.pk
post.media.save()
if post.content:
print("Moving content plugins...")
content_plugins = post.content.cmsplugin_set.filter(language=translation.language_code)
content_plugins.update(placeholder=content_content)
post.content.content_type = content_type
post.content.object_id = content.pk
post.content.save()
if post.liveblog:
live_plugins = post.liveblog.cmsplugin_set.filter(language=translation.language_code)
live_plugins.update(placeholder=content_liveblog)

post.liveblog.content_type = content_type
post.liveblog.object_id = content.pk
post.liveblog.save()


def move_plugins_back_to_blog(apps, schema_editor):
"""Adds instances for the new model.ATTENTION: All fields of the model must have a valid default value!"""
Expand Down

0 comments on commit 2471e1e

Please sign in to comment.