pierrejeambrun commented on code in PR #42782:
URL: https://github.com/apache/airflow/pull/42782#discussion_r1793614269
##########
airflow/api_fastapi/parameters.py:
##########
@@ -181,11 +193,53 @@ def to_orm(self, select: Select) -> Select:
select = select.order_by(None)
if self.value[0] == "-":
- return select.order_by(nullscheck, column.desc(),
DagModel.dag_id.desc())
+ return select.order_by(nullscheck, column.desc(),
self.order_column.desc())
else:
- return select.order_by(nullscheck, column.asc(),
DagModel.dag_id.asc())
+ return select.order_by(nullscheck, column.asc(),
self.order_column.asc())
+
+ def depends(self, order_by: str) -> SortParam:
+ return self.set_value(order_by)
+
+
+class SortConnectionParam(SortParam):
+ """Order Connection by the attribute."""
- def depends(self, order_by: str = "dag_id") -> SortParam:
+ def __init__(self, allowed_attrs: list[str]) -> None:
+ super().__init__(
+ allowed_attrs=allowed_attrs,
+ attr_mapping={
+ "connection_id": Connection.conn_id,
+ "conn_type": Connection.conn_type,
+ "description": Connection.description,
+ "host": Connection.host,
+ "port": Connection.port,
+ "id": Connection.id,
+ },
+ model=Connection,
+ order_column=Connection.conn_id,
+ to_replace={"connection_id": "conn_id"},
+ )
+
Review Comment:
Also I am wondering if we need to subclass it. I think that just avoid a big
'attr_mapping', but actually none of them conflicts, so we might as well have
that and not bother with extending multiple subclasses, as many as `Model`
there are.
Because we will have a lot of them.
You can just give `model` to the SortConnectionParam extra constructor to
specify the default 'fallback' model if not in the 'mapping' that should clear
things out.
So we end up with:
```
SortParam(
["dag_id", "dag_display_name", "next_dagrun",
"last_run_state", "last_run_start_date"],
DagModel
)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]