From 19e6b6195347db0764052803ceae394b51d88b39 Mon Sep 17 00:00:00 2001 From: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Date: Mon, 7 Aug 2023 06:16:39 -0700 Subject: [PATCH] Fixed the get routines to meet the new backend standard (#5271) * Added fixes * Fixed test * Unhid try except * Fixed everything else * Fixed typing * Fixed pylint * Fixed tests * Fixed --------- Co-authored-by: Andrew Co-authored-by: James Maslek --- openbb_terminal/account/account_controller.py | 38 +++++++++------- openbb_terminal/account/account_view.py | 43 +++++++++++++++---- openbb_terminal/core/session/hub_model.py | 17 ++++---- .../account/test_account_controller.py | 13 +++--- .../test_call_list.txt | 2 +- .../openbb_terminal/session/test_hub_model.py | 4 +- 6 files changed, 76 insertions(+), 41 deletions(-) diff --git a/openbb_terminal/account/account_controller.py b/openbb_terminal/account/account_controller.py index 459675080ecd..12b26bb88698 100644 --- a/openbb_terminal/account/account_controller.py +++ b/openbb_terminal/account/account_controller.py @@ -2,6 +2,7 @@ import logging from pathlib import Path from typing import Dict, List, Optional +from uuid import UUID from openbb_terminal.account.account_view import ( display_default_routines, @@ -55,12 +56,12 @@ def __init__(self, queue: Optional[List[str]] = None): """Constructor""" super().__init__(queue) self.LOCAL_ROUTINES: Dict[str, Path] = {} - self.REMOTE_CHOICES: List[str] = [] + self.REMOTE_CHOICES: Dict[str, UUID] = {} self.DEFAULT_ROUTINES: List[Dict[str, str]] = self.fetch_default_routines() - self.DEFAULT_CHOICES: List[str] = [ - r["name"] for r in self.DEFAULT_ROUTINES if "name" in r - ] + self.DEFAULT_CHOICES: Dict[str, None] = { + r["name"]: None for r in self.DEFAULT_ROUTINES if "name" in r + } if session and get_current_user().preferences.USE_PROMPT_TOOLKIT: self.choices: dict = self.choices_default @@ -75,7 +76,11 @@ def update_runtime_choices(self): {c: {} for c in self.LOCAL_ROUTINES} ) self.choices["download"]["--name"].update( - {c: {} for c in self.DEFAULT_CHOICES + self.REMOTE_CHOICES} + { + c: {} + for c in list(self.DEFAULT_CHOICES.keys()) + + list(self.REMOTE_CHOICES.keys()) + } ) self.choices["delete"]["--name"].update( {c: {} for c in self.REMOTE_CHOICES} @@ -253,7 +258,8 @@ def call_list(self, other_args: List[str]): ) df, page, pages = get_personal_routines_info(response) if not df.empty: - self.REMOTE_CHOICES += list(df["name"]) + temp_dict = dict(zip(df["name"], df["uuid"])) + self.REMOTE_CHOICES = {**self.REMOTE_CHOICES, **temp_dict} self.update_runtime_choices() display_personal_routines(df, page, pages) else: @@ -366,7 +372,8 @@ def call_upload(self, other_args: List[str]): console.print("[info]Aborted.[/info]") if response and response.status_code == 200: - self.REMOTE_CHOICES.append(name) + the_uuid = response.json()["uuid"] + self.REMOTE_CHOICES[name] = the_uuid self.update_runtime_choices() # store data in list with "personal/default" to identify data's routine type @@ -401,12 +408,13 @@ def call_download(self, other_args: List[str]): data = [] name = " ".join(ns_parser.name) # Personal routines - response = Hub.download_routine( - auth_header=get_current_user().profile.get_auth_header(), - name=name, - ) - if response and response.status_code == 200: - data = [response.json(), "personal"] + if name in self.REMOTE_CHOICES: + response = Hub.download_routine( + auth_header=get_current_user().profile.get_auth_header(), + uuid=self.REMOTE_CHOICES[name], + ) + if response and response.status_code == 200: + data = [response.json(), "personal"] # Default routine elif name in self.DEFAULT_CHOICES: data = [ @@ -490,14 +498,14 @@ def call_delete(self, other_args: List[str]): if i.lower() in ["y", "yes"]: response = Hub.delete_routine( auth_header=get_current_user().profile.get_auth_header(), - name=name, + uuid=self.REMOTE_CHOICES[name], ) if ( response and response.status_code == 200 and name in self.REMOTE_CHOICES ): - self.REMOTE_CHOICES.remove(name) + self.REMOTE_CHOICES.pop(name) self.update_runtime_choices() else: console.print("[info]Aborted.[/info]") diff --git a/openbb_terminal/account/account_view.py b/openbb_terminal/account/account_view.py index 45f2f93a4e2f..5dcbff376bfe 100644 --- a/openbb_terminal/account/account_view.py +++ b/openbb_terminal/account/account_view.py @@ -4,6 +4,34 @@ from openbb_terminal.rich_config import console +def clean_df(df: pd.DataFrame) -> pd.DataFrame: + """ + Cleans the dataframe before displaying it. + + Parameters + ---------- + df : pd.DataFrame + The dataframe to clean. + + Returns + ------- + pd.DataFrame + The cleaned dataframe. + """ + df["updated_date"] = pd.to_datetime(df["updated_date"]) + df["updated_date"] = df["updated_date"].dt.strftime("%Y-%m-%d %H:%M:%S") + df.replace(to_replace=[None], value="-", inplace=True) + to_rename = { + "name": "Name", + "description": "Description", + "version": "Version", + "updated_date": "Last Update", + } + df = df.rename(columns=to_rename) + df = df[["Name", "Description", "Version", "Last Update"]] + return df + + def display_personal_routines(df: pd.DataFrame, page: int, pages: int): """Display the routines. @@ -20,18 +48,17 @@ def display_personal_routines(df: pd.DataFrame, page: int, pages: int): title = f"Personal routines - page {page}" if pages: title += f" of {pages}" + df = clean_df(df) - df["updated_date"] = pd.to_datetime(df["updated_date"]) - df["updated_date"] = df["updated_date"].dt.strftime("%Y-%m-%d %H:%M:%S") - df.replace(to_replace=[None], value="-", inplace=True) print_rich_table( df=df, title=title, - headers=["Name", "Description", "Version", "Last update"], + headers=list(df.columns), show_index=True, index_name="#", ) - except Exception: + except Exception as exc: + print(exc) console.print("Failed to display personal routines.") @@ -44,9 +71,9 @@ def display_default_routines(df: pd.DataFrame): The default routines list. """ try: - df["date_updated"] = pd.to_datetime(df["date_updated"]) - df["date_updated"] = df["date_updated"].dt.strftime("%Y-%m-%d %H:%M:%S") - df.replace(to_replace=[None], value="-", inplace=True) + df = df.rename(columns={"date_updated": "updated_date"}) + print(df) + df = clean_df(df) print_rich_table( df=df, title="Default routines", diff --git a/openbb_terminal/core/session/hub_model.py b/openbb_terminal/core/session/hub_model.py index b180671d199a..f47412a46b07 100644 --- a/openbb_terminal/core/session/hub_model.py +++ b/openbb_terminal/core/session/hub_model.py @@ -1,6 +1,7 @@ import datetime import json from typing import Any, Dict, List, Literal, Optional +from uuid import UUID import requests from jose import jwt @@ -494,7 +495,7 @@ def upload_routine( def download_routine( auth_header: str, - name: str = "", + uuid: UUID, base_url=BASE_URL, timeout: int = TIMEOUT, ) -> Optional[requests.Response]: @@ -504,8 +505,8 @@ def download_routine( ---------- auth_header : str The authorization header, e.g. "Bearer ". - name : str - The name of the routine. + uuid : UUID + The uuid of the routine. base_url : str The base url, by default BASE_URL timeout : int @@ -519,7 +520,7 @@ def download_routine( try: response = requests.get( headers={"Authorization": auth_header}, - url=base_url + "terminal/script/" + name, + url=base_url + "terminal/script/" + uuid, timeout=timeout, ) if response.status_code == 404: @@ -540,7 +541,7 @@ def download_routine( def delete_routine( auth_header: str, - name: str = "", + uuid: UUID, base_url=BASE_URL, timeout: int = TIMEOUT, ) -> Optional[requests.Response]: @@ -550,8 +551,8 @@ def delete_routine( ---------- auth_header : str The authorization header, e.g. "Bearer ". - name : str - The name of the routine. + uuid : UUID + The uuid of the routine. base_url : str The base url, by default BASE_URL timeout : int @@ -565,7 +566,7 @@ def delete_routine( try: response = requests.delete( headers={"Authorization": auth_header}, - url=base_url + "terminal/script/" + name, + url=base_url + "terminal/script/" + uuid, timeout=timeout, ) if response.status_code == 200: diff --git a/tests/openbb_terminal/account/test_account_controller.py b/tests/openbb_terminal/account/test_account_controller.py index fcc1d7b9d748..6cbfb4edb3d2 100644 --- a/tests/openbb_terminal/account/test_account_controller.py +++ b/tests/openbb_terminal/account/test_account_controller.py @@ -50,18 +50,21 @@ "description": "abc", "version": "0.0.0", "updated_date": "2021-01-01", + "uuid": "dad15f7d-4757-4fa6-a1e4-b9d150282ea0", }, { "name": "script2", "description": "def", "version": "0.0.1", "updated_date": "2022-01-01", + "uuid": "4d87035e-33fa-4714-8b8f-9b699423595a", }, { "name": "script3", "description": "ghi", "version": "0.0.2", "updated_date": "2023-01-01", + "uuid": "4d87035e-33fa-4714-8b8f-9b699423595b", }, ], "total": 3, @@ -449,6 +452,7 @@ def test_call_upload(mocker, test_user): @pytest.mark.record_stdout def test_call_download(mocker, test_user): controller = account_controller.AccountController(queue=None) + controller.REMOTE_CHOICES = {"script1": "script1"} path_controller = "openbb_terminal.account.account_controller" mocker.patch( @@ -475,16 +479,11 @@ def test_call_download(mocker, test_user): return_value="path_to_file", ) - controller.call_download( - other_args=[ - "--name", - "script1", - ] - ) + controller.call_download(other_args=["--name", "script1"]) mock_download_routine.assert_called_once_with( auth_header="Bearer 123", - name="script1", + uuid="script1", ) mock_save_routine.assert_called_once_with( file_name="script1.openbb", routine=["do something", "personal"] diff --git a/tests/openbb_terminal/account/txt/test_account_controller/test_call_list.txt b/tests/openbb_terminal/account/txt/test_account_controller/test_call_list.txt index 84365dcc9179..4bb927001a97 100644 --- a/tests/openbb_terminal/account/txt/test_account_controller/test_call_list.txt +++ b/tests/openbb_terminal/account/txt/test_account_controller/test_call_list.txt @@ -1,4 +1,4 @@ - name description version updated_date + Name Description Version Last Update 1 scrip1 abc 0.0.0 2021-01-01 00:00:00 2 script2 def 0.0.1 2022-01-01 00:00:00 3 script3 ghi 0.0.2 2023-01-01 00:00:00 diff --git a/tests/openbb_terminal/session/test_hub_model.py b/tests/openbb_terminal/session/test_hub_model.py index 92e78bc694d7..25f620deef92 100644 --- a/tests/openbb_terminal/session/test_hub_model.py +++ b/tests/openbb_terminal/session/test_hub_model.py @@ -677,7 +677,7 @@ def test_download_routine(auth_header, name, base_url, timeout, status_code): return_value=mock_response, ) as requests_get_mock: result = hub_model.download_routine( - auth_header=auth_header, name=name, base_url=base_url, timeout=timeout + auth_header=auth_header, uuid=name, base_url=base_url, timeout=timeout ) assert result.status_code == mock_response.status_code @@ -721,7 +721,7 @@ def test_delete_routine(auth_header, name, base_url, timeout, status_code): return_value=mock_response, ) as requests_get_mock: result = hub_model.delete_routine( - auth_header=auth_header, name=name, base_url=base_url, timeout=timeout + auth_header=auth_header, uuid=name, base_url=base_url, timeout=timeout ) assert result.status_code == mock_response.status_code