From e57de3b453afd6617a5714eef74ab5925afe5a33 Mon Sep 17 00:00:00 2001 From: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Date: Wed, 15 Feb 2023 14:59:15 +0000 Subject: [PATCH] Account menu improvements (#4252) * ask for confirmation on delete, replace * fix logout --------- Co-authored-by: James Maslek --- openbb_terminal/account/account_controller.py | 54 ++++++++++++++----- openbb_terminal/session/local_model.py | 15 ++++-- openbb_terminal/session/session_model.py | 2 +- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/openbb_terminal/account/account_controller.py b/openbb_terminal/account/account_controller.py index 56581a0e371..25ec0b14c54 100644 --- a/openbb_terminal/account/account_controller.py +++ b/openbb_terminal/account/account_controller.py @@ -177,11 +177,12 @@ class AccountController(BaseController): i = console.input( "\nDo you want to load the configurations above? (y/n): " ) + console.print("") if i.lower() in ["y", "yes"]: Local.apply_configs(configs=configs_diff) - console.print("\n[info]Done.[/info]") + console.print("[info]Done.[/info]") else: - console.print("\n[info]Aborted.[/info]") + console.print("[info]Aborted.[/info]") else: console.print("[info]No changes to apply.[/info]") @@ -197,14 +198,14 @@ class AccountController(BaseController): ns_parser = self.parse_known_args_and_warn(parser, other_args) if ns_parser: i = console.input( - "[red]This action is irreversible![/red]\n" + "[bold red]This action is irreversible![/bold red]\n" "Are you sure you want to permanently delete your data? (y/n): " ) + console.print("") if i.lower() in ["y", "yes"]: - console.print("") Hub.clear_user_configs(auth_header=User.get_auth_header()) else: - console.print("\n[info]Aborted.[/info]") + console.print("[info]Aborted.[/info]") @log_start_end(log=logger) def call_list(self, other_args: List[str]): @@ -303,7 +304,7 @@ class AccountController(BaseController): if response is not None and response.status_code == 409: i = console.input( "A routine with the same name already exists, " - "do you want to overwrite it? (y/n): " + "do you want to replace it? (y/n): " ) console.print("") if i.lower() in ["y", "yes"]: @@ -366,7 +367,23 @@ class AccountController(BaseController): file_name=file_name, routine=script, ) - if file_path: + if file_path == "File already exists": + i = console.input( + "\nA file with the same name already exists, " + "do you want to replace it? (y/n): " + ) + console.print("") + if i.lower() in ["y", "yes"]: + file_path = Local.save_routine( + file_name=file_name, + routine=script, + force=True, + ) + if file_path: + console.print(f"[info]Location:[/info] {file_path}") + else: + console.print("[info]Aborted.[/info]") + elif file_path: console.print(f"[info]Location:[/info] {file_path}") @log_start_end(log=logger) @@ -392,11 +409,20 @@ class AccountController(BaseController): ns_parser = self.parse_known_args_and_warn(parser, other_args) if ns_parser: name = " ".join(ns_parser.name) - response = Hub.delete_routine( - auth_header=User.get_auth_header(), - name=name, + + i = console.input( + "[bold red]This action is irreversible![/bold red]\n" + "Are you sure you want to delete this routine? (y/n): " ) - if response and response.status_code == 200: - if name in self.REMOTE_CHOICES: - self.REMOTE_CHOICES.remove(name) - self.update_runtime_choices() + console.print("") + if i.lower() in ["y", "yes"]: + response = Hub.delete_routine( + auth_header=User.get_auth_header(), + name=name, + ) + if response and response.status_code == 200: + if name in self.REMOTE_CHOICES: + self.REMOTE_CHOICES.remove(name) + self.update_runtime_choices() + else: + console.print("[info]Aborted.[/info]") diff --git a/openbb_terminal/session/local_model.py b/openbb_terminal/session/local_model.py index 969d93c7c87..ca990518e99 100644 --- a/openbb_terminal/session/local_model.py +++ b/openbb_terminal/session/local_model.py @@ -1,7 +1,7 @@ import json import os from pathlib import Path -from typing import Optional +from typing import Optional, Union from openbb_terminal import ( config_plot as cfg_plot, @@ -217,8 +217,11 @@ def get_routine( def save_routine( - file_name: str, routine: str, folder: Path = USER_ROUTINES_DIRECTORY -) -> Optional[Path]: + file_name: str, + routine: str, + folder: Path = USER_ROUTINES_DIRECTORY, + force: bool = False, +) -> Union[Optional[Path], str]: """Save the routine. Parameters @@ -229,10 +232,12 @@ def save_routine( The routine. folder : Path The routines folder. + force : bool + Force the save. Returns ------- - Optional[Path] + Optional[Path, str] The path to the routine or None. """ try: @@ -242,6 +247,8 @@ def save_routine( os.makedirs(user_folder) file_path = user_folder / file_name + if os.path.exists(file_path) and not force: + return "File already exists" with open(file_path, "w") as f: f.write(routine) return user_folder / file_name diff --git a/openbb_terminal/session/session_model.py b/openbb_terminal/session/session_model.py index c001e5628a9..b7a4787b98c 100644 --- a/openbb_terminal/session/session_model.py +++ b/openbb_terminal/session/session_model.py @@ -130,7 +130,7 @@ def logout( if not Local.remove_cli_history_file(): success = False - clear_openbb_env_vars() + clear_openbb_env_vars(exceptions=["OPENBB_ENABLE_AUTHENTICATION"]) remove_log_handlers() reload_openbb_config_modules()