Skip to content

Instantly share code, notes, and snippets.

@ephes
Created February 4, 2023 13:36
Show Gist options
  • Save ephes/f4e34136e79617b51e28869a288e1993 to your computer and use it in GitHub Desktop.
Save ephes/f4e34136e79617b51e28869a288e1993 to your computer and use it in GitHub Desktop.
def blog_to_podcast(blog, content_type):
exclude = {"id", "page_ptr_id", "page_ptr", "translation_key"}
kwargs = {
f.name: getattr(blog, f.name)
for f in Blog._meta.fields
if f.name not in exclude
}
kwargs["slug"] = f"new_{blog.slug}"
kwargs["content_type"] = content_type
kwargs["new_itunes_artwork"] = blog.itunes_artwork
kwargs["new_itunes_categories"] = blog.itunes_categories
kwargs["new_keywords"] = blog.keywords
kwargs["new_explicit"] = blog.explicit
return Podcast(**kwargs)
blog = Blog.objects.first()
original_slug = blog.slug
blog_parent = Page.objects.parent_of(blog).first()
# fix hostname and port
site = Site.objects.first()
site.hostname = "localhost"
site.port = 8000
site.save()
# create new page
podcast_content_type = ContentType.objects.get(app_label="cast", model="podcast")
podcast = blog_to_podcast(blog, podcast_content_type)
podcast = blog_parent.add_child(instance=podcast)
podcast = Podcast.objects.first() # super important!
# fix treebeard, dunno why this is needed
from django.core.management import call_command
call_command("fixtree")
podcast = Podcast.objects.first() # super important!
# move children - this is extremely brittle!
from wagtail.actions.move_page import MovePageAction
for child in blog.get_children():
mpa = MovePageAction(child, podcast, pos="last-child")
mpa.execute()
# delete old page
blog.delete()
# restore slug
podcast.slug = original_slug
podcast.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment