atlassian-python-api
atlassian-python-api copied to clipboard
Create a page from a template
Is it possible to get a page from a template and after modifying it, create a new page?
I use this library for this purpose.
My solution was to grab the XML representation of a Confluence page (which is full of Jira Macros) and simply change the JQL feeding the Macros using String.replace for each page I need to create.
@kierun Could you provide your use case, please ?
@gonchik Sure. I have a few hundred documents that need importing into Confluence. I have a script that reads the meta data for those, creates a confluence page (via REST API), and upload the document to said page. Obviously, I need to add the meta (author, why it was created, a note saying it was imported, yadda, yadda, …) to the page properties. This can easily be done via a template manually. However, I could not find a way to do it via the REST API apart from creating a blank page, getting the HTML from that (note it is space aware!!!), and using that as a page body.
+1 for this feature. It'd be a game-changer.
I want to create a robust template page in Confluence to handle all document structure and styling, then just pass information into that template when creating a new page through python.
Thanks!
I'm testing this right now and finding that when I get and update a page with representation='storage' the macros don't render - it ends up being static HTML.
source = confluence.get_page_by_id(source_page_id, expand='body.view')
newbody = source['body']['view']['value']
dest =confluence.update_or_create(parent_page_id, 'Clone test', newbody, representation='storage')
I do see multiple representation options documented by Atlassian over here and it says that storage is "our XML storage format".
I also see the supported representations here: https://github.com/atlassian-api/atlassian-python-api/blob/master/atlassian/confluence.py#L35
After some intense brow-furrowing I found an example that instead used expand='body.storage' and realized I was requesting the "VIEW (HTML representation for viewing in a web page)" on the initial call.
Here's the working clone/copy command:
source = confluence.get_page_by_id(source_page_id, expand='body.storage')
newbody = source['body']['storage']['value']
dest = confluence.update_or_create(parent_page_id, 'Clone test', newbody)
As for the templating part, I'm going to throw some identifiable placeholders into the template and do some search-replacing before the update_or_create().
body = confluence.get_content_template(TEMPLATE_ID)["body"]["storage"]["value"]
page = confluence.create_page(WIKI_SPACE, "Some title", body)
I've got nothing for replacing placeholders (yet), but this is the basic create-a-page-from-a-template move.