uranusjr commented on a change in pull request #14895:
URL: https://github.com/apache/airflow/pull/14895#discussion_r600334911



##########
File path: airflow/api_connexion/parameters.py
##########
@@ -86,3 +87,32 @@ def wrapped_function(*args, **kwargs):
         return cast(T, wrapped_function)
 
     return format_parameters_decorator
+
+
+def apply_sorting(model, query, order_by, to_replace=None, allowed_attrs=None):
+    """Apply sorting to query"""
+    lstriped_orderby = order_by.lstrip('-')
+    if allowed_attrs and lstriped_orderby not in allowed_attrs:
+        modelname = model.__tablename__.capitalize()
+        model_mapping = {
+            "Ab_user": 'User',
+            "Slot_pool": "Pool",
+            "Dag_run": "DagRun",
+            "Dag": "DagModel",
+            "Ab_role": "Role",
+            "Import_error": "ImportError",
+        }
+        if model_mapping.get(modelname, None):
+            modelname = model_mapping[modelname]

Review comment:
       Yeah, probably not a good idea to put them on models directly. I’m 
thinking a separate definition like how Flask-WTForm classes are separate to 
the ORM model (it doesn’t even need to be backed by a model).
   
   ```python
   class ConnectionFilter:
       replacements = {"connection_id": "conn_id"}
       allowed_attrs = {'connection_id', 'conn_type', 'description', 'host', 
'port', 'id'}
   
       @classmethod
       def order_by(cls, query: Query, order_by: str) -> Query:
           ...
   
   def get_connections(session, limit, offset=0, order_by="id"):
       ...
       query = ConnectionFilter.order_by(session.query(Connection), order_by)
       ...
   ```
   
   Would this feel like an overkill?




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to