Created
December 12, 2021 11:17
-
-
Save netchampfaris/cada95c4bb4bfb46ee2db31f238ec21b to your computer and use it in GitHub Desktop.
Migrate Help Article to Wiki Page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from frappe.frappeclient import FrappeClient | |
from unittest.mock import patch | |
client = FrappeClient('https://frappe.io', username='username', password='password') | |
articles = client.get_list('Help Article', fields=['name', 'route'], limit_page_length=200) | |
category_slug = { | |
'Accounting': 'accounts', | |
'Selling': 'selling', | |
'Asset': 'asset', | |
'Buying': 'buying', | |
'HR': 'human-resources', | |
'Stock': 'stock', | |
'Projects': 'projects', | |
'Support': 'support', | |
'Manufacturing': 'manufacturing', | |
} | |
def create_revisions(wiki_page, owner, modified, modified_by): | |
revision = frappe.new_doc("Wiki Page Revision") | |
revision.wiki_page = wiki_page.name | |
revision.content = wiki_page.content | |
revision.message = "Create Wiki Page" | |
revision.owner = owner | |
revision.creation = wiki_page.creation | |
revision.insert() | |
revision = frappe.new_doc("Wiki Page Revision") | |
revision.wiki_page = wiki_page.name | |
revision.content = wiki_page.content | |
revision.message = f"Update {wiki_page.title}" | |
revision.owner = modified_by | |
revision.creation = modified | |
revision.insert() | |
for article in articles: | |
print() | |
print(article['name']) | |
article_category_slug = frappe.scrub(article['category']).replace('_', '-').replace('/', '-') | |
slug = category_slug.get(article['category']) or article_category_slug | |
route = article['route'].replace(f'kb/{article_category_slug}', f'docs/v13/user/manual/en/{slug}/articles') | |
name = route[:140] | |
d = frappe.get_doc( | |
doctype='Wiki Page', | |
__newname=name, | |
title=article['title'], | |
route=route, | |
published=article['published'], | |
allow_guest=1, | |
content=article['content'], | |
owner=article['owner'], | |
creation=article['creation'], | |
modified=article['modified'], | |
modified_by=article['modified_by'] | |
) | |
d.flags.ignore_version = True | |
if not frappe.db.exists('Wiki Page', name): | |
with patch.object(d, 'after_insert') as mocked: | |
d.insert() | |
create_revisions(d, article['owner'], article['modified'], article['modified_by']) | |
print(d) | |
frappe.db.commit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment