Skip to content

Commit c9ef927

Browse files
authored
[Confluence] Update template methods as they are no longer experimental (atlassian-api#731)
* Confluence: Update template methods to non-experimental. * Confluence: Add remove_template method. * Confluence: Add create_template method. * Confluence: Add create_or_update_template method. * Fix Black formatting. * Confluence: Document template methods. * Confluence: Refactor template method names.
1 parent c6842c9 commit c9ef927

File tree

2 files changed

+142
-21
lines changed

2 files changed

+142
-21
lines changed

atlassian/confluence.py

+98-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import time
5+
import json
56

67
from requests import HTTPError
78
from deprecated import deprecated
@@ -666,12 +667,69 @@ def move_page(self, space_key, page_id, target_id=None, target_title=None, posit
666667
params["position"] = position
667668
return self.post(url, params=params, headers=self.no_check_headers)
668669

669-
def get_template_by_id(self, template_id):
670+
def create_or_update_template(
671+
self, name, body, template_type="page", template_id=None, description=None, labels=None, space=None
672+
):
670673
"""
671-
Get user template by id. Experimental API
672-
Use case is get template body and create page from that
674+
Creates a new or updates an existing content template.
675+
676+
Note, blueprint templates cannot be created or updated via the REST API.
677+
678+
If you provide a ``template_id`` then this method will update the template with the provided settings.
679+
If no ``template_id`` is provided, then this method assumes you are creating a new template.
680+
681+
:param str name: If creating, the name of the new template. If updating, the name to change
682+
the template name to. Set to the current name if this field is not being updated.
683+
:param dict body: This object is used when creating or updating content.
684+
{
685+
"storage": {
686+
"value": "<string>",
687+
"representation": "view"
688+
}
689+
}
690+
:param str template_type: OPTIONAL: The type of the new template. Default: "page".
691+
:param str template_id: OPTIONAL: The ID of the template being updated. REQUIRED if updating a template.
692+
:param str description: OPTIONAL: A description of the new template. Max length 255.
693+
:param list labels: OPTIONAL: Labels for the new template. An array like:
694+
[
695+
{
696+
"prefix": "<string>",
697+
"name": "<string>",
698+
"id": "<string>",
699+
"label": "<string>",
700+
}
701+
]
702+
:param dict space: OPTIONAL: The key for the space of the new template. Only applies to space templates.
703+
If not specified, the template will be created as a global template.
704+
:return:
705+
"""
706+
data = {"name": name, "templateType": template_type, "body": body}
707+
708+
if description:
709+
data["description"] = description
710+
711+
if labels:
712+
data["labels"] = labels
713+
714+
if space:
715+
data["space"] = {"key": space}
716+
717+
if template_id:
718+
data["templateId"] = template_id
719+
return self.put("wiki/rest/api/template", data=json.dumps(data))
720+
721+
return self.post("wiki/rest/api/template", json=data)
722+
723+
def get_content_template(self, template_id):
724+
"""
725+
Get a content template.
726+
727+
This includes information about the template, like the name, the space or blueprint
728+
that the template is in, the body of the template, and more.
729+
:param str template_id: The ID of the content template to be returned
730+
:return:
673731
"""
674-
url = "rest/experimental/template/{template_id}".format(template_id=template_id)
732+
url = "wiki/rest/api/template/{template_id}".format(template_id=template_id)
675733

676734
try:
677735
response = self.get(url)
@@ -688,17 +746,19 @@ def get_template_by_id(self, template_id):
688746

689747
return response
690748

691-
def get_all_blueprints_from_space(self, space, start=0, limit=20, expand=None):
749+
def get_blueprint_templates(self, space=None, start=0, limit=None, expand=None):
692750
"""
693-
Get all users blue prints from space. Experimental API
694-
:param space: Space Key
695-
:param start: OPTIONAL: The start point of the collection to return. Default: None (0).
696-
:param limit: OPTIONAL: The limit of the number of pages to return, this may be restricted by
697-
fixed system limits. Default: 20
698-
:param expand: OPTIONAL: expand e.g. body
751+
Gets all templates provided by blueprints.
699752
753+
Use this method to retrieve all global blueprint templates or all blueprint templates in a space.
754+
:param space: OPTIONAL: The key of the space to be queried for templates. If ``space`` is not
755+
specified, global blueprint templates will be returned.
756+
:param start: OPTIONAL: The starting index of the returned templates. Default: None (0).
757+
:param limit: OPTIONAL: The limit of the number of pages to return, this may be restricted by
758+
fixed system limits. Default: 25
759+
:param expand: OPTIONAL: A multi-value parameter indicating which properties of the template to expand.
700760
"""
701-
url = "rest/experimental/template/blueprint"
761+
url = "wiki/rest/api/template/blueprint"
702762
params = {}
703763
if space:
704764
params["spaceKey"] = space
@@ -722,19 +782,19 @@ def get_all_blueprints_from_space(self, space, start=0, limit=20, expand=None):
722782

723783
return response.get("results") or []
724784

725-
def get_all_templates_from_space(self, space, start=0, limit=20, expand=None):
785+
def get_content_templates(self, space=None, start=0, limit=None, expand=None):
726786
"""
727-
Get all users templates from space. Experimental API
728-
ref: https://docs.atlassian.com/atlassian-confluence/1000.73.0/com/atlassian/confluence/plugins/restapi\
729-
/resources/TemplateResource.html
730-
:param space: Space Key
787+
Get all content templates.
788+
Use this method to retrieve all global content templates or all content templates in a space.
789+
:param space: OPTIONAL: The key of the space to be queried for templates. If ``space`` is not
790+
specified, global templates will be returned.
731791
:param start: OPTIONAL: The start point of the collection to return. Default: None (0).
732792
:param limit: OPTIONAL: The limit of the number of pages to return, this may be restricted by
733-
fixed system limits. Default: 20
734-
:param expand: OPTIONAL: expand e.g. body
735-
793+
fixed system limits. Default: 25
794+
:param expand: OPTIONAL: A multi-value parameter indicating which properties of the template to expand.
795+
e.g. ``body``
736796
"""
737-
url = "rest/experimental/template/page"
797+
url = "wiki/rest/api/template/page"
738798
params = {}
739799
if space:
740800
params["spaceKey"] = space
@@ -758,6 +818,23 @@ def get_all_templates_from_space(self, space, start=0, limit=20, expand=None):
758818

759819
return response.get("results") or []
760820

821+
def remove_template(self, template_id):
822+
"""
823+
Deletes a template.
824+
825+
This results in different actions depending on the type of template:
826+
* If the template is a content template, it is deleted.
827+
* If the template is a modified space-level blueprint template, it reverts to the template
828+
inherited from the global-level blueprint template.
829+
* If the template is a modified global-level blueprint template, it reverts to the default
830+
global-level blueprint template.
831+
Note: Unmodified blueprint templates cannot be deleted.
832+
833+
:param str template_id: The ID of the template to be deleted.
834+
:return:
835+
"""
836+
return self.delete("wiki/rest/api/template/{}".format(template_id))
837+
761838
def get_all_spaces(self, start=0, limit=500, expand=None, space_type=None, space_status=None):
762839
"""
763840
Get all spaces with provided limit

docs/confluence.rst

+44
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,50 @@ Page actions
151151
# Add comment into page
152152
confluence.add_comment(page_id, text)
153153
154+
Template actions
155+
----------------
156+
157+
.. code-block:: python
158+
159+
# Updating a content template
160+
template_id = "<string>"
161+
name = "<string>"
162+
body = {"value": "<string>", "representation": "view"}
163+
template_type = "page"
164+
description = "<string>"
165+
labels = [{"prefix": "<string>", "name": "<string>", "id": "<string>", "label": "<string>"}]
166+
space = "<key_string>"
167+
168+
confluence.create_or_update_template(name, body, template_type, template_id, description, labels, space)
169+
170+
# Creating a new content template
171+
name = "<string>"
172+
body = {"value": "<string>", "representation": "view"}
173+
template_type = "page"
174+
description = "<string>"
175+
labels = [{"prefix": "<string>", "name": "<string>", "id": "<string>", "label": "<string>"}]
176+
space = "<key_string>"
177+
178+
confluence.create_or_update_template(name, body, template_type, description=description, labels=labels, space=space)
179+
180+
# Get a template by its ID
181+
confluence.get_template_by_id(template_id)
182+
183+
# Get all global content templates
184+
confluence.get_all_templates_from_space()
185+
186+
# Get content templates in a space
187+
confluence.get_all_templates_from_space(space)
188+
189+
# Get all global blueprint templates
190+
confluence.get_all_blueprints_from_space()
191+
192+
# Get all blueprint templates in a space
193+
confluence.get_all_blueprints_from_space(space)
194+
195+
# Removing a template
196+
confluence.remove_template(template_id)
197+
154198
Get spaces info
155199
---------------
156200

0 commit comments

Comments
 (0)