Skip to content

Commit

Permalink
Fixed the get routines to meet the new backend standard (OpenBB-finan…
Browse files Browse the repository at this point in the history
…ce#5271)

* Added fixes

* Fixed test

* Unhid try except

* Fixed everything else

* Fixed typing

* Fixed pylint

* Fixed tests

* Fixed

---------

Co-authored-by: Andrew <andrew.kenreich@gmail.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
  • Loading branch information
3 people authored Aug 7, 2023
1 parent 2798508 commit 19e6b61
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 41 deletions.
38 changes: 23 additions & 15 deletions openbb_terminal/account/account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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]")
Expand Down
43 changes: 35 additions & 8 deletions openbb_terminal/account/account_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.")


Expand All @@ -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",
Expand Down
17 changes: 9 additions & 8 deletions openbb_terminal/core/session/hub_model.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]:
Expand All @@ -504,8 +505,8 @@ def download_routine(
----------
auth_header : str
The authorization header, e.g. "Bearer <token>".
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
Expand All @@ -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:
Expand All @@ -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]:
Expand All @@ -550,8 +551,8 @@ def delete_routine(
----------
auth_header : str
The authorization header, e.g. "Bearer <token>".
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
Expand All @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions tests/openbb_terminal/account/test_account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/openbb_terminal/session/test_hub_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19e6b61

Please sign in to comment.