You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2022/07/01 17:14:11 UTC

[airflow] branch main updated: Move Flask hook registration to end of file (#24776)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new e8b49d7ebe Move Flask hook registration to end of file (#24776)
e8b49d7ebe is described below

commit e8b49d7ebedf7818ff1ba9c9b8595fc6dc13ba67
Author: Tzu-ping Chung <ur...@gmail.com>
AuthorDate: Sat Jul 2 01:14:05 2022 +0800

    Move Flask hook registration to end of file (#24776)
    
    Pylance is too clever here. It (correctly) detects the connect() call is
    a NoReturn and marks everything after it as unreachable. But this is not
    correct; Flask does trickery at runtime to make it not the case. The
    simplest workaround is to just move those registration code to the end
    of the file.
---
 airflow/www/views.py | 48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/airflow/www/views.py b/airflow/www/views.py
index bfd5ed462c..c630d47af8 100644
--- a/airflow/www/views.py
+++ b/airflow/www/views.py
@@ -714,28 +714,6 @@ class AirflowBaseView(BaseView):
         )
 
 
-def add_user_permissions_to_dag(sender, template, context, **extra):
-    """
-    Adds `.can_edit`, `.can_trigger`, and `.can_delete` properties
-    to DAG based on current user's permissions.
-    Located in `views.py` rather than the DAG model to keep
-    permissions logic out of the Airflow core.
-    """
-    if 'dag' in context:
-        dag = context['dag']
-        can_create_dag_run = get_airflow_app().appbuilder.sm.has_access(
-            permissions.ACTION_CAN_CREATE, permissions.RESOURCE_DAG_RUN
-        )
-
-        dag.can_edit = get_airflow_app().appbuilder.sm.can_edit_dag(dag.dag_id)
-        dag.can_trigger = dag.can_edit and can_create_dag_run
-        dag.can_delete = get_airflow_app().appbuilder.sm.can_delete_dag(dag.dag_id)
-        context['dag'] = dag
-
-
-before_render_template.connect(add_user_permissions_to_dag)
-
-
 class Airflow(AirflowBaseView):
     """Main Airflow application."""
 
@@ -5632,3 +5610,29 @@ class CustomUserRemoteUserModelView(MultiResourceUserMixin, UserRemoteUserModelV
         permissions.ACTION_CAN_EDIT,
         permissions.ACTION_CAN_DELETE,
     ]
+
+
+def add_user_permissions_to_dag(sender, template, context, **extra):
+    """
+    Adds `.can_edit`, `.can_trigger`, and `.can_delete` properties
+    to DAG based on current user's permissions.
+    Located in `views.py` rather than the DAG model to keep
+    permissions logic out of the Airflow core.
+    """
+    if 'dag' in context:
+        dag = context['dag']
+        can_create_dag_run = get_airflow_app().appbuilder.sm.has_access(
+            permissions.ACTION_CAN_CREATE, permissions.RESOURCE_DAG_RUN
+        )
+
+        dag.can_edit = get_airflow_app().appbuilder.sm.can_edit_dag(dag.dag_id)
+        dag.can_trigger = dag.can_edit and can_create_dag_run
+        dag.can_delete = get_airflow_app().appbuilder.sm.can_delete_dag(dag.dag_id)
+        context['dag'] = dag
+
+
+# NOTE: Put this at the end of the file. Pylance is too clever and detects that
+# before_render_template.connect() is declared as NoReturn, and marks everything
+# after this line as unreachable code. It's technically correct based on the
+# lint-time information, but that's not what actually happens at runtime.
+before_render_template.connect(add_user_permissions_to_dag)