|
| 1 | +import datetime |
| 2 | + |
1 | 3 | from .future import DSSFuture |
2 | 4 | import json, warnings |
3 | 5 |
|
@@ -164,23 +166,21 @@ def delete(self): |
164 | 166 | def get_settings(self): |
165 | 167 | """ |
166 | 168 | Gets the settings of the user |
| 169 | +
|
167 | 170 | :rtype: :class:`DSSUserSettings` |
168 | 171 | """ |
169 | 172 | raw = self.client._perform_json("GET", "/admin/users/%s" % self.login) |
170 | 173 | return DSSUserSettings(self.client, self.login, raw) |
171 | 174 |
|
172 | | - def get_last_activity(self): |
| 175 | + def get_activity(self): |
173 | 176 | """ |
174 | | - Gets the last activity of a user: |
175 | | -
|
176 | | - * last successful login attempt |
177 | | - * last failed login attempt |
178 | | - * last loading of DSS (new tab or tab refresh) |
| 177 | + Gets the activity of the user |
179 | 178 |
|
180 | | - :return: the user's activity, as a dict |
181 | | - :rtype: dict |
| 179 | + :return: the user's activity |
| 180 | + :rtype: :class:`DSSUserActivity` |
182 | 181 | """ |
183 | | - return self.client._perform_json("GET", "/admin/users/%s/activity" % self.login) |
| 182 | + raw = self.client._perform_json("GET", "/admin/users/%s/activity" % self.login) |
| 183 | + return DSSUserActivity(self.client, self.login, raw) |
184 | 184 |
|
185 | 185 | ######################################################## |
186 | 186 | # Legacy |
@@ -237,6 +237,7 @@ def get_client_as(self): |
237 | 237 | else: |
238 | 238 | raise ValueError("Don't know how to proxy this client") |
239 | 239 |
|
| 240 | + |
240 | 241 | class DSSOwnUser(object): |
241 | 242 | """ |
242 | 243 | A handle to interact with your own user |
@@ -377,6 +378,73 @@ def save(self): |
377 | 378 | self.client._perform_empty("PUT", "/current-user", body = self.settings) |
378 | 379 |
|
379 | 380 |
|
| 381 | +class DSSUserActivity(object): |
| 382 | + """ |
| 383 | + Settings for a DSS user. |
| 384 | + Do not call this directly, use :meth:`DSSUser.get_activity` |
| 385 | + """ |
| 386 | + |
| 387 | + def __init__(self, client, login, activity): |
| 388 | + self.client = client |
| 389 | + self.login = login |
| 390 | + self.activity = activity |
| 391 | + |
| 392 | + def get_raw(self): |
| 393 | + """ |
| 394 | + Get the raw activity of the user as a dict. |
| 395 | +
|
| 396 | + :return: the raw activity |
| 397 | + :rtype: dict |
| 398 | + """ |
| 399 | + return self.activity |
| 400 | + |
| 401 | + def get_last_successful_login_attempt(self, as_date=False): |
| 402 | + """ |
| 403 | + Get the last successful login attempt of the user as a timestamp or as a :class:`datetime.datetime` |
| 404 | + |
| 405 | + Returns `0` or `1970-01-01 01:00:00` if there is no logged attempt. |
| 406 | +
|
| 407 | + :return: the last successful login attempt |
| 408 | + :rtype: int or :class:`datetime.datetime` |
| 409 | + """ |
| 410 | + timestamp = self.activity["lastSuccessfulLogin"] |
| 411 | + if as_date: |
| 412 | + return datetime.datetime.fromtimestamp(timestamp / 1000) |
| 413 | + else: |
| 414 | + return timestamp |
| 415 | + |
| 416 | + def get_last_failed_login_attempt(self, as_date=False): |
| 417 | + """ |
| 418 | + Get the last failed login attempt of the user as a timestamp or as a :class:`datetime.datetime` |
| 419 | +
|
| 420 | + Returns `0` or `1970-01-01 01:00:00` if there is no logged attempt. |
| 421 | +
|
| 422 | + :return: the last failed login attempt |
| 423 | + :rtype: int or :class:`datetime.datetime` |
| 424 | + """ |
| 425 | + timestamp = self.activity["lastFailedLogin"] |
| 426 | + if as_date: |
| 427 | + return datetime.datetime.fromtimestamp(timestamp / 1000) |
| 428 | + else: |
| 429 | + return timestamp |
| 430 | + |
| 431 | + def get_last_session_loading(self, as_date=False): |
| 432 | + """ |
| 433 | + Get the last session loading of the user as a timestamp or as a :class:`datetime.datetime`, i.e. the last time |
| 434 | + he opened a new DSS tab or refreshed his session. |
| 435 | +
|
| 436 | + Returns `0` or `1970-01-01 01:00:00` if there is no logged attempt. |
| 437 | +
|
| 438 | + :return: the last session loading |
| 439 | + :rtype: int or :class:`datetime.datetime` |
| 440 | + """ |
| 441 | + timestamp = self.activity["lastLoaded"] |
| 442 | + if as_date: |
| 443 | + return datetime.datetime.fromtimestamp(timestamp / 1000) |
| 444 | + else: |
| 445 | + return timestamp |
| 446 | + |
| 447 | + |
380 | 448 | class DSSGroup(object): |
381 | 449 | """ |
382 | 450 | A group on the DSS instance. |
|
0 commit comments