Skip to content

Commit c98bb64

Browse files
committed
Fix build and adjust imports
1 parent 5c5ff35 commit c98bb64

File tree

6 files changed

+113
-59
lines changed

6 files changed

+113
-59
lines changed

CONTRIBUTING.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ How to contribute
33

44
You’re very welcome to make bug fixes or enhancements to this library.
55
This document lays out the guidelines for how to get those changes into
6-
the main package repository.
6+
the main package repository. As it's a community project, we welcome all
7+
contributions, whether they are small bug fixes or large new features.
78

89
Getting Started
910
---------------

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ and is compatible with both Atlassian Server and Cloud instances.
1616

1717
Overall, the **atlassian-python-api** is a useful tool for Python developers who want to work with Atlassian products.
1818
It is well-documented and actively maintained, and provides a convenient way to access the full range of
19-
functionality offered by the Atlassian REST APIs.
19+
functionality offered by the Atlassian REST APIs and made with love for Atlassian.
2020

2121

2222
Documentation

atlassian/confluence/__init__.py

Lines changed: 97 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@
2121
ApiNotFoundError,
2222
ApiPermissionError,
2323
ApiValueError,
24+
JsonRPCError,
25+
JsonRPCRestrictionsError,
2426
)
2527
from atlassian.rest_client import AtlassianRestAPI
28+
import sys
29+
30+
if sys.version_info >= (3, 8):
31+
from typing import Literal # Python 3.8+
32+
else:
33+
from typing_extensions import Literal # Python <=3.7
2634

2735
log = logging.getLogger(__name__)
2836

