21
21
ApiNotFoundError ,
22
22
ApiPermissionError ,
23
23
ApiValueError ,
24
+ JsonRPCError ,
25
+ JsonRPCRestrictionsError ,
24
26
)
25
27
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
26
34
27
35
log = logging .getLogger (__name__ )
28
36
@@ -870,14 +878,9 @@ def get_all_restrictions_from_page_json_rpc(self, page_id):
870
878
if self .api_version == "cloud" or self .cloud :
871
879
return {}
872
880
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 ]}
879
882
return self .post (url , data = data ).get ("result" ) or {}
880
-
883
+
881
884
def update_restrictions_for_page_json_rpc (self , page_id , permission_type , content_permissions ):
882
885
"""
883
886
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
892
895
"jsonrpc" : "2.0" ,
893
896
"method" : "setContentPermissions" ,
894
897
"id" : 9 ,
895
- "params" : [page_id , permission_type , content_permissions ]
898
+ "params" : [page_id , permission_type , content_permissions ],
896
899
}
897
900
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" ]
901
904
restrictions_in_page = self .get_all_restrictions_from_page_json_rpc (page_id = page_id )
902
905
try :
903
906
if len (restrictions_in_page ) > 0 :
904
907
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" )
907
910
return users
908
911
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
+ )
910
915
except JsonRPCError :
911
916
raise
912
917
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
+
916
921
return content
917
922
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" )
920
925
user_find_view_bool = False
921
926
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
+ )
926
939
try :
927
940
if None not in [users_content_view , users_content_edit ]:
928
941
if users_content_view is not None :
929
942
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" ):
931
944
user_find_view_bool = True
932
945
if users_content_edit is not None :
933
946
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" ):
935
948
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
+ )
939
954
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
+ )
947
972
users_content_view .append (current_user_content_view )
948
973
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
+ )
954
987
except JsonRPCError :
955
988
raise
956
989
957
990
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" )
959
992
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" )
964
1001
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" ):
966
1003
user_find_bool = True
967
1004
users_content_view .pop (user_index )
968
1005
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" ):
970
1007
user_find_bool = True
971
1008
users_content_edit .pop (user_index )
972
1009
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
+ )
976
1017
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
+ )
979
1022
except JsonRPCError :
980
1023
raise
981
1024
@@ -1691,10 +1734,10 @@ def download_attachments_from_page(self, page_id, path=None, start=0, limit=50,
1691
1734
else :
1692
1735
# Sanitize filename if needed
1693
1736
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 )
1695
1738
warnings .warn (
1696
1739
f"File name '{ file_name } ' contained invalid characters and was renamed to '{ sanitized } '." ,
1697
- UserWarning
1740
+ UserWarning ,
1698
1741
)
1699
1742
file_name = sanitized
1700
1743
file_path = os .path .join (path , file_name )
0 commit comments