Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve importing the modules in Airflow www package #33810

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions airflow/www/api/experimental/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import logging
from functools import wraps
from typing import Callable, TypeVar, cast
from typing import TYPE_CHECKING, Callable, TypeVar, cast

from flask import Blueprint, Response, current_app, g, jsonify, request, url_for
from flask import Blueprint, current_app, g, jsonify, request, url_for

from airflow import models
from airflow.api.common.experimental import delete_dag as delete, pool as pool_api, trigger_dag as trigger
Expand All @@ -37,6 +37,9 @@
from airflow.utils.strings import to_boolean
from airflow.version import version

if TYPE_CHECKING:
from flask import Response

log = logging.getLogger(__name__)

T = TypeVar("T", bound=Callable)
Expand Down
10 changes: 7 additions & 3 deletions airflow/www/extensions/init_appbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

import logging
from functools import reduce
from typing import TYPE_CHECKING

from flask import Blueprint, current_app, url_for
from flask_appbuilder import BaseView, __version__
from flask_appbuilder import __version__
from flask_appbuilder.babel.manager import BabelManager
from flask_appbuilder.const import (
LOGMSG_ERR_FAB_ADD_PERMISSION_MENU,
Expand All @@ -35,14 +36,17 @@
)
from flask_appbuilder.filters import TemplateFilters
from flask_appbuilder.menu import Menu
from flask_appbuilder.security.manager import BaseSecurityManager
from flask_appbuilder.views import IndexView, UtilView
from sqlalchemy.orm import Session

from airflow import settings
from airflow.configuration import conf
from airflow.www.extensions.init_auth_manager import get_auth_manager

if TYPE_CHECKING:
from flask_appbuilder import BaseView
from flask_appbuilder.security.manager import BaseSecurityManager
from sqlalchemy.orm import Session

# This product contains a modified portion of 'Flask App Builder' developed by Daniel Vaz Gaspar.
# (https://github.com/dpgaspar/Flask-AppBuilder).
# Copyright 2013, Daniel Vaz Gaspar
Expand Down
6 changes: 5 additions & 1 deletion airflow/www/extensions/init_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
import warnings
from functools import cached_property
from os import path
from typing import TYPE_CHECKING

from connexion import FlaskApi, ProblemException, Resolver
from connexion.decorators.validation import RequestBodyValidator
from connexion.exceptions import BadRequestProblem
from flask import Flask, request
from flask import request

from airflow.api_connexion.exceptions import common_error_handler
from airflow.configuration import conf
from airflow.exceptions import RemovedInAirflow3Warning
from airflow.security import permissions
from airflow.utils.yaml import safe_load

if TYPE_CHECKING:
from flask import Flask

log = logging.getLogger(__name__)

# airflow/www/extensions/init_views.py => airflow/
Expand Down
2 changes: 1 addition & 1 deletion airflow/www/extensions/init_wsgi_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from typing import TYPE_CHECKING, Iterable
from urllib.parse import urlsplit

from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.middleware.proxy_fix import ProxyFix

Expand All @@ -29,6 +28,7 @@

if TYPE_CHECKING:
from _typeshed.wsgi import StartResponse, WSGIEnvironment
from flask import Flask


def _root_app(env: WSGIEnvironment, resp: StartResponse) -> Iterable[bytes]:
Expand Down
12 changes: 8 additions & 4 deletions airflow/www/fab_security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

import datetime
import logging
from typing import Any
from typing import TYPE_CHECKING, Any
from uuid import uuid4

