From f5662ca9c40c755685212e0338345d1861ff1923 Mon Sep 17 00:00:00 2001 From: Diogo Sousa Date: Mon, 20 May 2024 11:45:37 +0100 Subject: [PATCH] fix: only update credentials on login --- .../core/openbb_core/app/model/credentials.py | 4 ++++ .../openbb_core/app/service/user_service.py | 9 --------- .../core/openbb_core/app/static/account.py | 12 +++++------- .../core/openbb_core/app/static/coverage.py | 17 +++++++---------- .../core/tests/app/service/test_user_service.py | 15 --------------- 5 files changed, 16 insertions(+), 41 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/model/credentials.py b/openbb_platform/core/openbb_core/app/model/credentials.py index a1d7792bd0e0..e8d25ad33cb7 100644 --- a/openbb_platform/core/openbb_core/app/model/credentials.py +++ b/openbb_platform/core/openbb_core/app/model/credentials.py @@ -116,3 +116,7 @@ def show(self): [f"{k}: {v}" for k, v in sorted(self.model_dump(mode="json").items())] ) ) + + def update(self, incoming: "Credentials"): + """Update current credentials.""" + self.__dict__.update(incoming.model_dump(exclude_none=True)) diff --git a/openbb_platform/core/openbb_core/app/service/user_service.py b/openbb_platform/core/openbb_core/app/service/user_service.py index 8ab438782ebe..05c0c5fd8c73 100644 --- a/openbb_platform/core/openbb_core/app/service/user_service.py +++ b/openbb_platform/core/openbb_core/app/service/user_service.py @@ -50,15 +50,6 @@ def write_default_user_settings( ) path.write_text(user_settings_json, encoding="utf-8") - @classmethod - def update_default(cls, user_settings: UserSettings) -> UserSettings: - """Update default user settings.""" - d1 = cls.read_default_user_settings().model_dump() - d2 = user_settings.model_dump() if user_settings else {} - updated = cls._merge_dicts([d1, d2]) - - return UserSettings.model_validate(updated) - @staticmethod def _merge_dicts(list_of_dicts: List[Dict[str, Any]]) -> Dict[str, Any]: """Merge a list of dictionaries.""" diff --git a/openbb_platform/core/openbb_core/app/static/account.py b/openbb_platform/core/openbb_core/app/static/account.py index b17eef7c7130..6195c85b781c 100644 --- a/openbb_platform/core/openbb_core/app/static/account.py +++ b/openbb_platform/core/openbb_core/app/static/account.py @@ -18,7 +18,7 @@ from openbb_core.app.static.app_factory import BaseApp -class Account: +class Account: # noqa: D205, D400 """/account login logout @@ -123,8 +123,8 @@ def login( """ self._hub_service = self._create_hub_service(email, password, pat) incoming = self._hub_service.pull() - updated: UserSettings = UserService.update_default(incoming) - self._base_app._command_runner.user_settings = updated + self._base_app.user.profile = incoming.profile + self._base_app.user.credentials.update(incoming.credentials) if remember_me: Path(self._openbb_directory).mkdir(parents=False, exist_ok=True) session_file = Path(self._openbb_directory, self.SESSION_FILE) @@ -185,10 +185,8 @@ def refresh(self, return_settings: bool = False) -> Optional[UserSettings]: ) else: incoming = self._hub_service.pull() - updated: UserSettings = UserService.update_default(incoming) - updated.id = self._base_app._command_runner.user_settings.id - self._base_app._command_runner.user_settings = updated - + self._base_app.user.profile = incoming.profile + self._base_app.user.credentials.update(incoming.credentials) if return_settings: return self._base_app._command_runner.user_settings return None diff --git a/openbb_platform/core/openbb_core/app/static/coverage.py b/openbb_platform/core/openbb_core/app/static/coverage.py index aa1472f0ac8a..f63ffc54ddbc 100644 --- a/openbb_platform/core/openbb_core/app/static/coverage.py +++ b/openbb_platform/core/openbb_core/app/static/coverage.py @@ -11,16 +11,13 @@ from openbb_core.app.static.app_factory import BaseApp -class Coverage: - """Coverage class. - - /coverage - - providers - commands - command_model - command_schemas - reference +class Coverage: # noqa: D205, D400 + """/coverage + providers + commands + command_model + command_schemas + reference """ def __init__(self, app: "BaseApp"): diff --git a/openbb_platform/core/tests/app/service/test_user_service.py b/openbb_platform/core/tests/app/service/test_user_service.py index e65f0a56aca9..cf64ff98de20 100644 --- a/openbb_platform/core/tests/app/service/test_user_service.py +++ b/openbb_platform/core/tests/app/service/test_user_service.py @@ -4,7 +4,6 @@ import tempfile from pathlib import Path -from openbb_core.app.model.defaults import Defaults from openbb_core.app.service.user_service import ( UserService, UserSettings, @@ -47,20 +46,6 @@ def test_write_default_user_settings(): temp_path.unlink() -def test_update_default(): - """Test update default user settings.""" - - # Some settings - defaults_test = Defaults(routes={"test": {"test": "test"}}) - other_settings = UserSettings(defaults=defaults_test) - - # Update the default settings - updated_settings = UserService.update_default(other_settings) - - assert "test" in updated_settings.defaults.model_dump()["routes"] - assert updated_settings.defaults.model_dump()["routes"]["test"] == {"test": "test"} - - def test_merge_dicts(): """Test merge dicts.""" result = UserService._merge_dicts( # pylint: disable=protected-access