From 216119868e241a6030edc1cf4a2f831484832522 Mon Sep 17 00:00:00 2001 From: Dmitrij Djachkov Date: Thu, 16 Jul 2020 13:28:15 +0300 Subject: [PATCH] Confluence: update_page_property method + example (#536) --- atlassian/confluence.py | 43 +++++++++++++++++-- .../confluence/confluence-page-properties.py | 43 +++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 examples/confluence/confluence-page-properties.py diff --git a/atlassian/confluence.py b/atlassian/confluence.py index b782094fd..499d2ab34 100644 --- a/atlassian/confluence.py +++ b/atlassian/confluence.py @@ -111,8 +111,8 @@ def get_parent_content_id(self, page_id): parent_content_id = None try: parent_content_id = ( - (self.get_page_by_id(page_id=page_id, expand='ancestors').get('ancestors') or {})[-1].get( - 'id') or None) + (self.get_page_by_id(page_id=page_id, expand='ancestors').get('ancestors') or {})[-1].get( + 'id') or None) except Exception as e: log.error(e) return parent_content_id @@ -1255,6 +1255,44 @@ def set_page_property(self, page_id, data): return response + def update_page_property(self, page_id, data): + """ + Update the page (content) property. + Use json data or independent keys + :param page_id: content_id format + :data: property data in json format + :return: + """ + url = 'rest/api/content/{page_id}/property/{key}'.format(page_id=page_id, key=data.get("key")) + try: + response = self.put(path=url, data=data) + except HTTPError as e: + if e.response.status_code == 400: + raise ApiValueError( + "The given property has a different content id to the one in the " + "path, or the content already has a value with the given key, or " + "the value is missing, or the value is too long", + reason=e) + if e.response.status_code == 403: + raise ApiPermissionError( + "The user does not have permission to " + "edit the content with the given id", + reason=e) + if e.response.status_code == 404: + raise ApiNotFoundError( + "There is no content with the given id, or no property with the given key, " + "or if the calling user does not have permission to view the content.", + reason=e + ) + if e.response.status_code == 409: + raise ApiConflictError( + "The given version is does not match the expected " + "target version of the updated property", reason=e) + if e.response.status_code == 413: + raise ApiValueError("The value is too long", reason=e) + raise + return response + def delete_page_property(self, page_id, page_property): """ Delete the page (content) property e.g. delete key of hash @@ -1287,7 +1325,6 @@ def get_page_property(self, page_id, page_property_key): """ url = 'rest/api/content/{page_id}/property/{key}'.format(page_id=page_id, key=str(page_property_key)) - try: response = self.get(path=url) except HTTPError as e: diff --git a/examples/confluence/confluence-page-properties.py b/examples/confluence/confluence-page-properties.py new file mode 100644 index 000000000..63b018fd0 --- /dev/null +++ b/examples/confluence/confluence-page-properties.py @@ -0,0 +1,43 @@ +# coding=utf-8 +from atlassian import Confluence + +"""This example shows how to export pages""" + +confluence = Confluence( + url='http://localhost:8090', + username='admin', + password='admin') + +# Set page property +data = { + "key": "newprp", + "value": { + "anything": "goes" + } +} +print("SET") +print(confluence.set_page_property(242793586, data)) + +# # Get page property +print("GET") +print(confluence.get_page_property(242793586, "newprp")) + + +# Update page property +data = { + "key": "newprp", + "value": { + "anything": "goes around" + }, + "version": { + "number": 2, + "minorEdit": False, + "hidden": False + } +} +print("UPDATE") +print(confluence.update_page_property(242793586, data)) + +# Delete page property +print("DELETE") +print(confluence.delete_page_property(242793586, "newprp")) \ No newline at end of file