import re2
from flask import Flask, g, session, url_for
from flask_appbuilder import AppBuilder
from flask import g, session, url_for
from flask_appbuilder.const import (
AUTH_DB,
AUTH_LDAP,
Expand Down Expand Up @@ -63,10 +62,15 @@
from flask_limiter.util import get_remote_address
from werkzeug.security import check_password_hash

from airflow.auth.managers.fab.models import Action, Permission, RegisterUser, Resource, Role, User
from airflow.configuration import conf
from airflow.www.extensions.init_auth_manager import get_auth_manager

if TYPE_CHECKING:
from flask import Flask
from flask_appbuilder import AppBuilder

from airflow.auth.managers.fab.models import Action, Permission, RegisterUser, Resource, Role, User

# This product contains a modified portion of 'Flask App Builder' developed by Daniel Vaz Gaspar.
# (https://github.com/dpgaspar/Flask-AppBuilder).
# Copyright 2013, Daniel Vaz Gaspar
Expand Down
4 changes: 3 additions & 1 deletion airflow/www/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from flask import g
from sqlalchemy import or_, select
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.orm import joinedload

from airflow.auth.managers.fab.models import Permission, Resource, Role, User
from airflow.auth.managers.fab.views.permissions import (
Expand Down Expand Up @@ -61,6 +61,8 @@
}

if TYPE_CHECKING:
from sqlalchemy.orm import Session

SecurityManagerOverride: type = object
else:
# Fetch the security manager override from the auth manager
Expand Down
9 changes: 5 additions & 4 deletions airflow/www/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@
from flask.helpers import flash
from flask_appbuilder.forms import FieldConverter
from flask_appbuilder.models.filters import BaseFilter
from flask_appbuilder.models.sqla import Model, filters as fab_sqlafilters
from flask_appbuilder.models.sqla import filters as fab_sqlafilters
from flask_appbuilder.models.sqla.filters import get_field_setup_query, set_value_to_type
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_babel import lazy_gettext
from markdown_it import MarkdownIt
from markupsafe import Markup
from pendulum.datetime import DateTime
from pygments import highlight, lexers
from pygments.formatters import HtmlFormatter
from pygments.lexer import Lexer
from sqlalchemy import delete, func, select, types
from sqlalchemy.ext.associationproxy import AssociationProxy
from sqlalchemy.sql import Select

from airflow.exceptions import RemovedInAirflow3Warning
from airflow.models import errors
Expand All @@ -56,7 +53,11 @@
from airflow.www.widgets import AirflowDateTimePickerWidget

if TYPE_CHECKING:
from flask_appbuilder.models.sqla import Model
from pendulum.datetime import DateTime
from pygments.lexer import Lexer
from sqlalchemy.orm.session import Session
from sqlalchemy.sql import Select
from sqlalchemy.sql.operators import ColumnOperators

from airflow.www.fab_security.sqla.manager import SecurityManager
Expand Down
19 changes: 11 additions & 8 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from collections import defaultdict
from functools import cached_property, wraps
from json import JSONDecodeError
from typing import Any, Callable, Collection, Iterator, Mapping, MutableMapping, Sequence
from typing import TYPE_CHECKING, Any, Callable, Collection, Iterator, Mapping, MutableMapping, Sequence
from urllib.parse import unquote, urljoin, urlsplit

import configupdater
Expand Down Expand Up @@ -69,7 +69,7 @@
from pendulum.parsing.exceptions import ParserError
from sqlalchemy import Date, and_, case, desc, func, inspect, select, union_all
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.orm import joinedload
from wtforms import BooleanField, validators

import airflow
Expand All @@ -96,12 +96,10 @@
from airflow.jobs.scheduler_job_runner import SchedulerJobRunner
from airflow.jobs.triggerer_job_runner import TriggererJobRunner
from airflow.models import Connection, DagModel, DagTag, Log, SlaMiss, TaskFail, Trigger, XCom, errors
from airflow.models.abstractoperator import AbstractOperator
from airflow.models.dag import DAG, get_dataset_triggered_next_run_info
from airflow.models.dag import get_dataset_triggered_next_run_info
from airflow.models.dagrun import RUN_ID_REGEX, DagRun, DagRunType
from airflow.models.dataset import DagScheduleDatasetReference, DatasetDagRunQueue, DatasetEvent, DatasetModel
from airflow.models.mappedoperator import MappedOperator
from airflow.models.operator import Operator
from airflow.models.serialized_dag import SerializedDagModel
from airflow.models.taskinstance import TaskInstance, TaskInstanceNote
from airflow.providers_manager import ProvidersManager
Expand Down Expand Up @@ -138,6 +136,13 @@
)
from airflow.www.widgets import AirflowModelListWidget, AirflowVariableShowWidget

if TYPE_CHECKING:
from sqlalchemy.orm import Session

from airflow.models.abstractoperator import AbstractOperator
from airflow.models.dag import DAG
from airflow.models.operator import Operator

PAGE_SIZE = conf.getint("webserver", "page_size")
FILTER_TAGS_COOKIE = "tags_filter"
FILTER_STATUS_COOKIE = "dag_status_filter"
Expand Down Expand Up @@ -835,9 +840,7 @@ def index(self):

is_paused_count = dict(
session.execute(
all_dags.with_only_columns([DagModel.is_paused, func.count()]).group_by(
DagModel.is_paused
)
select(DagModel.is_paused, func.count(DagModel.dag_id)).group_by(DagModel.is_paused)
).all()
)

Expand Down