Skip to content

Commit

Permalink
add public routines flag (#5236)
Browse files Browse the repository at this point in the history
* add public routines flag

* Spelling

* mypy

* Tests

---------

Co-authored-by: James Maslek <[email protected]>
  • Loading branch information
2 people authored and IgorWounds committed Jul 24, 2023
1 parent 96cada4 commit 1453885
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
9 changes: 9 additions & 0 deletions openbb_terminal/account/account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ def call_upload(self, other_args: List[str]):
default="",
nargs="+",
)
parser.add_argument(
"-p",
"--public",
dest="public",
action="store_true",
help="Whether the routine should be public or not",
default=False,
)
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()
Expand Down Expand Up @@ -341,6 +349,7 @@ def call_upload(self, other_args: List[str]):
"description": description,
"routine": routine,
"tags": tags,
"public": ns_parser.public,
}
response = Hub.upload_routine(**kwargs) # type: ignore

Expand Down
7 changes: 5 additions & 2 deletions openbb_terminal/core/session/hub_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,15 @@ def upload_config(
return None


# pylint: disable=too-many-arguments
def upload_routine(
auth_header: str,
name: str = "",
description: str = "",
routine: str = "",
override: bool = False,
tags: str = "",
public: bool = False,
base_url: str = BASE_URL,
timeout: int = TIMEOUT,
) -> Optional[requests.Response]:
Expand All @@ -420,6 +422,8 @@ def upload_routine(
Whether to override the routine if it already exists.
tags : str
The tags of the routine.
public : bool
Whether to make the routine public or not.
base_url : str
The base url, by default BASE_URL
timeout : int
Expand All @@ -430,15 +434,14 @@ 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,
"public": public,
}

try:
Expand Down
14 changes: 14 additions & 0 deletions openbb_terminal/parent_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@

SUPPORT_TYPE = ["bug", "suggestion", "question", "generic"]


# TODO: We should try to avoid these global variables
RECORD_SESSION = False
SESSION_RECORDED = list()
SESSION_RECORDED_NAME = ""
SESSION_RECORDED_DESCRIPTION = ""
SESSION_RECORDED_TAGS = ""
SESSION_RECORDED_PUBLIC = False


class BaseController(metaclass=ABCMeta):
Expand Down Expand Up @@ -910,6 +913,14 @@ def call_record(self, other_args) -> None:
default="",
nargs="+",
)
parser.add_argument(
"-p",
"--public",
dest="public",
action="store_true",
help="Whether the routine should be public or not",
default=False,
)
if other_args and "-" not in other_args[0][0]:
other_args.insert(0, "-n")
ns_parser = self.parse_simple_args(parser, other_args)
Expand All @@ -919,6 +930,7 @@ def call_record(self, other_args) -> None:
global SESSION_RECORDED_NAME
global SESSION_RECORDED_DESCRIPTION
global SESSION_RECORDED_TAGS
global SESSION_RECORDED_PUBLIC

SESSION_RECORDED_NAME = (
ns_parser.name
Expand All @@ -928,6 +940,7 @@ def call_record(self, other_args) -> None:

SESSION_RECORDED_DESCRIPTION = " ".join(ns_parser.description)
SESSION_RECORDED_TAGS = " ".join(ns_parser.tags) if ns_parser.tags else ""
SESSION_RECORDED_PUBLIC = ns_parser.public

console.print(
"[green]The session is successfully being recorded."
Expand Down Expand Up @@ -980,6 +993,7 @@ def call_stop(self, _) -> None:
"description": SESSION_RECORDED_DESCRIPTION,
"routine": routine,
"tags": SESSION_RECORDED_TAGS,
"public": SESSION_RECORDED_PUBLIC,
}
response = Hub.upload_routine(**kwargs) # type: ignore
if response is not None and response.status_code == 409:
Expand Down
1 change: 1 addition & 0 deletions tests/openbb_terminal/account/test_account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def test_call_upload(mocker, test_user):
description="abc",
routine="do something",
tags="stocks",
public=False,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ qtd,-
ytd,-
3m,-
6m,-
1y,"Adj Close NaN
Total NaN
dtype: float64"
1y,-
3y,"Adj Close NaN
Total NaN
dtype: float64"
Expand Down
41 changes: 37 additions & 4 deletions tests/openbb_terminal/session/test_hub_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,45 @@ def test_clear_user_configs_exception():


@pytest.mark.parametrize(
"auth_header, name, description, routine, override, base_url, timeout, status_code",
"auth_header, name, description, routine, override, tags, public, base_url, timeout, status_code",
[
("auth_header", "name", "description", "routine", True, "base_url", 10, 200),
("auth_header", "name", "description", "routine", False, "base_url", 10, 400),
(
"auth_header",
"name",
"description",
"routine",
True,
"TEST_TAG",
True,
"base_url",
10,
200,
),
(
"auth_header",
"name",
"description",
"routine",
False,
"TEST_TAG",
False,
"base_url",
10,
400,
),
],
)
def test_upload_routine(
auth_header, name, description, routine, override, base_url, timeout, status_code
auth_header,
name,
description,
routine,
override,
tags,
public,
base_url,
timeout,
status_code,
):
mock_response = MagicMock(spec=requests.Response)
mock_response.status_code = status_code
Expand All @@ -555,6 +586,8 @@ def test_upload_routine(
name=name,
description=description,
routine=routine,
tags=tags,
public=public,
override=override,
base_url=base_url,
timeout=timeout,
Expand Down

0 comments on commit 1453885

Please sign in to comment.