Skip to content

Commit

Permalink
fix: delete old placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Sep 10, 2024
1 parent 7e491a3 commit e2a5078
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
21 changes: 0 additions & 21 deletions djangocms_blog/migrations/0044_copy_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def __exit__(self, exc_type, exc_value, traceback):

def move_plugins(source, content, content_type):
if source:
print(f"Creating placeholder {source.slot}")
new_placeholder = source.__class__.objects.create(
slot=source.slot if source.slot != "post_content" else "content",
default_width=source.default_width,
Expand All @@ -31,7 +30,6 @@ def move_plugins(source, content, content_type):
for position, plugin in enumerate(plugins, start=1):
plugin.position = position
plugin.__class__.objects.bulk_update(plugins, ["position"])
print(f"Moved {position} plugins")


def move_plugins_to_blog_content(apps, schema_editor):
Expand Down Expand Up @@ -85,28 +83,16 @@ def move_plugins_to_blog_content(apps, schema_editor):
created_by=migration_user,
state=PUBLISHED if post.publish else DRAFT,
)
print("(published)" if post.publish else "(draft)")

media = post.media
post_content = post.content
liveblog = post.liveblog
move_plugins(media, content, content_type)
move_plugins(post_content, content, content_type)
move_plugins(liveblog, content, content_type)
# First set the PlaceholderField to None
# to avoid on_delete=CASCADE to delete the post object
post.media = None
post.content = None
post.liveblog = None
post.save()
media.delete()
print(post_content.delete())
liveblog.delete()
else:
print(f"Post content {translation.title} ({translation.language_code}) already exists, skipping...")

print("Migration 44 finished.", Post.objects.all().count(), "posts survived.")


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 All @@ -119,12 +105,5 @@ class Migration(migrations.Migration):
]

operations = [
migrations.AddField(
model_name="post",
name="content",
field=models.CharField(
blank=True, default="get_author_schemaorg", max_length=200, verbose_name="Schema.org author name"
),
),
migrations.RunPython(move_plugins_to_blog_content, move_plugins_back_to_blog, elidable=True),
]
10 changes: 10 additions & 0 deletions djangocms_blog/migrations/0045_auto_20230314_0747.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
from django.db import migrations


def remove_old_placeholders(apps, schema_editor):
Placeholder = apps.get_model("cms", "Placeholder")
ContentType = apps.get_model("contenttypes", "ContentType")

content_type = ContentType.objects.filter(app_label="djangocms_blog", model="post").first()
if content_type:
Placeholder.objects.filter(content_type=content_type).delete()


class Migration(migrations.Migration):
dependencies = [
("djangocms_blog", "0044_copy_plugins"),
Expand Down Expand Up @@ -44,4 +53,5 @@ class Migration(migrations.Migration):
migrations.DeleteModel(
name="PostTranslation",
),
migrations.RunPython(remove_old_placeholders, elidable=True),
]

0 comments on commit e2a5078

Please sign in to comment.