Skip to content

Commit

Permalink
feat: REST API new select columns query param (#2242)
Browse files Browse the repository at this point in the history
* feat: REST API new select columns

* feat: REST API new select columns

* fix lint
  • Loading branch information
dpgaspar authored May 20, 2024
1 parent 83ad6e9 commit ec51363
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
22 changes: 19 additions & 3 deletions flask_appbuilder/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
API_PERMISSIONS_RIS_KEY,
API_RESULT_RES_KEY,
API_SELECT_COLUMNS_RIS_KEY,
API_SELECT_SEL_COLUMNS_RIS_KEY,
API_SHOW_COLUMNS_RES_KEY,
API_SHOW_COLUMNS_RIS_KEY,
API_SHOW_TITLE_RES_KEY,
Expand Down Expand Up @@ -1572,8 +1573,23 @@ def get_list_headless(self, **kwargs: Any) -> Response:
response = dict()
args = kwargs.get("rison", {})
# handle select columns
select_cols = args.get(API_SELECT_COLUMNS_RIS_KEY, [])
pruned_select_cols = [col for col in select_cols if col in self.list_columns]
output_select_cols = args.get(API_SELECT_COLUMNS_RIS_KEY, [])
select_cols = args.get(API_SELECT_SEL_COLUMNS_RIS_KEY, [])
if select_cols and output_select_cols:
return self.response_400(message="Cannot use both select and sel columns")
list_select_columns = self.list_select_columns
pruned_select_cols = []
if output_select_cols:
pruned_select_cols = [
col for col in output_select_cols if col in self.list_columns
]
if select_cols:
pruned_select_cols = [
col for col in select_cols if col in self.list_columns
]
list_select_columns = [
col for col in select_cols if col in self.list_select_columns
]
# map decorated metadata
self.set_response_key_mappings(
response,
Expand Down Expand Up @@ -1606,7 +1622,7 @@ def get_list_headless(self, **kwargs: Any) -> Response:
order_direction,
page=page_index,
page_size=page_size,
select_columns=self.list_select_columns,
select_columns=list_select_columns,
outer_default_load=self.list_outer_default_load,
)
pks = self.datamodel.get_keys(lst)
Expand Down
2 changes: 2 additions & 0 deletions flask_appbuilder/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
API_PERMISSIONS_RIS_KEY,
API_SELECT_COLUMNS_RIS_KEY,
API_SELECT_KEYS_RIS_KEY,
API_SELECT_SEL_COLUMNS_RIS_KEY,
API_SHOW_COLUMNS_RIS_KEY,
API_SHOW_TITLE_RIS_KEY,
)
Expand Down Expand Up @@ -70,6 +71,7 @@ def load(self, data, *, instance=None, **kwargs):
},
},
API_SELECT_COLUMNS_RIS_KEY: {"type": "array", "items": {"type": "string"}},
API_SELECT_SEL_COLUMNS_RIS_KEY: {"type": "array", "items": {"type": "string"}},
API_ORDER_COLUMN_RIS_KEY: {"type": "string"},
API_ORDER_DIRECTION_RIS_KEY: {"type": "string", "enum": ["asc", "desc"]},
API_PAGE_INDEX_RIS_KEY: {"type": "integer"},
Expand Down
1 change: 1 addition & 0 deletions flask_appbuilder/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
API_FILTERS_RIS_KEY = "filters"
API_PERMISSIONS_RIS_KEY = "permissions"
API_SELECT_COLUMNS_RIS_KEY = "columns"
API_SELECT_SEL_COLUMNS_RIS_KEY = "select_columns"
API_SELECT_KEYS_RIS_KEY = "keys"
API_ORDER_COLUMN_RIS_KEY = "order_column"
API_ORDER_DIRECTION_RIS_KEY = "order_direction"
Expand Down
33 changes: 31 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
API_SECURITY_REFRESH_TOKEN_KEY,
API_SELECT_COLUMNS_RIS_KEY,
API_SELECT_KEYS_RIS_KEY,
API_SELECT_SEL_COLUMNS_RIS_KEY,
API_SHOW_COLUMNS_RIS_KEY,
API_SHOW_TITLE_RIS_KEY,
API_URI_RIS_KEY,
Expand Down Expand Up @@ -870,7 +871,7 @@ def assert_get_item(self, rv, data, value):
)
self.assertEqual(rv.status_code, 200)

def test_get_item_select_cols(self):
def test_get_item_choose_cols(self):
"""
REST Api: Test get item with select columns
"""
Expand Down Expand Up @@ -1854,7 +1855,7 @@ def test_get_info_custom_search_filters(self):
{"name": "Custom Filter", "operator": "custom_filter"}, field_string_filters
)

def test_get_list_select_cols(self):
def test_get_list_choose_cols(self):
"""
REST Api: Test get list with select columns
"""
Expand Down Expand Up @@ -1882,6 +1883,34 @@ def test_get_list_select_cols(self):
self.assertEqual(data[API_LIST_COLUMNS_RES_KEY], ["field_integer"])
self.assertEqual(rv.status_code, 200)

def test_get_list_choose_select_cols(self):
"""
REST Api: Test get list with select columns
"""
client = self.app.test_client()
token = self.login(client, USERNAME_ADMIN, PASSWORD_ADMIN)

argument = {
API_SELECT_SEL_COLUMNS_RIS_KEY: ["field_integer"],
"order_column": "field_integer",
"order_direction": "asc",
}

uri = f"api/v1/model1api/?{API_URI_RIS_KEY}={prison.dumps(argument)}"
with model1_data(self.appbuilder.session, 5):
rv = self.auth_client_get(client, token, uri)
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(data[API_RESULT_RES_KEY][0], {"field_integer": 0})
self.assertEqual(
data[API_LABEL_COLUMNS_RES_KEY], {"field_integer": "Field Integer"}
)
self.assertEqual(
data[API_DESCRIPTION_COLUMNS_RES_KEY],
{"field_integer": "Field Integer"},
)
self.assertEqual(data[API_LIST_COLUMNS_RES_KEY], ["field_integer"])
self.assertEqual(rv.status_code, 200)

def test_get_list_select_meta_data(self):
"""
REST Api: Test get list select meta data
Expand Down

0 comments on commit ec51363

Please sign in to comment.