From 105557e8985f8431004e8f86c900460b4e2a7003 Mon Sep 17 00:00:00 2001 From: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Date: Wed, 21 Jun 2023 13:31:00 +0100 Subject: [PATCH] Feature/routine tags (#5154) * no guest if installer * prevent guest if logout from account * this not true * rename reloop var * ask for email again if no email * add tags to routines upload * Update constants.py * Update terminal_controller.py * Update terminal_controller.py * black * ruff * black * Update constants.py * point at .co * mypy * sneak in askobb timeout * fix test --------- Co-authored-by: James Maslek --- openbb_terminal/account/account_controller.py | 37 +++++++---- openbb_terminal/core/session/constants.py | 9 +++ openbb_terminal/core/session/hub_model.py | 5 ++ openbb_terminal/helper_funcs.py | 2 +- openbb_terminal/parent_classes.py | 66 +++++++++++++------ .../account/test_account_controller.py | 3 + 6 files changed, 88 insertions(+), 34 deletions(-) diff --git a/openbb_terminal/account/account_controller.py b/openbb_terminal/account/account_controller.py index 826aa28d5d5..1400affd914 100644 --- a/openbb_terminal/account/account_controller.py +++ b/openbb_terminal/account/account_controller.py @@ -9,6 +9,7 @@ from openbb_terminal.account.account_view import ( ) from openbb_terminal.account.show_prompt import set_show_prompt from openbb_terminal.core.session import hub_model as Hub +from openbb_terminal.core.session.constants import SCRIPT_TAGS from openbb_terminal.core.session.current_user import ( get_current_user, is_local, @@ -63,6 +64,7 @@ class AccountController(BaseController): if session and get_current_user().preferences.USE_PROMPT_TOOLKIT: self.choices: dict = self.choices_default + self.choices["upload"]["--tags"] = {c: None for c in SCRIPT_TAGS} self.completer = NestedCompleter.from_nested_dict(self.choices) def update_runtime_choices(self): @@ -303,6 +305,15 @@ class AccountController(BaseController): help="The name of the routine.", nargs="+", ) + parser.add_argument( + "-t", + "--tags", + type=str, + dest="tags", + help="The tags of the routine", + default="", + nargs="+", + ) ns_parser = self.parse_known_args_and_warn(parser, other_args) if is_local() and "-h" not in other_args and "--help" not in other_args: print_guest_block_msg() @@ -320,14 +331,19 @@ class AccountController(BaseController): ] ) + tags = " ".join(ns_parser.tags) if ns_parser.tags else "" + current_user = get_current_user() - response = Hub.upload_routine( - auth_header=current_user.profile.get_auth_header(), - name=name, - description=description, - routine=routine, - ) + kwargs = { + "auth_header": current_user.profile.get_auth_header(), + "name": name, + "description": description, + "routine": routine, + "tags": tags, + } + response = Hub.upload_routine(**kwargs) # type: ignore + if response is not None and response.status_code == 409: i = console.input( "A routine with the same name already exists, " @@ -335,13 +351,8 @@ class AccountController(BaseController): ) console.print("") if i.lower() in ["y", "yes"]: - response = Hub.upload_routine( - auth_header=current_user.profile.get_auth_header(), - name=name, - description=description, - routine=routine, - override=True, - ) + kwargs["override"] = True # type: ignore + response = Hub.upload_routine(**kwargs) # type: ignore else: console.print("[info]Aborted.[/info]") diff --git a/openbb_terminal/core/session/constants.py b/openbb_terminal/core/session/constants.py index cfb47f10b38..d2a05d06840 100644 --- a/openbb_terminal/core/session/constants.py +++ b/openbb_terminal/core/session/constants.py @@ -14,3 +14,12 @@ DEFAULT_ROUTINES_URL = "https://openbb-cms.directus.app/items/Routines" TIMEOUT = 30 CONNECTION_ERROR_MSG = "[red]Connection error.[/red]" CONNECTION_TIMEOUT_MSG = "[red]Connection timeout.[/red]" + +SCRIPT_TAGS = [ + "stocks", + "due diligence", + "price targets", + "crypto", + "earnings", + "economy", +] diff --git a/openbb_terminal/core/session/hub_model.py b/openbb_terminal/core/session/hub_model.py index 1167f9b551a..84e36213baa 100644 --- a/openbb_terminal/core/session/hub_model.py +++ b/openbb_terminal/core/session/hub_model.py @@ -402,6 +402,7 @@ def upload_routine( description: str = "", routine: str = "", override: bool = False, + tags: str = "", base_url: str = BASE_URL, timeout: int = TIMEOUT, ) -> Optional[requests.Response]: @@ -417,6 +418,8 @@ def upload_routine( The routine. override : bool Whether to override the routine if it already exists. + tags : str + The tags of the routine. base_url : str The base url, by default BASE_URL timeout : int @@ -427,11 +430,13 @@ def upload_routine( Optional[requests.Response] The response from the post request. """ + data = { "name": name, "description": description, "script": routine, "override": override, + "tags": tags, "version": get_current_system().VERSION, "public": False, } diff --git a/openbb_terminal/helper_funcs.py b/openbb_terminal/helper_funcs.py index c1e07db5bd6..6a591e3c145 100644 --- a/openbb_terminal/helper_funcs.py +++ b/openbb_terminal/helper_funcs.py @@ -2265,7 +2265,7 @@ def query_LLM_remote(query_text: str): data = {"prompt": query_text, "accessToken": get_current_user().profile.token} - ask_obbrequest_data = request(url, method="POST", json=data).json() + ask_obbrequest_data = request(url, method="POST", json=data, timeout=10).json() if "error" in ask_obbrequest_data: console.print(f"[red]{ask_obbrequest_data['error']}[/red]") diff --git a/openbb_terminal/parent_classes.py b/openbb_terminal/parent_classes.py index 8c9b4adf0fb..4fbf78b7b3a 100644 --- a/openbb_terminal/parent_classes.py +++ b/openbb_terminal/parent_classes.py @@ -81,6 +81,8 @@ SUPPORT_TYPE = ["bug", "suggestion", "question", "generic"] RECORD_SESSION = False SESSION_RECORDED = list() SESSION_RECORDED_NAME = "" +SESSION_RECORDED_DESCRIPTION = "" +SESSION_RECORDED_TAGS = "" class BaseController(metaclass=ABCMeta): @@ -762,25 +764,50 @@ class BaseController(metaclass=ABCMeta): description="Start recording session into .openbb routine file", ) parser.add_argument( - "-r", - "--routine", + "-n", + "--name", action="store", - dest="routine_name", + dest="name", type=str, default=datetime.now().strftime("%Y%m%d_%H%M%S_routine.openbb"), help="Routine file name to be saved.", ) + parser.add_argument( + "-d", + "--description", + type=str, + dest="description", + help="The description of the routine", + default="", + nargs="+", + ) + parser.add_argument( + "-t", + "--tags", + type=str, + dest="tags", + help="The tags of the routine", + default="", + nargs="+", + ) if other_args and "-" not in other_args[0][0]: - other_args.insert(0, "-r") + other_args.insert(0, "-n") ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: - global SESSION_RECORDED_NAME global RECORD_SESSION - if ".openbb" in ns_parser.routine_name: - SESSION_RECORDED_NAME = ns_parser.routine_name - else: - SESSION_RECORDED_NAME = ns_parser.routine_name + ".openbb" + global SESSION_RECORDED_NAME + global SESSION_RECORDED_DESCRIPTION + global SESSION_RECORDED_TAGS + + SESSION_RECORDED_NAME = ( + ns_parser.name + if ".openbb" in ns_parser.name + else ns_parser.name + ".openbb" + ) + + SESSION_RECORDED_DESCRIPTION = " ".join(ns_parser.description) + SESSION_RECORDED_TAGS = " ".join(ns_parser.tags) if ns_parser.tags else "" console.print( "[green]The session is successfully being recorded." @@ -827,11 +854,14 @@ class BaseController(metaclass=ABCMeta): routine = read_routine(file_name=routine_file) if routine is not None: name = SESSION_RECORDED_NAME.split(sep=".openbb", maxsplit=-1)[0] - response = Hub.upload_routine( - auth_header=current_user.profile.get_auth_header(), - name=name, - routine=routine, - ) + kwargs = { + "auth_header": current_user.profile.get_auth_header(), + "name": name, + "description": SESSION_RECORDED_DESCRIPTION, + "routine": routine, + "tags": SESSION_RECORDED_TAGS, + } + response = Hub.upload_routine(**kwargs) # type: ignore if response is not None and response.status_code == 409: i = console.input( "A routine with the same name already exists, " @@ -839,12 +869,8 @@ class BaseController(metaclass=ABCMeta): ) console.print("") if i.lower() in ["y", "yes"]: - response = Hub.upload_routine( - auth_header=current_user.profile.get_auth_header(), - name=name, - routine=routine, - override=True, - ) + kwargs["override"] = True # type: ignore + response = Hub.upload_routine(**kwargs) # type: ignore else: console.print("[info]Aborted.[/info]") diff --git a/tests/openbb_terminal/account/test_account_controller.py b/tests/openbb_terminal/account/test_account_controller.py index a3495117fdd..652ddd316f6 100644 --- a/tests/openbb_terminal/account/test_account_controller.py +++ b/tests/openbb_terminal/account/test_account_controller.py @@ -430,6 +430,8 @@ def test_call_upload(mocker, test_user): "abc", "--name", "script1", + "--tags", + "stocks", ] ) @@ -439,6 +441,7 @@ def test_call_upload(mocker, test_user): name="script1", description="abc", routine="do something", + tags="stocks", )