Skip to content

Commit 2161198

Browse files
author
Dmitrij Djachkov
authored
Confluence: update_page_property method + example (#536)
1 parent 12793dd commit 2161198

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

atlassian/confluence.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ def get_parent_content_id(self, page_id):
111111
parent_content_id = None
112112
try:
113113
parent_content_id = (
114-
(self.get_page_by_id(page_id=page_id, expand='ancestors').get('ancestors') or {})[-1].get(
115-
'id') or None)
114+
(self.get_page_by_id(page_id=page_id, expand='ancestors').get('ancestors') or {})[-1].get(
115+
'id') or None)
116116
except Exception as e:
117117
log.error(e)
118118
return parent_content_id
@@ -1255,6 +1255,44 @@ def set_page_property(self, page_id, data):
12551255

12561256
return response
12571257

1258+
def update_page_property(self, page_id, data):
1259+
"""
1260+
Update the page (content) property.
1261+
Use json data or independent keys
1262+
:param page_id: content_id format
1263+
:data: property data in json format
1264+
:return:
1265+
"""
1266+
url = 'rest/api/content/{page_id}/property/{key}'.format(page_id=page_id, key=data.get("key"))
1267+
try:
1268+
response = self.put(path=url, data=data)
1269+
except HTTPError as e:
1270+
if e.response.status_code == 400:
1271+
raise ApiValueError(
1272+
"The given property has a different content id to the one in the "
1273+
"path, or the content already has a value with the given key, or "
1274+
"the value is missing, or the value is too long",
1275+
reason=e)
1276+
if e.response.status_code == 403:
1277+
raise ApiPermissionError(
1278+
"The user does not have permission to "
1279+
"edit the content with the given id",
1280+
reason=e)
1281+
if e.response.status_code == 404:
1282+
raise ApiNotFoundError(
1283+
"There is no content with the given id, or no property with the given key, "
1284+
"or if the calling user does not have permission to view the content.",
1285+
reason=e
1286+
)
1287+
if e.response.status_code == 409:
1288+
raise ApiConflictError(
1289+
"The given version is does not match the expected "
1290+
"target version of the updated property", reason=e)
1291+
if e.response.status_code == 413:
1292+
raise ApiValueError("The value is too long", reason=e)
1293+
raise
1294+
return response
1295+
12581296
def delete_page_property(self, page_id, page_property):
12591297
"""
12601298
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):
12871325
"""
12881326
url = 'rest/api/content/{page_id}/property/{key}'.format(page_id=page_id,
12891327
key=str(page_property_key))
1290-
12911328
try:
12921329
response = self.get(path=url)
12931330
except HTTPError as e:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# coding=utf-8
2+
from atlassian import Confluence
3+
4+
"""This example shows how to export pages"""
5+
6+
confluence = Confluence(
7+
url='http://localhost:8090',
8+
username='admin',
9+
password='admin')
10+
11+
# Set page property
12+
data = {
13+
"key": "newprp",
14+
"value": {
15+
"anything": "goes"
16+
}
17+
}
18+
print("SET")
19+
print(confluence.set_page_property(242793586, data))
20+
21+
# # Get page property
22+
print("GET")
23+
print(confluence.get_page_property(242793586, "newprp"))
24+
25+
26+
# Update page property
27+
data = {
28+
"key": "newprp",
29+
"value": {
30+
"anything": "goes around"
31+
},
32+
"version": {
33+
"number": 2,
34+
"minorEdit": False,
35+
"hidden": False
36+
}
37+
}
38+
print("UPDATE")
39+
print(confluence.update_page_property(242793586, data))
40+
41+
# Delete page property
42+
print("DELETE")
43+
print(confluence.delete_page_property(242793586, "newprp"))

0 commit comments

Comments
 (0)