Skip to content

Commit

Permalink
Confluence: update_page_property method + example (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrij Djachkov authored Jul 16, 2020
1 parent 12793dd commit 2161198
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
43 changes: 40 additions & 3 deletions atlassian/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions examples/confluence/confluence-page-properties.py
Original file line number Diff line number Diff line change
@@ -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"))

0 comments on commit 2161198

Please sign in to comment.