Store account information asynchronously when completing request

And make the PyQt properties listen to the signal of completion.

Contributes to issue CURA-8539.
This commit is contained in:
Ghostkeeper 2021-11-19 16:02:25 +01:00
parent 591a2f89b8
commit 92437a920d
No known key found for this signature in database
GPG key ID: D2A8871EE34EC59A

View file

@ -1,15 +1,15 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from datetime import datetime
from typing import Any, Optional, Dict, TYPE_CHECKING, Callable
from datetime import datetime
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty, QTimer, Q_ENUMS from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty, QTimer, Q_ENUMS
from typing import Any, Optional, Dict, TYPE_CHECKING, Callable
from UM.Logger import Logger from UM.Logger import Logger
from UM.Message import Message from UM.Message import Message
from UM.i18n import i18nCatalog from UM.i18n import i18nCatalog
from cura.OAuth2.AuthorizationService import AuthorizationService from cura.OAuth2.AuthorizationService import AuthorizationService
from cura.OAuth2.Models import OAuth2Settings from cura.OAuth2.Models import OAuth2Settings, UserProfile
from cura.UltimakerCloud import UltimakerCloudConstants from cura.UltimakerCloud import UltimakerCloudConstants
if TYPE_CHECKING: if TYPE_CHECKING:
@ -46,6 +46,9 @@ class Account(QObject):
loginStateChanged = pyqtSignal(bool) loginStateChanged = pyqtSignal(bool)
"""Signal emitted when user logged in or out""" """Signal emitted when user logged in or out"""
userProfileChanged = pyqtSignal()
"""Signal emitted when new account information is available."""
additionalRightsChanged = pyqtSignal("QVariantMap") additionalRightsChanged = pyqtSignal("QVariantMap")
"""Signal emitted when a users additional rights change""" """Signal emitted when a users additional rights change"""
@ -73,6 +76,7 @@ class Account(QObject):
self._error_message = None # type: Optional[Message] self._error_message = None # type: Optional[Message]
self._logged_in = False self._logged_in = False
self._user_profile = None
self._additional_rights: Dict[str, Any] = {} self._additional_rights: Dict[str, Any] = {}
self._sync_state = SyncState.IDLE self._sync_state = SyncState.IDLE
self._manual_sync_enabled = False self._manual_sync_enabled = False
@ -196,12 +200,17 @@ class Account(QObject):
self._logged_in = logged_in self._logged_in = logged_in
self.loginStateChanged.emit(logged_in) self.loginStateChanged.emit(logged_in)
if logged_in: if logged_in:
self._authorization_service.getUserProfile(self._onProfileChanged)
self._setManualSyncEnabled(False) self._setManualSyncEnabled(False)
self._sync() self._sync()
else: else:
if self._update_timer.isActive(): if self._update_timer.isActive():
self._update_timer.stop() self._update_timer.stop()
def _onProfileChanged(self, profile: UserProfile):
self._user_profile = profile
self.userProfileChanged.emit(profile)
def _sync(self) -> None: def _sync(self) -> None:
"""Signals all sync services to start syncing """Signals all sync services to start syncing
@ -243,32 +252,28 @@ class Account(QObject):
return return
self._authorization_service.startAuthorizationFlow(force_logout_before_login) self._authorization_service.startAuthorizationFlow(force_logout_before_login)
@pyqtProperty(str, notify=loginStateChanged) @pyqtProperty(str, notify = userProfileChanged)
def userName(self): def userName(self):
user_profile = self._authorization_service.getUserProfile() if not self._user_profile:
if not user_profile: return ""
return None return self._user_profile.username
return user_profile.username
@pyqtProperty(str, notify = loginStateChanged) @pyqtProperty(str, notify = userProfileChanged)
def profileImageUrl(self): def profileImageUrl(self):
user_profile = self._authorization_service.getUserProfile() if not self._user_profile:
if not user_profile: return ""
return None return self._user_profile.profile_image_url
return user_profile.profile_image_url
@pyqtProperty(str, notify=accessTokenChanged) @pyqtProperty(str, notify=accessTokenChanged)
def accessToken(self) -> Optional[str]: def accessToken(self) -> Optional[str]:
return self._authorization_service.getAccessToken() return self._authorization_service.getAccessToken()
@pyqtProperty("QVariantMap", notify = loginStateChanged) @pyqtProperty("QVariantMap", notify = userProfileChanged)
def userProfile(self) -> Optional[Dict[str, Optional[str]]]: def userProfile(self) -> Optional[Dict[str, Optional[str]]]:
"""None if no user is logged in otherwise the logged in user as a dict containing containing user_id, username and profile_image_url """ """None if no user is logged in otherwise the logged in user as a dict containing containing user_id, username and profile_image_url """
if not self._user_profile:
user_profile = self._authorization_service.getUserProfile()
if not user_profile:
return None return None
return user_profile.__dict__ return self._user_profile.__dict__
@pyqtProperty(str, notify=lastSyncDateTimeChanged) @pyqtProperty(str, notify=lastSyncDateTimeChanged)
def lastSyncDateTime(self) -> str: def lastSyncDateTime(self) -> str: