99from asgiref .sync import sync_to_async
1010
1111from fastadmin .api .exceptions import AdminApiException
12- from fastadmin .api .helpers import get_user_id_from_session_id , sanitize
12+ from fastadmin .api .helpers import get_user_id_from_session_id , sanitize_filter_key , sanitize_filter_value
1313from fastadmin .api .schemas import (
1414 ActionInputSchema ,
1515 ChangePasswordInputSchema ,
@@ -88,8 +88,6 @@ async def list(
8888 if not admin_model :
8989 raise AdminApiException (404 , detail = f"{ model } model is not registered." )
9090
91- filters = {k : sanitize (v ) for k , v in filters .items () if k not in ("search" , "sort_by" , "offset" , "limit" )}
92-
9391 # validations
9492 fields = admin_model .get_fields_for_serialize ()
9593
@@ -98,11 +96,19 @@ async def list(
9896 if field not in fields :
9997 raise AdminApiException (422 , detail = f"Search by { field } is not allowed" )
10098
99+ exclude_filter_fields = ("search" , "sort_by" , "offset" , "limit" )
101100 if filters :
102- for filter_condition in filters .keys ():
103- field = filter_condition .split ("__" , 1 )[0 ]
101+ for k in filters .keys ():
102+ if k in exclude_filter_fields :
103+ continue
104+ field = k .split ("__" , 1 )[0 ]
104105 if field not in fields :
105- raise AdminApiException (422 , detail = f"Filter by { filter_condition } is not allowed" )
106+ raise AdminApiException (422 , detail = f"Filter by { k } is not allowed" )
107+ filters = {
108+ sanitize_filter_key (k , admin_model .get_model_fields_with_widget_types ()): sanitize_filter_value (v )
109+ for k , v in filters .items ()
110+ if k not in exclude_filter_fields
111+ }
106112
107113 if sort_by :
108114 if sort_by .strip ("-" ) not in fields :
@@ -169,7 +175,7 @@ async def change_password(
169175 if not current_user_id :
170176 raise AdminApiException (401 , detail = "User is not authenticated." )
171177
172- admin_model = get_admin_or_admin_inline_model (settings .ADMIN_USER_MODEL )
178+ admin_model = get_admin_model (settings .ADMIN_USER_MODEL )
173179 if not admin_model :
174180 raise AdminApiException (404 , detail = f"{ settings .ADMIN_USER_MODEL } model is not registered." )
175181
@@ -223,7 +229,35 @@ async def export(
223229 if not admin_model :
224230 raise AdminApiException (404 , detail = f"{ model } model is not registered." )
225231
226- filters = {k : sanitize (v ) for k , v in filters .items () if k not in ("search" , "sort_by" , "offset" , "limit" )}
232+ # validations
233+ fields = admin_model .get_fields_for_serialize ()
234+
235+ if search and admin_model .search_fields :
236+ for field in admin_model .search_fields :
237+ if field not in fields :
238+ raise AdminApiException (422 , detail = f"Search by { field } is not allowed" )
239+
240+ exclude_filter_fields = ("search" , "sort_by" , "offset" , "limit" )
241+ if filters :
242+ for k in filters .keys ():
243+ if k in exclude_filter_fields :
244+ continue
245+ field = k .split ("__" , 1 )[0 ]
246+ if field not in fields :
247+ raise AdminApiException (422 , detail = f"Filter by { k } is not allowed" )
248+ filters = {
249+ sanitize_filter_key (k , admin_model .get_model_fields_with_widget_types ()): sanitize_filter_value (v )
250+ for k , v in filters .items ()
251+ if k not in exclude_filter_fields
252+ }
253+
254+ if sort_by :
255+ if sort_by .strip ("-" ) not in fields :
256+ raise AdminApiException (422 , detail = f"Sort by { sort_by } is not allowed" )
257+ elif admin_model .ordering :
258+ for ordering_field in admin_model .ordering :
259+ if ordering_field .strip ("-" ) not in fields :
260+ raise AdminApiException (422 , detail = f"Sort by { ordering_field } is not allowed" )
227261
228262 content_type = "text/plain"
229263 file_name = f"{ model } .txt"
0 commit comments