@@ -870,14 +878,9 @@ def get_all_restrictions_from_page_json_rpc(self, page_id):
870878
if self.api_version == "cloud" or self.cloud:
871879
return {}
872880
url = "rpc/json-rpc/confluenceservice-v2"
873-
data = {
874-
"jsonrpc": "2.0",
875-
"method": "getContentPermissionSets",
876-
"id": 9,
877-
"params": [page_id]
878-
}
881+
data = {"jsonrpc": "2.0", "method": "getContentPermissionSets", "id": 9, "params": [page_id]}
879882
return self.post(url, data=data).get("result") or {}
880-
883+
881884
def update_restrictions_for_page_json_rpc(self, page_id, permission_type, content_permissions):
882885
"""
883886
The JSON-RPC APIs for Confluence are provided here to help you browse and discover APIs you have access to.
@@ -892,90 +895,130 @@ def update_restrictions_for_page_json_rpc(self, page_id, permission_type, conten
892895
"jsonrpc": "2.0",
893896
"method": "setContentPermissions",
894897
"id": 9,
895-
"params": [page_id, permission_type, content_permissions]
898+
"params": [page_id, permission_type, content_permissions],
896899
}
897900
return self.post(url, data=data).get("result") or {}
898-
899-
def get_users_from_restricts_in_page_by_type(self, page_id: str, restriction_type: Literal['View', 'Edit']):
900-
page_name = self.get_page_by_id(page_id=page_id)['title']
901+
902+
def get_users_from_restricts_in_page_by_type(self, page_id: str, restriction_type: Literal["View", "Edit"]):
903+
page_name = self.get_page_by_id(page_id=page_id)["title"]
901904
restrictions_in_page = self.get_all_restrictions_from_page_json_rpc(page_id=page_id)
902905
try:
903906
if len(restrictions_in_page) > 0:
904907
for restriction_type_in_page in restrictions_in_page:
905-
if dict(restriction_type_in_page).get('type') == restriction_type:
906-
users = dict(restriction_type_in_page).get('contentPermissions')
908+
if dict(restriction_type_in_page).get("type") == restriction_type:
909+
users = dict(restriction_type_in_page).get("contentPermissions")
907910
return users
908911
else:
909-
raise JsonRPCRestrictionsError(f'On page "{page_name}" has no restrictions type of "{restriction_type}"')
912+
raise JsonRPCRestrictionsError(
913+
f'On page "{page_name}" has no restrictions type of "{restriction_type}"'
914+
)
910915
except JsonRPCError:
911916
raise
912917

913-
def create_restricts_from_from_user(self, user_name: str, restriction_type: Literal['View', 'Edit']):
914-
content = {'type': restriction_type, 'userName': user_name, 'groupName': None}
915-
918+
def create_restricts_from_from_user(self, user_name: str, restriction_type: Literal["View", "Edit"]):
919+
content = {"type": restriction_type, "userName": user_name, "groupName": None}
920+
916921
return content
917922

918-
def add_user_in_restricted_page(self, user_name: str, page_id: str, restriction_type: Literal['View', 'Edit']):
919-
page_name = self.get_page_by_id(page_id=page_id).get('title')
923+
def add_user_in_restricted_page(self, user_name: str, page_id: str, restriction_type: Literal["View", "Edit"]):
924+
page_name = self.get_page_by_id(page_id=page_id).get("title")
920925
user_find_view_bool = False
921926
user_find_edit_bool = False
922-
users_content_view: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='View')
923-
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='Edit')
924-
current_user_content_view: dict = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
925-
current_user_content_edit: dict = self.create_restricts_from_from_user(user_name=user_name, restriction_type='Edit')
927+
users_content_view: list = self.get_users_from_restricts_in_page_by_type(
928+
page_id=page_id, restriction_type="View"
929+
)
930+
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(
931+
page_id=page_id, restriction_type="Edit"
932+
)
933+
current_user_content_view: dict = self.create_restricts_from_from_user(
934+
user_name=user_name, restriction_type="View"
935+
)
936+
current_user_content_edit: dict = self.create_restricts_from_from_user(
937+
user_name=user_name, restriction_type="Edit"
938+
)
926939
try:
927940
if None not in [users_content_view, users_content_edit]:
928941
if users_content_view is not None:
929942
for user in users_content_view:
930-
if dict(user).get('userName') == current_user_content_view.get('userName'):
943+
if dict(user).get("userName") == current_user_content_view.get("userName"):
931944
user_find_view_bool = True
932945
if users_content_edit is not None:
933946
for user in users_content_edit:
934-
if dict(user).get('userName') == current_user_content_edit.get('userName'):
947+
if dict(user).get("userName") == current_user_content_edit.get("userName"):
935948
user_find_edit_bool = True
936-
if restriction_type == 'View':
937-
if user_find_view_bool == False:
938-
current_user_content = self.create_restricts_from_from_user(user_name=user_name, restriction_type=restriction_type)
949+
if restriction_type == "View":
950+
if user_find_view_bool is False:
951+
current_user_content = self.create_restricts_from_from_user(
952+
user_name=user_name, restriction_type=restriction_type
953+
)
939954
users_content_view.append(current_user_content)
940-
self.update_restrictions_for_page_json_rpc(page_id=page_id, user=user_name, permission_type=restriction_type, content_permissions=users_content_view)
941-
elif user_find_view_bool == True:
942-
raise JsonRPCRestrictionsError(f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"')
943-
elif restriction_type == 'Edit':
944-
if user_find_edit_bool == False:
945-
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
946-
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type=restriction_type)
955+
self.update_restrictions_for_page_json_rpc(
956+
page_id=page_id,
957+
permission_type=restriction_type,
958+
content_permissions=users_content_view,
959+
)
960+
elif user_find_view_bool:
961+
raise JsonRPCRestrictionsError(
962+
f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"'
963+
)
964+
elif restriction_type == "Edit":
965+
if not user_find_edit_bool:
966+
current_user_content_view = self.create_restricts_from_from_user(
967+
user_name=user_name, restriction_type="View"
968+
)
969+
current_user_content_edit = self.create_restricts_from_from_user(
970+
user_name=user_name, restriction_type=restriction_type
971+
)
947972
users_content_view.append(current_user_content_view)
948973
users_content_edit.append(current_user_content_edit)
949-
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='View', content_permissions=users_content_view)
950-
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type=restriction_type, content_permissions=users_content_edit)
951-
print(f'User "{user_name}" granted restrictions type of "{restriction_type}" on page "{page_name}"')
952-
elif user_find_edit_bool == True:
953-
raise JsonRPCRestrictionsError(f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"')
974+
self.update_restrictions_for_page_json_rpc(
975+
page_id=page_id, permission_type="View", content_permissions=users_content_view
976+
)
977+
self.update_restrictions_for_page_json_rpc(
978+
page_id=page_id, permission_type=restriction_type, content_permissions=users_content_edit
979+
)
980+
print(
981+
f'User "{user_name}" granted restrictions type of "{restriction_type}" on page "{page_name}"'
982+
)
983+
elif user_find_edit_bool:
984+
raise JsonRPCRestrictionsError(
985+
f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"'
986+
)
954987
except JsonRPCError:
955988
raise
956989

957990
def remove_user_from_restricted_page(self, user_name: str, page_id: str):
958-
page_name = self.get_page_by_id(page_id=page_id).get('title')
991+
page_name = self.get_page_by_id(page_id=page_id).get("title")
959992
user_find_bool = False
960-
users_content_view: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='View')
961-
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='Edit')
962-
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
963-
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type='Edit')
993+
users_content_view: list = self.get_users_from_restricts_in_page_by_type(
994+
page_id=page_id, restriction_type="View"
995+
)
996+
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(
997+
page_id=page_id, restriction_type="Edit"
998+
)
999+
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type="View")
1000+
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type="Edit")
9641001
for user_index, user_value in enumerate(users_content_view):
965-
if dict(user_value).get('userName') == current_user_content_view.get('userName'):
1002+
if dict(user_value).get("userName") == current_user_content_view.get("userName"):
9661003
user_find_bool = True
9671004
users_content_view.pop(user_index)
9681005
for user_index, user_value in enumerate(users_content_edit):
969-
if dict(user_value).get('userName') == current_user_content_edit.get('userName'):
1006+
if dict(user_value).get("userName") == current_user_content_edit.get("userName"):
9701007
user_find_bool = True
9711008
users_content_edit.pop(user_index)
9721009
try:
973-
if user_find_bool == True:
974-
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='View', content_permissions=users_content_view)
975-
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='Edit', content_permissions=users_content_edit)
1010+
if user_find_bool:
1011+
self.update_restrictions_for_page_json_rpc(
1012+
page_id=page_id, permission_type="View", content_permissions=users_content_view
1013+
)
1014+
self.update_restrictions_for_page_json_rpc(
1015+
page_id=page_id, permission_type="Edit", content_permissions=users_content_edit
1016+
)
9761017
print(f'User "{user_name}" has been deleted from restrictions on page "{page_name}"')
977-
elif user_find_bool == False:
978-
raise JsonRPCRestrictionsError(f'User "{user_name}" has not founded in restrictions on page "{page_name}"')
1018+
elif not user_find_bool:
1019+
raise JsonRPCRestrictionsError(
1020+
f'User "{user_name}" has not founded in restrictions on page "{page_name}"'
1021+
)
9791022
except JsonRPCError:
9801023
raise
9811024

@@ -1691,10 +1734,10 @@ def download_attachments_from_page(self, page_id, path=None, start=0, limit=50,
16911734
else:
16921735
# Sanitize filename if needed
16931736
if re.search(r'[<>:"/\\|?*\x00-\x1F]', file_name):
1694-
sanitized = re.sub(r'[<>:"/\\|?*\x00-\x1F]', '_', file_name)
1737+
sanitized = re.sub(r'[<>:"/\\|?*\x00-\x1F]', "_", file_name)
16951738
warnings.warn(
16961739
f"File name '{file_name}' contained invalid characters and was renamed to '{sanitized}'.",
1697-
UserWarning
1740+
UserWarning,
16981741
)
16991742
file_name = sanitized
17001743
file_path = os.path.join(path, file_name)

atlassian/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ class ApiConflictError(ApiError):
3131
class ApiNotAcceptable(ApiError):
3232
pass
3333

34+
3435
class JsonRPCRestrictionsError(JsonRPCError):
3536
pass

atlassian/jira.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88
from deprecated import deprecated
99
from requests import HTTPError, Response
10-
from typing_extensions import Literal
10+
import sys
1111

12+
if sys.version_info >= (3, 8):
13+
from typing import Literal # Python 3.8+
14+
else:
15+
from typing_extensions import Literal # Python <=3.7
1216
from .errors import ApiNotFoundError, ApiPermissionError
1317
from .rest_client import AtlassianRestAPI
1418
from .typehints import T_id, T_resp_json

atlassian/rest_client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding=utf-8
2+
23
import logging
34
import random
45
import time
@@ -13,11 +14,15 @@
1314
overload,
1415
)
1516
from urllib.parse import urlencode
16-
17+
import sys
1718
import requests
1819
import urllib3
1920
from requests.adapters import HTTPAdapter
20-
from typing_extensions import Literal
21+
22+
if sys.version_info >= (3, 8):
23+
from typing import Literal # Python 3.8+
24+
else:
25+
from typing_extensions import Literal # Python <=3.7
2126

2227
from atlassian.typehints import T_resp_json
2328

0 commit comments

Comments
 (0)