4040
4141from airflow .api_connexion .endpoints .task_instance_endpoint import _convert_ti_states
4242from airflow .jobs .job import Job
43- from airflow .models import Base , Connection
43+ from airflow .models import Base
4444from airflow .models .asset import AssetEvent , AssetModel , DagScheduleAssetReference , TaskOutletAssetReference
4545from airflow .models .dag import DagModel , DagTag
4646from airflow .models .dagrun import DagRun
4747from airflow .models .dagwarning import DagWarning , DagWarningType
48- from airflow .models .errors import ParseImportError
4948from airflow .models .taskinstance import TaskInstance
5049from airflow .typing_compat import Self
5150from airflow .utils import timezone
@@ -218,16 +217,8 @@ def depends(self, dag_display_name_pattern: str | None = None) -> _DagDisplayNam
218217class SortParam (BaseParam [str ]):
219218 """Order result by the attribute."""
220219
221- attr_mapping = {
222- "last_run_state" : DagRun .state ,
223- "last_run_start_date" : DagRun .start_date ,
224- "connection_id" : Connection .conn_id ,
225- "import_error_id" : ParseImportError .id ,
226- "dag_run_id" : DagRun .run_id ,
227- }
228-
229220 def __init__ (
230- self , allowed_attrs : list [str ], model : Base , to_replace : dict [str , str ] | None = None
221+ self , allowed_attrs : list [str ], model : Base , to_replace : dict [str , str | Column ] | None = None
231222 ) -> None :
232223 super ().__init__ ()
233224 self .allowed_attrs = allowed_attrs
@@ -242,19 +233,22 @@ def to_orm(self, select: Select) -> Select:
242233 self .value = self .get_primary_key_string ()
243234
244235 lstriped_orderby = self .value .lstrip ("-" )
236+ column : Column | None = None
245237 if self .to_replace :
246- lstriped_orderby = self .to_replace .get (lstriped_orderby , lstriped_orderby )
238+ replacement = self .to_replace .get (lstriped_orderby , lstriped_orderby )
239+ if isinstance (replacement , str ):
240+ lstriped_orderby = replacement
241+ else :
242+ column = replacement
247243
248- if self .allowed_attrs and lstriped_orderby not in self .allowed_attrs :
244+ if ( self .allowed_attrs and lstriped_orderby not in self .allowed_attrs ) and column is None :
249245 raise HTTPException (
250246 400 ,
251247 f"Ordering with '{ lstriped_orderby } ' is disallowed or "
252248 f"the attribute does not exist on the model" ,
253249 )
254-
255- column : Column = self .attr_mapping .get (lstriped_orderby , None ) or getattr (
256- self .model , lstriped_orderby
257- )
250+ if column is None :
251+ column = getattr (self .model , lstriped_orderby )
258252
259253 # MySQL does not support `nullslast`, and True/False ordering depends on the
260254 # database implementation.
0 commit comments