Skip to content

Commit

Permalink
Choice descriptions (#76)
Browse files Browse the repository at this point in the history
* Changed choices return type to be nested dictionary mapping rather than list

* Updated CLI to output more detailed choices format

* Updated CHOICES_DATA in tests to reflect new format
  • Loading branch information
tombch authored Apr 24, 2024
1 parent d3fbc6a commit a1c4325
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
23 changes: 20 additions & 3 deletions onyx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ def fields(self, project: str) -> Dict[str, Any]:
return response.json()["data"]

@onyx_errors
def choices(self, project: str, field: str) -> List[str]:
def choices(self, project: str, field: str) -> Dict[str, Dict[str, Any]]:
"""
View choices for a field.
Expand All @@ -1186,7 +1186,7 @@ def choices(self, project: str, field: str) -> List[str]:
field: Choice field on the project.
Returns:
List of choices for the field.
Dictionary mapping choices to information about the choice.
Examples:
```python
Expand All @@ -1203,7 +1203,24 @@ def choices(self, project: str, field: str) -> List[str]:
```
```python
>>> choices
["ENG", "WALES", "SCOT", "NI"]
{
"ENG": {
"description": "England",
"is_active" : True,
},
"WALES": {
"description": "Wales",
"is_active" : True,
},
"SCOT": {
"description": "Scotland",
"is_active" : True,
},
"NI": {
"description": "Northern Ireland",
"is_active" : True,
},
}
```
"""

Expand Down
25 changes: 22 additions & 3 deletions onyx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ class Actions(enum.Enum):
DELETE = "[bold red]delete[/]"


class ActiveStatus(enum.Enum):
ACTIVE = "[bold green]active[/]"
INACTIVE = "[bold red]inactive[/]"


def format_action(action: str) -> str:
"""
Format an action and apply its colour.
Expand Down Expand Up @@ -627,9 +632,23 @@ def choices(
table = Table(
show_lines=True,
)
table.add_column("Field")
table.add_column("Values")
table.add_row(field, ", ".join(choices))
table.add_column("Choice")
table.add_column("Description")
table.add_column("Status")
for choice, choice_info in choices.items():
active_status = choice_info.get("is_active")
if active_status == True:
active_status = ActiveStatus.ACTIVE.value
elif active_status == False:
active_status = ActiveStatus.INACTIVE.value
else:
active_status = ""

table.add_row(
choice,
choice_info.get("description", ""),
active_status,
)
console.print(table)
else:
typer.echo(json_dump_pretty(choices))
Expand Down
22 changes: 18 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,24 @@
"status": "success",
"code": 200,
"data": [
"England",
"N. Ireland",
"Scotland",
"Wales",
{
"eng": {
"description": "England",
"is_active": True,
},
"ni": {
"description": "N. Ireland",
"is_active": True,
},
"scot": {
"description": "Scotland",
"is_active": True,
},
"wales": {
"description": "Wales",
"is_active": True,
},
}
],
}
CLIMB_ID = "C-0123456789"
Expand Down

0 comments on commit a1c4325

Please sign in to comment.