-
-
Notifications
You must be signed in to change notification settings - Fork 318
/
people_manager.py
350 lines (299 loc) · 12.7 KB
/
people_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from office365.runtime.client_result import ClientResult
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.runtime.types.collections import StringCollection
from office365.sharepoint.entity import Entity
from office365.sharepoint.entity_collection import EntityCollection
from office365.sharepoint.principal.users.user import User
from office365.sharepoint.userprofiles.hash_tag import HashTagCollection
from office365.sharepoint.userprofiles.person_properties import PersonProperties
from office365.sharepoint.userprofiles.personal_site_creation_priority import (
PersonalSiteCreationPriority,
)
def _ensure_user(user_or_name, action):
"""
:param str or User user_or_name: User or Login name of the specified user.
:param (str) -> None action: Callback
"""
if isinstance(user_or_name, User):
def _user_loaded():
action(user_or_name.login_name)
user_or_name.ensure_property("LoginName", _user_loaded)
else:
action(user_or_name)
class PeopleManager(Entity):
"""Provides methods for operations related to people."""
def __init__(self, context):
super(PeopleManager, self).__init__(
context, ResourcePath("SP.UserProfiles.PeopleManager")
)
@staticmethod
def get_trending_tags(context):
"""Gets a collection of the 20 (or fewer) most popular hash tags over the past week.
The returned collection is sorted in descending order of frequency of use.
:type context: office365.sharepoint.client_context.ClientContext
"""
return_type = HashTagCollection(context)
manager = PeopleManager(context)
qry = ServiceOperationQuery(
manager, "GetTrendingTags", None, None, None, return_type, True
)
context.add_query(qry)
return return_type
def get_user_onedrive_quota_max(self, account_name):
"""
:param str account_name: Account name of the specified user.
"""
return_type = ClientResult(self.context, int())
params = {"accountName": account_name}
qry = ServiceOperationQuery(
self, "GetUserOneDriveQuotaMax", params, None, None, return_type
)
self.context.add_query(qry)
return return_type
def am_i_following(self, account_name):
"""
Checks whether the current user is following the specified user.
:param str account_name: Account name of the specified user.
:return:
"""
result = ClientResult(self.context)
params = {"accountName": account_name}
qry = ServiceOperationQuery(self, "AmIFollowing", params, None, None, result)
self.context.add_query(qry)
return result
def get_followers_for(self, account):
# type: (str|User) -> EntityCollection[PersonProperties]
"""
Gets the people who are following the specified user.
:param str|User account: Account name of the specified user.
"""
return_type = EntityCollection(self.context, PersonProperties)
def _get_followers_for(account_name):
params = {"accountName": account_name}
qry = ServiceOperationQuery(
self, "GetFollowersFor", params, None, None, return_type
)
self.context.add_query(qry)
if isinstance(account, User):
def _account_loaded():
_get_followers_for(account.login_name)
account.ensure_property("LoginName", _account_loaded)
else:
_get_followers_for(account)
return return_type
def get_user_information(self, account_name, site_id):
"""
:param str account_name: Account name of the specified user.
:param str site_id: Site Identifier.
"""
return_type = ClientResult(self.context, {})
params = {"accountName": account_name, "siteId": site_id}
qry = ServiceOperationQuery(
self, "GetSPUserInformation", params, None, None, return_type
)
self.context.add_query(qry)
return return_type
def follow(self, account_name):
"""
Add the specified user to the current user's list of followed users.
:param str account_name: Account name of the specified user.
"""
params = {"accountName": account_name}
qry = ServiceOperationQuery(self, "Follow", params, None, None, None)
self.context.add_query(qry)
return self
def stop_following(self, account_name):
"""
Remove the specified user from the current user's list of followed users.
:param str account_name:
"""
params = {"accountName": account_name}
qry = ServiceOperationQuery(self, "StopFollowing", params)
self.context.add_query(qry)
return self
def stop_following_tag(self, value):
"""
The StopFollowingTag method sets the current user to no longer be following the specified tag.
:param str value: Specifies the tag by its GUID
"""
params = {"value": value}
qry = ServiceOperationQuery(self, "StopFollowingTag", params)
self.context.add_query(qry)
return self
def get_user_profile_properties(self, user_or_name):
"""
Gets the specified user profile properties for the specified user.
:param str or User user_or_name: User or Login name of the specified user.
"""
return_type = ClientResult(self.context)
def _user_resolved(account_name):
params = {"accountName": account_name}
qry = ServiceOperationQuery(
self, "GetUserProfileProperties", params, None, None, return_type
)
self.context.add_query(qry)
_ensure_user(user_or_name, _user_resolved)
return return_type
def get_properties_for(self, user_or_name):
"""
Gets user properties for the specified user.
:param str or User user_or_name: Specifies the User object or its login name.
"""
return_type = PersonProperties(self.context)
def _get_properties_for_inner(account_name):
# type: (str) -> None
params = {"accountName": account_name}
qry = ServiceOperationQuery(
self, "GetPropertiesFor", params, None, None, return_type
)
self.context.add_query(qry)
_ensure_user(user_or_name, _get_properties_for_inner)
return return_type
def get_default_document_library(
self,
user_or_name,
create_site_if_not_exists=False,
site_creation_priority=PersonalSiteCreationPriority.Low,
):
"""
Gets the OneDrive Document library path for a given user.
:param str or User user_or_name user_or_name: The login name of the user whose OneDrive URL is required.
For example, "i:0#.f|membership|admin@contoso.sharepoint.com”.
:param bool create_site_if_not_exists: If this value is set to true and the site doesn't exist, the site will
get created.
:param int site_creation_priority: The priority for site creation. Type: PersonalSiteCreationPriority
"""
return_type = ClientResult(self.context)
def _get_default_document_library(account_name):
params = {
"accountName": account_name,
"createSiteIfNotExists": create_site_if_not_exists,
"siteCreationPriority": site_creation_priority,
}
qry = ServiceOperationQuery(
self, "GetDefaultDocumentLibrary", params, None, None, return_type
)
self.context.add_query(qry)
_ensure_user(user_or_name, _get_default_document_library)
return return_type
def get_people_followed_by(self, account_name):
"""
The GetPeopleFollowedBy method returns a list of PersonProperties objects for people who the specified user
is following. This method can result in exceptions for conditions such as null arguments or if the specified
user cannot be found.
:param str account_name: Account name of the specified user.
"""
return_type = EntityCollection(self.context, PersonProperties)
params = {"accountName": account_name}
qry = ServiceOperationQuery(
self, "GetPeopleFollowedBy", params, None, None, return_type
)
self.context.add_query(qry)
return return_type
def get_my_followers(self):
"""
This method returns a list of PersonProperties objects for the people who are following the current user.
"""
return_type = EntityCollection(self.context, PersonProperties)
qry = ServiceOperationQuery(
self, "GetMyFollowers", None, None, None, return_type
)
self.context.add_query(qry)
return return_type
def follow_tag(self, value):
"""
The FollowTag method sets the current user to be following the specified tag.
:param str value: Specifies the tag by its GUID.
"""
qry = ServiceOperationQuery(self, "FollowTag", [value])
self.context.add_query(qry)
return self
def hide_suggestion(self, account_name):
"""The HideSuggestion method adds the specified user to list of rejected suggestions.
:param str account_name: Specifies the user by account name.
"""
params = {"accountName": account_name}
qry = ServiceOperationQuery(self, "HideSuggestion", params)
self.context.add_query(qry)
return self
def reset_user_onedrive_quota_to_default(self, account_name):
"""
:param str account_name: Specifies the user by account name.
"""
return_type = ClientResult(self.context, str())
params = {"accountName": account_name}
qry = ServiceOperationQuery(
self, "ResetUserOneDriveQuotaToDefault", params, None, None, return_type
)
self.context.add_query(qry)
return return_type
def set_my_profile_picture(self, picture):
"""
The SetMyProfilePicture method uploads and sets the user profile picture. Pictures in bmp, jpg and png formats
and up to 5,000,000 bytes are supported. A user can upload a picture only to the user's own profile.
:param str or bytes picture: Binary content of an image file
"""
qry = ServiceOperationQuery(
self, "SetMyProfilePicture", None, {"picture": picture}
)
self.context.add_query(qry)
return self
def set_user_onedrive_quota(self, account_name, new_quota, new_quota_warning):
"""
:param str account_name:
:param long new_quota:
:param long new_quota_warning:
"""
return_type = ClientResult(self.context, str())
payload = {
"accountName": account_name,
"newQuota": new_quota,
"newQuotaWarning": new_quota_warning,
}
qry = ServiceOperationQuery(
self, "SetUserOneDriveQuota", None, payload, None, return_type
)
self.context.add_query(qry)
return return_type
def set_multi_valued_profile_property(
self, account_name, property_name, property_values
):
"""
Sets the value of a multivalued user profile property.
:param str account_name: Specifies the user by account name.
:param str property_name: The name of the property to set.
:param list[str] property_values: The values being set on the property.
"""
payload = {
"accountName": account_name,
"propertyName": property_name,
"propertyValues": StringCollection(property_values),
}
qry = ServiceOperationQuery(
self, "SetMultiValuedProfileProperty", None, payload
)
self.context.add_query(qry)
return self
def set_single_value_profile_property(
self, account_name, property_name, property_value
):
"""
Sets the value of a user profile property.
:param str account_name: Specifies the user by account name.
:param str property_name: The name of the property to set.
:param str property_value: The value being set on the property.
"""
payload = {
"accountName": account_name,
"propertyName": property_name,
"propertyValue": property_value,
}
qry = ServiceOperationQuery(
self, "SetSingleValueProfileProperty", None, payload
)
self.context.add_query(qry)
return self
@property
def entity_type_name(self):
return "SP.UserProfiles.PeopleManager"