pygeobox 1.0.1__py3-none-any.whl → 1.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pygeobox/__init__.py +63 -63
- pygeobox/api.py +2517 -2517
- pygeobox/apikey.py +232 -232
- pygeobox/attachment.py +297 -297
- pygeobox/base.py +364 -364
- pygeobox/basemap.py +162 -162
- pygeobox/dashboard.py +316 -316
- pygeobox/enums.py +354 -354
- pygeobox/exception.py +47 -47
- pygeobox/field.py +309 -309
- pygeobox/map.py +858 -858
- pygeobox/model3d.py +334 -334
- pygeobox/mosaic.py +647 -647
- pygeobox/plan.py +260 -260
- pygeobox/query.py +668 -668
- pygeobox/raster.py +756 -756
- pygeobox/route.py +63 -63
- pygeobox/scene.py +317 -317
- pygeobox/settings.py +165 -165
- pygeobox/task.py +354 -354
- pygeobox/tile3d.py +294 -294
- pygeobox/tileset.py +585 -585
- pygeobox/user.py +423 -423
- pygeobox/utils.py +43 -43
- pygeobox/vectorlayer.py +1149 -1149
- pygeobox/version.py +248 -248
- pygeobox/view.py +859 -859
- pygeobox/workflow.py +315 -315
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/METADATA +18 -33
- pygeobox-1.0.3.dist-info/RECORD +37 -0
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/licenses/LICENSE +21 -21
- pygeobox-1.0.1.dist-info/RECORD +0 -37
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/WHEEL +0 -0
- {pygeobox-1.0.1.dist-info → pygeobox-1.0.3.dist-info}/top_level.txt +0 -0
pygeobox/user.py
CHANGED
@@ -1,424 +1,424 @@
|
|
1
|
-
from typing import List, Any, TYPE_CHECKING, Union, Dict
|
2
|
-
from urllib.parse import urlencode, urljoin
|
3
|
-
|
4
|
-
from .base import Base
|
5
|
-
from .utils import clean_data
|
6
|
-
from .enums import UserRole, UserStatus
|
7
|
-
from .plan import Plan
|
8
|
-
|
9
|
-
if TYPE_CHECKING:
|
10
|
-
from . import GeoboxClient
|
11
|
-
|
12
|
-
|
13
|
-
class User(Base):
|
14
|
-
|
15
|
-
BASE_ENDPOINT: str = 'users/'
|
16
|
-
|
17
|
-
def __init__(self, api: 'GeoboxClient', user_id: int, data: dict = {}) -> None:
|
18
|
-
"""
|
19
|
-
Initialize a User instance.
|
20
|
-
|
21
|
-
Args:
|
22
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
23
|
-
user_id (int): the id of the user
|
24
|
-
data (Dict): The data of the user.
|
25
|
-
"""
|
26
|
-
super().__init__(api, data=data)
|
27
|
-
self.user_id = user_id
|
28
|
-
self.endpoint = urljoin(self.BASE_ENDPOINT, f'{self.user_id}/') if self.user_id else 'me'
|
29
|
-
|
30
|
-
|
31
|
-
def __repr__(self) -> str:
|
32
|
-
"""
|
33
|
-
Return a string representation of the User instance.
|
34
|
-
|
35
|
-
Returns:
|
36
|
-
str: A string representation of the User instance.
|
37
|
-
"""
|
38
|
-
return f'User(first_name={self.first_name}, last_name={self.last_name})'
|
39
|
-
|
40
|
-
|
41
|
-
@property
|
42
|
-
def status(self) -> 'UserStatus':
|
43
|
-
"""
|
44
|
-
User status Property
|
45
|
-
|
46
|
-
Returns:
|
47
|
-
UserStatus: the user status
|
48
|
-
"""
|
49
|
-
return UserStatus(self.data.get('status')) if self.data.get('status') else None
|
50
|
-
|
51
|
-
|
52
|
-
@property
|
53
|
-
def plan(self) -> 'Plan':
|
54
|
-
"""
|
55
|
-
User plan Property
|
56
|
-
|
57
|
-
Returns:
|
58
|
-
Plan: the plan object
|
59
|
-
"""
|
60
|
-
plan = self.data.get('plan', {})
|
61
|
-
return Plan(self.api, plan.get('id'), plan) if plan else None
|
62
|
-
|
63
|
-
|
64
|
-
@classmethod
|
65
|
-
def get_users(cls, api: 'GeoboxClient', **kwargs) -> Union[List['User'], int]:
|
66
|
-
"""
|
67
|
-
Retrieves a list of users (Permission Required)
|
68
|
-
|
69
|
-
Args:
|
70
|
-
api (GeoboxClient): The API instance.
|
71
|
-
|
72
|
-
Keyword Args:
|
73
|
-
status (UserStatus): the status of the users filter.
|
74
|
-
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
|
75
|
-
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
76
|
-
search_fields (str): comma separated list of fields for searching.
|
77
|
-
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
78
|
-
return_count (bool): Whether to return total count. default is False.
|
79
|
-
skip (int): Number of items to skip. default is 0.
|
80
|
-
limit (int): Number of items to return. default is 10.
|
81
|
-
user_id (int): Specific user. privileges required.
|
82
|
-
shared (bool): Whether to return shared maps. default is False.
|
83
|
-
|
84
|
-
Returns:
|
85
|
-
List[User] | int: list of users or the count number.
|
86
|
-
|
87
|
-
Example:
|
88
|
-
>>> from geobox import Geoboxclient
|
89
|
-
>>> from geobox.user import User
|
90
|
-
>>> client = GeoboxClient()
|
91
|
-
>>> users = User.get_users(client)
|
92
|
-
or
|
93
|
-
>>> users = client.get_users()
|
94
|
-
"""
|
95
|
-
params = {
|
96
|
-
'f': 'json',
|
97
|
-
'q': kwargs.get('q'),
|
98
|
-
'search': kwargs.get('search'),
|
99
|
-
'search_fields': kwargs.get('search_fields'),
|
100
|
-
'order_by': kwargs.get('order_by'),
|
101
|
-
'return_count': kwargs.get('return_count', False),
|
102
|
-
'skip': kwargs.get('skip', 0),
|
103
|
-
'limit': kwargs.get('limit', 10),
|
104
|
-
'user_id': kwargs.get('user_id'),
|
105
|
-
'shared': kwargs.get('shared', False)
|
106
|
-
}
|
107
|
-
return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: User(api, item['id'], item))
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
@classmethod
|
112
|
-
def create_user(cls,
|
113
|
-
api: 'GeoboxClient',
|
114
|
-
username: str,
|
115
|
-
email: str,
|
116
|
-
password: str,
|
117
|
-
role: 'UserRole',
|
118
|
-
first_name: str,
|
119
|
-
last_name: str,
|
120
|
-
mobile: str,
|
121
|
-
status: 'UserStatus') -> 'User':
|
122
|
-
"""
|
123
|
-
Create a User (Permission Required)
|
124
|
-
|
125
|
-
Args:
|
126
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
127
|
-
username (str): the username of the user.
|
128
|
-
email (str): the email of the user.
|
129
|
-
password (str): the password of the user.
|
130
|
-
role (UserRole): the role of the user.
|
131
|
-
first_name (str): the firstname of the user.
|
132
|
-
last_name (str): the lastname of the user.
|
133
|
-
mobile (str): the mobile number of the user. e.g. "+98 9120123456".
|
134
|
-
status (UserStatus): the status of the user.
|
135
|
-
|
136
|
-
Returns:
|
137
|
-
User: the user object.
|
138
|
-
|
139
|
-
Example:
|
140
|
-
>>> from geobox import GeoboxClient
|
141
|
-
>>> from geobox.user import User
|
142
|
-
>>> client = GeoboxClient()
|
143
|
-
>>> user = User.create_user(client,
|
144
|
-
... username="user1",
|
145
|
-
... email="user1@example.com",
|
146
|
-
... password="P@ssw0rd",
|
147
|
-
... role=UserRole.ACCOUNT_ADMIN,
|
148
|
-
... first_name="user 1",
|
149
|
-
... last_name="user 1",
|
150
|
-
... mobile="+98 9120123456",
|
151
|
-
... status=UserStatus.ACTIVE)
|
152
|
-
or
|
153
|
-
>>> user = client.create_user(username="user1",
|
154
|
-
... email="user1@example.com",
|
155
|
-
... password="P@ssw0rd",
|
156
|
-
... role=UserRole.ACCOUNT_ADMIN,
|
157
|
-
... first_name="user 1",
|
158
|
-
... last_name="user 1",
|
159
|
-
... mobile="+98 9120123456",
|
160
|
-
... status=UserStatus.ACTIVE)
|
161
|
-
"""
|
162
|
-
data = {
|
163
|
-
"username": username,
|
164
|
-
"email": email,
|
165
|
-
"password": password,
|
166
|
-
"role": role.value,
|
167
|
-
"first_name": first_name,
|
168
|
-
"last_name": last_name,
|
169
|
-
"mobile": mobile,
|
170
|
-
"status": status.value
|
171
|
-
}
|
172
|
-
return super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: User(api, item['id'], item))
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
@classmethod
|
177
|
-
def search_users(cls, api: 'GeoboxClient', search: str = None, skip: int = 0, limit: int = 10) -> List['User']:
|
178
|
-
"""
|
179
|
-
Get list of users based on the search term.
|
180
|
-
|
181
|
-
Args:
|
182
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
183
|
-
search (str, optional): The Search Term.
|
184
|
-
skip (int, optional): Number of items to skip. default is 0.
|
185
|
-
limit (int, optional): Number of items to return. default is 10.
|
186
|
-
|
187
|
-
Returns:
|
188
|
-
List[User]: A list of User instances.
|
189
|
-
|
190
|
-
Example:
|
191
|
-
>>> from geobox import GeoboxClient
|
192
|
-
>>> from geobox.user import User
|
193
|
-
>>> client = GeoboxClient()
|
194
|
-
>>> users = User.get_users(client, search="John")
|
195
|
-
or
|
196
|
-
>>> users = client.get_users(search="John")
|
197
|
-
"""
|
198
|
-
params = {
|
199
|
-
'search': search,
|
200
|
-
'skip': skip,
|
201
|
-
'limit': limit
|
202
|
-
}
|
203
|
-
endpoint = urljoin(cls.BASE_ENDPOINT, 'search/')
|
204
|
-
return super()._get_list(api, endpoint, params, factory_func=lambda api, item: User(api, item['id'], item))
|
205
|
-
|
206
|
-
|
207
|
-
@classmethod
|
208
|
-
def get_user(cls, api: 'GeoboxClient', user_id: int = 'me') -> 'User':
|
209
|
-
"""
|
210
|
-
Get a user by its id (Permission Required)
|
211
|
-
|
212
|
-
Args:
|
213
|
-
api (GeoboxClient): The GeoboxClient instance for making requests.
|
214
|
-
user_id (int, optional): Specific user. don't specify a user_id to get the current user.
|
215
|
-
|
216
|
-
Returns:
|
217
|
-
User: the user object.
|
218
|
-
|
219
|
-
Raises:
|
220
|
-
NotFoundError: If the user with the specified id is not found.
|
221
|
-
|
222
|
-
Example:
|
223
|
-
>>> from geobox import GeoboxClient
|
224
|
-
>>> from geobox.user import User
|
225
|
-
>>> client = GeoboxClient()
|
226
|
-
>>> user = User.get_user(client, user_id=1)
|
227
|
-
or
|
228
|
-
>>> user = client.get_user(user_id=1)
|
229
|
-
|
230
|
-
get the current user
|
231
|
-
>>> user = User.get_user(client)
|
232
|
-
or
|
233
|
-
>>> user = client.get_user()
|
234
|
-
"""
|
235
|
-
params = {
|
236
|
-
'f': 'json'
|
237
|
-
}
|
238
|
-
return super()._get_detail(api, cls.BASE_ENDPOINT, user_id, params, factory_func=lambda api, item: User(api, item['id'], item))
|
239
|
-
|
240
|
-
|
241
|
-
def update(self, **kwargs) -> Dict:
|
242
|
-
"""
|
243
|
-
Update the user (Permission Required)
|
244
|
-
|
245
|
-
Keyword Args:
|
246
|
-
username (str)
|
247
|
-
email (str)
|
248
|
-
first_name (str)
|
249
|
-
last_name (str)
|
250
|
-
mobile (str): e.g. "+98 9120123456"
|
251
|
-
status (UserStatus)
|
252
|
-
role (UserRole)
|
253
|
-
plan (Plan)
|
254
|
-
expiration_date (str)
|
255
|
-
|
256
|
-
Returns:
|
257
|
-
Dict: updated data
|
258
|
-
|
259
|
-
Example:
|
260
|
-
>>> from geobox imoprt GeoboxClient
|
261
|
-
>>> from geobox.user import User
|
262
|
-
>>> client = GeoboxClient()
|
263
|
-
>>> user = User.get_user(client, user_id=1)
|
264
|
-
>>> user.update_user(status=UserStatus.PENDING)
|
265
|
-
"""
|
266
|
-
data = {
|
267
|
-
"csrf_token": kwargs.get('csrf_token'),
|
268
|
-
"username": kwargs.get('username'),
|
269
|
-
"email": kwargs.get('email'),
|
270
|
-
"first_name": kwargs.get('first_name'),
|
271
|
-
"last_name": kwargs.get('last_name'),
|
272
|
-
"mobile": kwargs.get('mobile'),
|
273
|
-
"status": kwargs.get('status').value if kwargs.get('status') else None,
|
274
|
-
"role": kwargs.get('role').value if kwargs.get('role') else None,
|
275
|
-
"plan_id": kwargs.get('plan').id if kwargs.get('plan') else None,
|
276
|
-
"expiration_date": kwargs.get('expiration_date')
|
277
|
-
}
|
278
|
-
return super()._update(self.endpoint, data)
|
279
|
-
|
280
|
-
|
281
|
-
def delete(self) -> None:
|
282
|
-
"""
|
283
|
-
Delete the user (Permission Required)
|
284
|
-
|
285
|
-
Returns:
|
286
|
-
None
|
287
|
-
|
288
|
-
Example:
|
289
|
-
>>> from geobox import GeoboxClient
|
290
|
-
>>> from geobox.user import User
|
291
|
-
>>> client = GeoboxClient()
|
292
|
-
>>> user = User.get_user(client, user_id=1)
|
293
|
-
>>> user.delete()
|
294
|
-
"""
|
295
|
-
super().delete(self.endpoint)
|
296
|
-
|
297
|
-
|
298
|
-
def get_sessions(self, user_id: int = 'me') -> List['Session']:
|
299
|
-
"""
|
300
|
-
Get a list of user available sessions (Permission Required)
|
301
|
-
|
302
|
-
Args:
|
303
|
-
user_id (int, optional): Specific user. don't specify user_id to get the current user.
|
304
|
-
|
305
|
-
Returns:
|
306
|
-
List[Session]: list of user sessions.
|
307
|
-
|
308
|
-
Example:
|
309
|
-
>>> from geobox import GeoboxClient
|
310
|
-
>>> from geobox.user import User
|
311
|
-
>>> client = GeoboxClient()
|
312
|
-
>>> user = User.get_user(client, user_id=1)
|
313
|
-
or
|
314
|
-
>>> user = client.get_user(user_id=1)
|
315
|
-
|
316
|
-
>>> user.get_user_sessions()
|
317
|
-
or
|
318
|
-
>>> client.get_user_sessions()
|
319
|
-
"""
|
320
|
-
params = clean_data({
|
321
|
-
'f': 'json'
|
322
|
-
})
|
323
|
-
query_string = urlencode(params)
|
324
|
-
if user_id != 'me':
|
325
|
-
user = self.get_user(self.api, user_id=user_id)
|
326
|
-
endpoint = f"{self.BASE_ENDPOINT}{user_id}/sessions/?{query_string}"
|
327
|
-
else:
|
328
|
-
user = self
|
329
|
-
endpoint = urljoin(self.endpoint, f'sessions/?{query_string}')
|
330
|
-
|
331
|
-
response = self.api.get(endpoint)
|
332
|
-
return [Session(item['uuid'], item, user) for item in response]
|
333
|
-
|
334
|
-
|
335
|
-
def change_password(self, new_password: str) -> None:
|
336
|
-
"""
|
337
|
-
Change the user password (privileges required)
|
338
|
-
|
339
|
-
Args:
|
340
|
-
new_password (str): new password for the user.
|
341
|
-
|
342
|
-
Returns:
|
343
|
-
None
|
344
|
-
|
345
|
-
Example:
|
346
|
-
>>> from geobox import GeoboxClient
|
347
|
-
>>> from geobox.user import User
|
348
|
-
>>> client = GeoboxClient()
|
349
|
-
>>> user = client.get_user()
|
350
|
-
>>> user.change_password(new_password='user_new_password')
|
351
|
-
"""
|
352
|
-
data = clean_data({
|
353
|
-
"new_password": new_password
|
354
|
-
})
|
355
|
-
endpoint = urljoin(self.endpoint, 'change-password')
|
356
|
-
self.api.post(endpoint, data, is_json=False)
|
357
|
-
|
358
|
-
|
359
|
-
def renew_plan(self) -> None:
|
360
|
-
"""
|
361
|
-
Renew the user plan
|
362
|
-
|
363
|
-
Returns:
|
364
|
-
None
|
365
|
-
|
366
|
-
Example:
|
367
|
-
>>> from geobox import GeoboxClient
|
368
|
-
>>> user = client.get_user(user_id=1)
|
369
|
-
>>> user.renew_plan()
|
370
|
-
"""
|
371
|
-
endpoint = urljoin(self.endpoint, 'renewPlan')
|
372
|
-
self.api.post(endpoint)
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
class Session(Base):
|
378
|
-
def __init__(self, uuid: str, data: Dict, user: 'User'):
|
379
|
-
"""
|
380
|
-
Initialize a user session instance.
|
381
|
-
|
382
|
-
Args:
|
383
|
-
uuid (str): The unique identifier for the user session.
|
384
|
-
data (Dict): The data of the session.
|
385
|
-
user (User): the user instance.
|
386
|
-
"""
|
387
|
-
self.uuid = uuid
|
388
|
-
self.data = data
|
389
|
-
self.user = user
|
390
|
-
self.endpoint = urljoin(self.user.endpoint, f'sessions/{self.uuid}')
|
391
|
-
|
392
|
-
|
393
|
-
def __repr__(self) -> str:
|
394
|
-
"""
|
395
|
-
Return a string representation of the resource.
|
396
|
-
|
397
|
-
Returns:
|
398
|
-
str: A string representation of the Session object.
|
399
|
-
"""
|
400
|
-
return f"Session(user={self.user}, agent='{self.agent}')"
|
401
|
-
|
402
|
-
|
403
|
-
def close(self) -> None:
|
404
|
-
"""
|
405
|
-
Close the user session
|
406
|
-
|
407
|
-
Returns:
|
408
|
-
None
|
409
|
-
|
410
|
-
Example:
|
411
|
-
>>> from geobox import geoboxClient
|
412
|
-
>>> from geobox.user import User
|
413
|
-
>>> client = GeoboxClient()
|
414
|
-
>>> user = User.get_user(client) # without user_id parameter, it gets the current user
|
415
|
-
or
|
416
|
-
>>> user = client.get_user() # without user_id parameter, it gets the current user
|
417
|
-
>>> session = user.get_sessions()[0]
|
418
|
-
>>> session.close()
|
419
|
-
"""
|
420
|
-
data = clean_data({
|
421
|
-
'user_id': self.user.user_id,
|
422
|
-
'session_uuid': self.uuid
|
423
|
-
})
|
1
|
+
from typing import List, Any, TYPE_CHECKING, Union, Dict
|
2
|
+
from urllib.parse import urlencode, urljoin
|
3
|
+
|
4
|
+
from .base import Base
|
5
|
+
from .utils import clean_data
|
6
|
+
from .enums import UserRole, UserStatus
|
7
|
+
from .plan import Plan
|
8
|
+
|
9
|
+
if TYPE_CHECKING:
|
10
|
+
from . import GeoboxClient
|
11
|
+
|
12
|
+
|
13
|
+
class User(Base):
|
14
|
+
|
15
|
+
BASE_ENDPOINT: str = 'users/'
|
16
|
+
|
17
|
+
def __init__(self, api: 'GeoboxClient', user_id: int, data: dict = {}) -> None:
|
18
|
+
"""
|
19
|
+
Initialize a User instance.
|
20
|
+
|
21
|
+
Args:
|
22
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
23
|
+
user_id (int): the id of the user
|
24
|
+
data (Dict): The data of the user.
|
25
|
+
"""
|
26
|
+
super().__init__(api, data=data)
|
27
|
+
self.user_id = user_id
|
28
|
+
self.endpoint = urljoin(self.BASE_ENDPOINT, f'{self.user_id}/') if self.user_id else 'me'
|
29
|
+
|
30
|
+
|
31
|
+
def __repr__(self) -> str:
|
32
|
+
"""
|
33
|
+
Return a string representation of the User instance.
|
34
|
+
|
35
|
+
Returns:
|
36
|
+
str: A string representation of the User instance.
|
37
|
+
"""
|
38
|
+
return f'User(first_name={self.first_name}, last_name={self.last_name})'
|
39
|
+
|
40
|
+
|
41
|
+
@property
|
42
|
+
def status(self) -> 'UserStatus':
|
43
|
+
"""
|
44
|
+
User status Property
|
45
|
+
|
46
|
+
Returns:
|
47
|
+
UserStatus: the user status
|
48
|
+
"""
|
49
|
+
return UserStatus(self.data.get('status')) if self.data.get('status') else None
|
50
|
+
|
51
|
+
|
52
|
+
@property
|
53
|
+
def plan(self) -> 'Plan':
|
54
|
+
"""
|
55
|
+
User plan Property
|
56
|
+
|
57
|
+
Returns:
|
58
|
+
Plan: the plan object
|
59
|
+
"""
|
60
|
+
plan = self.data.get('plan', {})
|
61
|
+
return Plan(self.api, plan.get('id'), plan) if plan else None
|
62
|
+
|
63
|
+
|
64
|
+
@classmethod
|
65
|
+
def get_users(cls, api: 'GeoboxClient', **kwargs) -> Union[List['User'], int]:
|
66
|
+
"""
|
67
|
+
Retrieves a list of users (Permission Required)
|
68
|
+
|
69
|
+
Args:
|
70
|
+
api (GeoboxClient): The API instance.
|
71
|
+
|
72
|
+
Keyword Args:
|
73
|
+
status (UserStatus): the status of the users filter.
|
74
|
+
q (str): query filter based on OGC CQL standard. e.g. "field1 LIKE '%GIS%' AND created_at > '2021-01-01'"
|
75
|
+
search (str): search term for keyword-based searching among search_fields or all textual fields if search_fields does not have value. NOTE: if q param is defined this param will be ignored.
|
76
|
+
search_fields (str): comma separated list of fields for searching.
|
77
|
+
order_by (str): comma separated list of fields for sorting results [field1 A|D, field2 A|D, …]. e.g. name A, type D. NOTE: "A" denotes ascending order and "D" denotes descending order.
|
78
|
+
return_count (bool): Whether to return total count. default is False.
|
79
|
+
skip (int): Number of items to skip. default is 0.
|
80
|
+
limit (int): Number of items to return. default is 10.
|
81
|
+
user_id (int): Specific user. privileges required.
|
82
|
+
shared (bool): Whether to return shared maps. default is False.
|
83
|
+
|
84
|
+
Returns:
|
85
|
+
List[User] | int: list of users or the count number.
|
86
|
+
|
87
|
+
Example:
|
88
|
+
>>> from geobox import Geoboxclient
|
89
|
+
>>> from geobox.user import User
|
90
|
+
>>> client = GeoboxClient()
|
91
|
+
>>> users = User.get_users(client)
|
92
|
+
or
|
93
|
+
>>> users = client.get_users()
|
94
|
+
"""
|
95
|
+
params = {
|
96
|
+
'f': 'json',
|
97
|
+
'q': kwargs.get('q'),
|
98
|
+
'search': kwargs.get('search'),
|
99
|
+
'search_fields': kwargs.get('search_fields'),
|
100
|
+
'order_by': kwargs.get('order_by'),
|
101
|
+
'return_count': kwargs.get('return_count', False),
|
102
|
+
'skip': kwargs.get('skip', 0),
|
103
|
+
'limit': kwargs.get('limit', 10),
|
104
|
+
'user_id': kwargs.get('user_id'),
|
105
|
+
'shared': kwargs.get('shared', False)
|
106
|
+
}
|
107
|
+
return super()._get_list(api, cls.BASE_ENDPOINT, params, factory_func=lambda api, item: User(api, item['id'], item))
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
@classmethod
|
112
|
+
def create_user(cls,
|
113
|
+
api: 'GeoboxClient',
|
114
|
+
username: str,
|
115
|
+
email: str,
|
116
|
+
password: str,
|
117
|
+
role: 'UserRole',
|
118
|
+
first_name: str,
|
119
|
+
last_name: str,
|
120
|
+
mobile: str,
|
121
|
+
status: 'UserStatus') -> 'User':
|
122
|
+
"""
|
123
|
+
Create a User (Permission Required)
|
124
|
+
|
125
|
+
Args:
|
126
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
127
|
+
username (str): the username of the user.
|
128
|
+
email (str): the email of the user.
|
129
|
+
password (str): the password of the user.
|
130
|
+
role (UserRole): the role of the user.
|
131
|
+
first_name (str): the firstname of the user.
|
132
|
+
last_name (str): the lastname of the user.
|
133
|
+
mobile (str): the mobile number of the user. e.g. "+98 9120123456".
|
134
|
+
status (UserStatus): the status of the user.
|
135
|
+
|
136
|
+
Returns:
|
137
|
+
User: the user object.
|
138
|
+
|
139
|
+
Example:
|
140
|
+
>>> from geobox import GeoboxClient
|
141
|
+
>>> from geobox.user import User
|
142
|
+
>>> client = GeoboxClient()
|
143
|
+
>>> user = User.create_user(client,
|
144
|
+
... username="user1",
|
145
|
+
... email="user1@example.com",
|
146
|
+
... password="P@ssw0rd",
|
147
|
+
... role=UserRole.ACCOUNT_ADMIN,
|
148
|
+
... first_name="user 1",
|
149
|
+
... last_name="user 1",
|
150
|
+
... mobile="+98 9120123456",
|
151
|
+
... status=UserStatus.ACTIVE)
|
152
|
+
or
|
153
|
+
>>> user = client.create_user(username="user1",
|
154
|
+
... email="user1@example.com",
|
155
|
+
... password="P@ssw0rd",
|
156
|
+
... role=UserRole.ACCOUNT_ADMIN,
|
157
|
+
... first_name="user 1",
|
158
|
+
... last_name="user 1",
|
159
|
+
... mobile="+98 9120123456",
|
160
|
+
... status=UserStatus.ACTIVE)
|
161
|
+
"""
|
162
|
+
data = {
|
163
|
+
"username": username,
|
164
|
+
"email": email,
|
165
|
+
"password": password,
|
166
|
+
"role": role.value,
|
167
|
+
"first_name": first_name,
|
168
|
+
"last_name": last_name,
|
169
|
+
"mobile": mobile,
|
170
|
+
"status": status.value
|
171
|
+
}
|
172
|
+
return super()._create(api, cls.BASE_ENDPOINT, data, factory_func=lambda api, item: User(api, item['id'], item))
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
@classmethod
|
177
|
+
def search_users(cls, api: 'GeoboxClient', search: str = None, skip: int = 0, limit: int = 10) -> List['User']:
|
178
|
+
"""
|
179
|
+
Get list of users based on the search term.
|
180
|
+
|
181
|
+
Args:
|
182
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
183
|
+
search (str, optional): The Search Term.
|
184
|
+
skip (int, optional): Number of items to skip. default is 0.
|
185
|
+
limit (int, optional): Number of items to return. default is 10.
|
186
|
+
|
187
|
+
Returns:
|
188
|
+
List[User]: A list of User instances.
|
189
|
+
|
190
|
+
Example:
|
191
|
+
>>> from geobox import GeoboxClient
|
192
|
+
>>> from geobox.user import User
|
193
|
+
>>> client = GeoboxClient()
|
194
|
+
>>> users = User.get_users(client, search="John")
|
195
|
+
or
|
196
|
+
>>> users = client.get_users(search="John")
|
197
|
+
"""
|
198
|
+
params = {
|
199
|
+
'search': search,
|
200
|
+
'skip': skip,
|
201
|
+
'limit': limit
|
202
|
+
}
|
203
|
+
endpoint = urljoin(cls.BASE_ENDPOINT, 'search/')
|
204
|
+
return super()._get_list(api, endpoint, params, factory_func=lambda api, item: User(api, item['id'], item))
|
205
|
+
|
206
|
+
|
207
|
+
@classmethod
|
208
|
+
def get_user(cls, api: 'GeoboxClient', user_id: int = 'me') -> 'User':
|
209
|
+
"""
|
210
|
+
Get a user by its id (Permission Required)
|
211
|
+
|
212
|
+
Args:
|
213
|
+
api (GeoboxClient): The GeoboxClient instance for making requests.
|
214
|
+
user_id (int, optional): Specific user. don't specify a user_id to get the current user.
|
215
|
+
|
216
|
+
Returns:
|
217
|
+
User: the user object.
|
218
|
+
|
219
|
+
Raises:
|
220
|
+
NotFoundError: If the user with the specified id is not found.
|
221
|
+
|
222
|
+
Example:
|
223
|
+
>>> from geobox import GeoboxClient
|
224
|
+
>>> from geobox.user import User
|
225
|
+
>>> client = GeoboxClient()
|
226
|
+
>>> user = User.get_user(client, user_id=1)
|
227
|
+
or
|
228
|
+
>>> user = client.get_user(user_id=1)
|
229
|
+
|
230
|
+
get the current user
|
231
|
+
>>> user = User.get_user(client)
|
232
|
+
or
|
233
|
+
>>> user = client.get_user()
|
234
|
+
"""
|
235
|
+
params = {
|
236
|
+
'f': 'json'
|
237
|
+
}
|
238
|
+
return super()._get_detail(api, cls.BASE_ENDPOINT, user_id, params, factory_func=lambda api, item: User(api, item['id'], item))
|
239
|
+
|
240
|
+
|
241
|
+
def update(self, **kwargs) -> Dict:
|
242
|
+
"""
|
243
|
+
Update the user (Permission Required)
|
244
|
+
|
245
|
+
Keyword Args:
|
246
|
+
username (str)
|
247
|
+
email (str)
|
248
|
+
first_name (str)
|
249
|
+
last_name (str)
|
250
|
+
mobile (str): e.g. "+98 9120123456"
|
251
|
+
status (UserStatus)
|
252
|
+
role (UserRole)
|
253
|
+
plan (Plan)
|
254
|
+
expiration_date (str)
|
255
|
+
|
256
|
+
Returns:
|
257
|
+
Dict: updated data
|
258
|
+
|
259
|
+
Example:
|
260
|
+
>>> from geobox imoprt GeoboxClient
|
261
|
+
>>> from geobox.user import User
|
262
|
+
>>> client = GeoboxClient()
|
263
|
+
>>> user = User.get_user(client, user_id=1)
|
264
|
+
>>> user.update_user(status=UserStatus.PENDING)
|
265
|
+
"""
|
266
|
+
data = {
|
267
|
+
"csrf_token": kwargs.get('csrf_token'),
|
268
|
+
"username": kwargs.get('username'),
|
269
|
+
"email": kwargs.get('email'),
|
270
|
+
"first_name": kwargs.get('first_name'),
|
271
|
+
"last_name": kwargs.get('last_name'),
|
272
|
+
"mobile": kwargs.get('mobile'),
|
273
|
+
"status": kwargs.get('status').value if kwargs.get('status') else None,
|
274
|
+
"role": kwargs.get('role').value if kwargs.get('role') else None,
|
275
|
+
"plan_id": kwargs.get('plan').id if kwargs.get('plan') else None,
|
276
|
+
"expiration_date": kwargs.get('expiration_date')
|
277
|
+
}
|
278
|
+
return super()._update(self.endpoint, data)
|
279
|
+
|
280
|
+
|
281
|
+
def delete(self) -> None:
|
282
|
+
"""
|
283
|
+
Delete the user (Permission Required)
|
284
|
+
|
285
|
+
Returns:
|
286
|
+
None
|
287
|
+
|
288
|
+
Example:
|
289
|
+
>>> from geobox import GeoboxClient
|
290
|
+
>>> from geobox.user import User
|
291
|
+
>>> client = GeoboxClient()
|
292
|
+
>>> user = User.get_user(client, user_id=1)
|
293
|
+
>>> user.delete()
|
294
|
+
"""
|
295
|
+
super().delete(self.endpoint)
|
296
|
+
|
297
|
+
|
298
|
+
def get_sessions(self, user_id: int = 'me') -> List['Session']:
|
299
|
+
"""
|
300
|
+
Get a list of user available sessions (Permission Required)
|
301
|
+
|
302
|
+
Args:
|
303
|
+
user_id (int, optional): Specific user. don't specify user_id to get the current user.
|
304
|
+
|
305
|
+
Returns:
|
306
|
+
List[Session]: list of user sessions.
|
307
|
+
|
308
|
+
Example:
|
309
|
+
>>> from geobox import GeoboxClient
|
310
|
+
>>> from geobox.user import User
|
311
|
+
>>> client = GeoboxClient()
|
312
|
+
>>> user = User.get_user(client, user_id=1)
|
313
|
+
or
|
314
|
+
>>> user = client.get_user(user_id=1)
|
315
|
+
|
316
|
+
>>> user.get_user_sessions()
|
317
|
+
or
|
318
|
+
>>> client.get_user_sessions()
|
319
|
+
"""
|
320
|
+
params = clean_data({
|
321
|
+
'f': 'json'
|
322
|
+
})
|
323
|
+
query_string = urlencode(params)
|
324
|
+
if user_id != 'me':
|
325
|
+
user = self.get_user(self.api, user_id=user_id)
|
326
|
+
endpoint = f"{self.BASE_ENDPOINT}{user_id}/sessions/?{query_string}"
|
327
|
+
else:
|
328
|
+
user = self
|
329
|
+
endpoint = urljoin(self.endpoint, f'sessions/?{query_string}')
|
330
|
+
|
331
|
+
response = self.api.get(endpoint)
|
332
|
+
return [Session(item['uuid'], item, user) for item in response]
|
333
|
+
|
334
|
+
|
335
|
+
def change_password(self, new_password: str) -> None:
|
336
|
+
"""
|
337
|
+
Change the user password (privileges required)
|
338
|
+
|
339
|
+
Args:
|
340
|
+
new_password (str): new password for the user.
|
341
|
+
|
342
|
+
Returns:
|
343
|
+
None
|
344
|
+
|
345
|
+
Example:
|
346
|
+
>>> from geobox import GeoboxClient
|
347
|
+
>>> from geobox.user import User
|
348
|
+
>>> client = GeoboxClient()
|
349
|
+
>>> user = client.get_user()
|
350
|
+
>>> user.change_password(new_password='user_new_password')
|
351
|
+
"""
|
352
|
+
data = clean_data({
|
353
|
+
"new_password": new_password
|
354
|
+
})
|
355
|
+
endpoint = urljoin(self.endpoint, 'change-password')
|
356
|
+
self.api.post(endpoint, data, is_json=False)
|
357
|
+
|
358
|
+
|
359
|
+
def renew_plan(self) -> None:
|
360
|
+
"""
|
361
|
+
Renew the user plan
|
362
|
+
|
363
|
+
Returns:
|
364
|
+
None
|
365
|
+
|
366
|
+
Example:
|
367
|
+
>>> from geobox import GeoboxClient
|
368
|
+
>>> user = client.get_user(user_id=1)
|
369
|
+
>>> user.renew_plan()
|
370
|
+
"""
|
371
|
+
endpoint = urljoin(self.endpoint, 'renewPlan')
|
372
|
+
self.api.post(endpoint)
|
373
|
+
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
class Session(Base):
|
378
|
+
def __init__(self, uuid: str, data: Dict, user: 'User'):
|
379
|
+
"""
|
380
|
+
Initialize a user session instance.
|
381
|
+
|
382
|
+
Args:
|
383
|
+
uuid (str): The unique identifier for the user session.
|
384
|
+
data (Dict): The data of the session.
|
385
|
+
user (User): the user instance.
|
386
|
+
"""
|
387
|
+
self.uuid = uuid
|
388
|
+
self.data = data
|
389
|
+
self.user = user
|
390
|
+
self.endpoint = urljoin(self.user.endpoint, f'sessions/{self.uuid}')
|
391
|
+
|
392
|
+
|
393
|
+
def __repr__(self) -> str:
|
394
|
+
"""
|
395
|
+
Return a string representation of the resource.
|
396
|
+
|
397
|
+
Returns:
|
398
|
+
str: A string representation of the Session object.
|
399
|
+
"""
|
400
|
+
return f"Session(user={self.user}, agent='{self.agent}')"
|
401
|
+
|
402
|
+
|
403
|
+
def close(self) -> None:
|
404
|
+
"""
|
405
|
+
Close the user session
|
406
|
+
|
407
|
+
Returns:
|
408
|
+
None
|
409
|
+
|
410
|
+
Example:
|
411
|
+
>>> from geobox import geoboxClient
|
412
|
+
>>> from geobox.user import User
|
413
|
+
>>> client = GeoboxClient()
|
414
|
+
>>> user = User.get_user(client) # without user_id parameter, it gets the current user
|
415
|
+
or
|
416
|
+
>>> user = client.get_user() # without user_id parameter, it gets the current user
|
417
|
+
>>> session = user.get_sessions()[0]
|
418
|
+
>>> session.close()
|
419
|
+
"""
|
420
|
+
data = clean_data({
|
421
|
+
'user_id': self.user.user_id,
|
422
|
+
'session_uuid': self.uuid
|
423
|
+
})
|
424
424
|
self.user.api.post(self.endpoint, data)
|