def migrate_old_posts():
"""
Converts all old posts from a simple page format to one Wagtail accepts
"""
# id=4 is the specific page ID for the news index page
index = Page.objects.get(id=4).specific
old_posts = Communication.objects.using('old_data').all().order_by('date')
user = get_user_model().objects.get(id=1)
for post in old_posts:
if post.title:
title = post.title
else:
title = 'Archived item from {date}'.format(date=date(post.date, 'D jS F Y'))
slug = slugify('{title} - {rand}'.format(title=title, rand=int(round(time.time() * 1000))))
if len(post.text) > 512:
intro = post.text[:512] + '...'
else:
intro = post.text
page = BlogPage(
search_description='',
seo_title=title,
show_in_menus=False,
slug=slug,
title=title,
date=post.date,
first_published_at=post.date,
intro=linebreaks(intro),
)
page.body.stream_data = [
('paragraph', RichText('<p>{body}</p>'.format(body=linebreaks(post.text))))
]
page.tags.add(COMMS_DICT[post.type])
print('Restoring article from {date}'.format(date=post.date))
index.add_child(instance=page)
revision = page.save_revision(
user=user,
submitted_for_moderation=False
)
revision.publish()
page.save()
评论列表
文章目录