Skip to content

Commit 9f3beb9

Browse files
authored
fix(get_records_ids): exclude filters when getting the ids if all_rec… (#332)
1 parent f77cb1e commit 9f3beb9

2 files changed

Lines changed: 85 additions & 3 deletions

File tree

src/agent_toolkit/forestadmin/agent_toolkit/resources/collections/filter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,16 @@ def _all_records_subset_query(request: Request) -> Dict[str, Any]:
7676
def _subset_or_query(request: Request, key: str) -> Optional[str]:
7777
res = None
7878
if request.body:
79-
sub_res = _all_records_subset_query(request).get(key)
80-
if sub_res:
81-
res = str(sub_res)
79+
# Only use all_records_subset_query when all_records is true
80+
try:
81+
attributes = request.body.get("data", {}).get("attributes", {})
82+
if attributes.get("all_records", False):
83+
sub_res = _all_records_subset_query(request).get(key)
84+
if sub_res:
85+
res = str(sub_res)
86+
except AttributeError:
87+
# data may be a list
88+
pass
8289
if not res and request.query:
8390
res = request.query.get(key)
8491
if res:

src/agent_toolkit/tests/resources/actions/test_resources.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,3 +1034,78 @@ def test_execute_should_get_all_form_fields_included_hidden(self):
10341034
response.body,
10351035
'{"success": "hidden_value"}',
10361036
)
1037+
1038+
def test_execute_should_ignore_all_records_subset_query_filters_when_all_records_is_false(self):
1039+
"""Test that when all_records is false, filters from all_records_subset_query are ignored."""
1040+
collected_filter = None
1041+
1042+
async def execute_with_filter_capture(ctx, rb):
1043+
nonlocal collected_filter
1044+
collected_filter = ctx.filter
1045+
return rb.success("done")
1046+
1047+
self.decorated_collection_book.add_action(
1048+
"test_bulk_action",
1049+
{
1050+
"scope": ActionsScope.BULK,
1051+
"execute": execute_with_filter_capture,
1052+
},
1053+
)
1054+
1055+
body_params = {
1056+
"data": {
1057+
"attributes": {
1058+
"values": {},
1059+
"ids": ["1", "2"],
1060+
"collection_name": "Book",
1061+
"parent_collection_name": None,
1062+
"parent_collection_id": None,
1063+
"parent_association_name": None,
1064+
"all_records": False,
1065+
"all_records_subset_query": {
1066+
"fields[Book]": "id,name,cost",
1067+
"page[number]": 1,
1068+
"page[size]": 15,
1069+
"sort": "-id",
1070+
"timezone": "Europe/Paris",
1071+
# This filter should be IGNORED when all_records is false
1072+
"filters": '{"field":"id","operator":"greater_than","value":"100"}',
1073+
},
1074+
"all_records_ids_excluded": [],
1075+
"smart_action_id": "Book-test_bulk_action",
1076+
"signed_approval_request": None,
1077+
},
1078+
"type": "custom-action-requests",
1079+
}
1080+
}
1081+
1082+
request = ActionRequest(
1083+
method=RequestMethod.POST,
1084+
action_name="test_bulk_action",
1085+
collection=self.decorated_collection_book,
1086+
body=body_params,
1087+
query={
1088+
"timezone": "Europe/Paris",
1089+
"collection_name": "Book",
1090+
"action_name": 0,
1091+
"slug": "test_bulk_action",
1092+
},
1093+
headers={},
1094+
user=self.mocked_caller,
1095+
client_ip="127.0.0.1",
1096+
)
1097+
1098+
response = self.loop.run_until_complete(self.action_resource.execute(request))
1099+
self.assertEqual(response.status, 200)
1100+
1101+
# The filter should NOT contain the "greater_than" condition from all_records_subset_query
1102+
# It should only contain the ID match condition for ids [1, 2]
1103+
self.assertIsNotNone(collected_filter)
1104+
if collected_filter.condition_tree:
1105+
filter_str = str(collected_filter.condition_tree)
1106+
# Should not contain the greater_than filter from all_records_subset_query
1107+
self.assertNotIn("greater_than", filter_str.lower())
1108+
self.assertNotIn(">", filter_str)
1109+
# Should contain the selected IDs (1 and 2)
1110+
self.assertIn("1", filter_str)
1111+
self.assertIn("2", filter_str)

0 commit comments

Comments
 (0)