apache-airflow-providers-fab 1.5.3__py3-none-any.whl → 2.0.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. airflow/providers/fab/LICENSE +0 -52
  2. airflow/providers/fab/__init__.py +3 -3
  3. airflow/providers/fab/auth_manager/api/auth/backend/basic_auth.py +3 -3
  4. airflow/providers/fab/auth_manager/api/auth/backend/kerberos_auth.py +4 -4
  5. airflow/providers/fab/auth_manager/api/auth/backend/session.py +1 -1
  6. airflow/providers/fab/auth_manager/api_endpoints/role_and_permission_endpoint.py +14 -13
  7. airflow/providers/fab/auth_manager/api_endpoints/user_endpoint.py +12 -12
  8. airflow/providers/fab/auth_manager/api_fastapi/__init__.py +16 -0
  9. airflow/providers/fab/auth_manager/api_fastapi/datamodels/__init__.py +16 -0
  10. airflow/providers/fab/auth_manager/api_fastapi/datamodels/login.py +32 -0
  11. airflow/providers/fab/auth_manager/api_fastapi/openapi/__init__.py +16 -0
  12. airflow/providers/fab/auth_manager/api_fastapi/openapi/v1-generated.yaml +152 -0
  13. airflow/providers/fab/auth_manager/api_fastapi/routes/__init__.py +16 -0
  14. airflow/providers/fab/auth_manager/api_fastapi/routes/login.py +51 -0
  15. airflow/providers/fab/auth_manager/api_fastapi/services/__init__.py +16 -0
  16. airflow/providers/fab/auth_manager/api_fastapi/services/login.py +58 -0
  17. airflow/providers/fab/auth_manager/cli_commands/db_command.py +2 -4
  18. airflow/providers/fab/auth_manager/cli_commands/user_command.py +2 -2
  19. airflow/providers/fab/auth_manager/cli_commands/utils.py +10 -9
  20. airflow/providers/fab/auth_manager/fab_auth_manager.py +231 -126
  21. airflow/providers/fab/auth_manager/models/__init__.py +1 -1
  22. airflow/providers/fab/auth_manager/models/anonymous_user.py +1 -1
  23. airflow/providers/fab/auth_manager/models/db.py +22 -5
  24. airflow/providers/fab/auth_manager/schemas/user_schema.py +1 -1
  25. airflow/providers/fab/auth_manager/security_manager/override.py +71 -632
  26. airflow/providers/fab/auth_manager/views/permissions.py +1 -1
  27. airflow/providers/fab/auth_manager/views/roles_list.py +1 -1
  28. airflow/providers/fab/auth_manager/views/user.py +1 -1
  29. airflow/providers/fab/auth_manager/views/user_edit.py +1 -1
  30. airflow/providers/fab/auth_manager/views/user_stats.py +1 -1
  31. airflow/providers/fab/get_provider_info.py +22 -16
  32. airflow/providers/fab/www/airflow_flask_app.py +31 -0
  33. airflow/providers/fab/www/api_connexion/__init__.py +17 -0
  34. airflow/providers/fab/www/api_connexion/exceptions.py +197 -0
  35. airflow/providers/fab/www/api_connexion/parameters.py +131 -0
  36. airflow/providers/fab/www/api_connexion/security.py +84 -0
  37. airflow/providers/fab/www/api_connexion/types.py +30 -0
  38. airflow/providers/fab/www/app.py +112 -0
  39. airflow/providers/fab/www/auth.py +350 -0
  40. airflow/providers/fab/www/constants.py +28 -0
  41. airflow/providers/fab/www/extensions/__init__.py +16 -0
  42. airflow/providers/fab/www/extensions/init_appbuilder.py +602 -0
  43. airflow/providers/fab/www/extensions/init_jinja_globals.py +82 -0
  44. airflow/providers/fab/www/extensions/init_manifest_files.py +61 -0
  45. airflow/providers/fab/www/extensions/init_security.py +61 -0
  46. airflow/providers/fab/www/extensions/init_session.py +64 -0
  47. airflow/providers/fab/www/extensions/init_views.py +177 -0
  48. airflow/providers/fab/www/package-lock.json +10127 -0
  49. airflow/providers/fab/www/package.json +81 -0
  50. airflow/providers/fab/www/security/__init__.py +17 -0
  51. airflow/providers/fab/www/security/permissions.py +126 -0
  52. airflow/providers/fab/www/security_appless.py +44 -0
  53. airflow/providers/fab/www/security_manager.py +122 -0
  54. airflow/providers/fab/www/session.py +41 -0
  55. airflow/providers/fab/www/static/css/bootstrap-theme.css +6215 -0
  56. airflow/providers/fab/www/static/css/flash.css +57 -0
  57. airflow/providers/fab/www/static/css/loading-dots.css +60 -0
  58. airflow/providers/fab/www/static/css/main.css +676 -0
  59. airflow/providers/fab/www/static/css/material-icons.css +84 -0
  60. airflow/providers/fab/www/static/dist/airflowDefaultTheme.feec4a4075c2f3d6ae01.css +33 -0
  61. airflow/providers/fab/www/static/dist/airflowDefaultTheme.feec4a4075c2f3d6ae01.js +1 -0
  62. airflow/providers/fab/www/static/dist/flash.137b30cff85b5588e661.css +18 -0
  63. airflow/providers/fab/www/static/dist/flash.137b30cff85b5588e661.js +1 -0
  64. airflow/providers/fab/www/static/dist/jquery-ui.min.css +5 -0
  65. airflow/providers/fab/www/static/dist/jquery-ui.min.js +2 -0
  66. airflow/providers/fab/www/static/dist/jquery-ui.min.js.LICENSE.txt +4 -0
  67. airflow/providers/fab/www/static/dist/loadingDots.48ab7d5b04e66f2686b0.css +18 -0
  68. airflow/providers/fab/www/static/dist/loadingDots.48ab7d5b04e66f2686b0.js +1 -0
  69. airflow/providers/fab/www/static/dist/main.ec1d38d994d72bb083cd.css +18 -0
  70. airflow/providers/fab/www/static/dist/main.ec1d38d994d72bb083cd.js +2 -0
  71. airflow/providers/fab/www/static/dist/main.ec1d38d994d72bb083cd.js.LICENSE.txt +18 -0
  72. airflow/providers/fab/www/static/dist/manifest.json +17 -0
  73. airflow/providers/fab/www/static/dist/materialIcons.57390fa60d8f61175334.css +18 -0
  74. airflow/providers/fab/www/static/dist/materialIcons.57390fa60d8f61175334.js +1 -0
  75. airflow/providers/fab/www/static/dist/moment.4d28b37c229bdfc54575.js +2 -0
  76. airflow/providers/fab/www/static/dist/moment.4d28b37c229bdfc54575.js.LICENSE.txt +11 -0
  77. airflow/providers/fab/www/static/dist/oss-licenses.json +29 -0
  78. airflow/providers/fab/www/static/js/datetime_utils.js +134 -0
  79. airflow/providers/fab/www/static/js/main.js +324 -0
  80. airflow/providers/fab/www/static/sort_asc.png +0 -0
  81. airflow/providers/fab/www/static/sort_both.png +0 -0
  82. airflow/providers/fab/www/static/sort_desc.png +0 -0
  83. airflow/providers/fab/www/templates/airflow/_messages.html +30 -0
  84. airflow/providers/fab/www/templates/airflow/error.html +35 -0
  85. airflow/providers/fab/www/templates/airflow/main.html +78 -0
  86. airflow/providers/fab/www/templates/airflow/traceback.html +53 -0
  87. airflow/providers/fab/www/templates/appbuilder/flash.html +34 -0
  88. airflow/providers/fab/www/templates/appbuilder/index.html +20 -0
  89. airflow/providers/fab/www/templates/appbuilder/navbar.html +60 -0
  90. airflow/providers/fab/www/templates/appbuilder/navbar_menu.html +60 -0
  91. airflow/providers/fab/www/templates/appbuilder/navbar_right.html +64 -0
  92. airflow/providers/fab/www/utils.py +272 -0
  93. airflow/providers/fab/www/views.py +129 -0
  94. airflow/providers/fab/www/webpack.config.js +213 -0
  95. {apache_airflow_providers_fab-1.5.3.dist-info → apache_airflow_providers_fab-2.0.0b1.dist-info}/METADATA +17 -35
  96. apache_airflow_providers_fab-2.0.0b1.dist-info/RECORD +122 -0
  97. {apache_airflow_providers_fab-1.5.3.dist-info → apache_airflow_providers_fab-2.0.0b1.dist-info}/WHEEL +1 -1
  98. airflow/providers/fab/auth_manager/decorators/auth.py +0 -126
  99. apache_airflow_providers_fab-1.5.3.dist-info/RECORD +0 -51
  100. /airflow/providers/fab/{auth_manager/decorators → www}/__init__.py +0 -0
  101. {apache_airflow_providers_fab-1.5.3.dist-info → apache_airflow_providers_fab-2.0.0b1.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,602 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # mypy: disable-error-code=var-annotated
19
+ from __future__ import annotations
20
+
21
+ import logging
22
+ from functools import reduce
23
+ from typing import TYPE_CHECKING
24
+
25
+ from flask import Blueprint, current_app, url_for
26
+ from flask_appbuilder import __version__
27
+ from flask_appbuilder.babel.manager import BabelManager
28
+ from flask_appbuilder.const import (
29
+ LOGMSG_ERR_FAB_ADD_PERMISSION_MENU,
30
+ LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW,
31
+ LOGMSG_ERR_FAB_ADDON_IMPORT,
32
+ LOGMSG_ERR_FAB_ADDON_PROCESS,
33
+ LOGMSG_INF_FAB_ADD_VIEW,
34
+ LOGMSG_INF_FAB_ADDON_ADDED,
35
+ LOGMSG_WAR_FAB_VIEW_EXISTS,
36
+ )
37
+ from flask_appbuilder.filters import TemplateFilters
38
+ from flask_appbuilder.menu import Menu
39
+ from flask_appbuilder.views import IndexView, UtilView
40
+
41
+ from airflow import settings
42
+ from airflow.api_fastapi.app import create_auth_manager, get_auth_manager
43
+ from airflow.configuration import conf
44
+ from airflow.providers.fab.www.security_manager import AirflowSecurityManagerV2
45
+ from airflow.providers.fab.www.views import FabIndexView
46
+
47
+ if TYPE_CHECKING:
48
+ from flask import Flask
49
+ from flask_appbuilder import BaseView
50
+ from flask_appbuilder.security.manager import BaseSecurityManager
51
+ from sqlalchemy.orm import Session
52
+
53
+
54
+ # This product contains a modified portion of 'Flask App Builder' developed by Daniel Vaz Gaspar.
55
+ # (https://github.com/dpgaspar/Flask-AppBuilder).
56
+ # Copyright 2013, Daniel Vaz Gaspar
57
+ # This module contains code imported from FlaskAppbuilder, so lets use _its_ logger name
58
+ log = logging.getLogger("flask_appbuilder.base")
59
+
60
+
61
+ def dynamic_class_import(class_path):
62
+ """
63
+ Will dynamically import a class from a string path.
64
+
65
+ :param class_path: string with class path
66
+ :return: class
67
+ """
68
+ # Split first occurrence of path
69
+ try:
70
+ tmp = class_path.split(".")
71
+ module_path = ".".join(tmp[0:-1])
72
+ package = __import__(module_path)
73
+ return reduce(getattr, tmp[1:], package)
74
+ except Exception as e:
75
+ log.exception(e)
76
+ log.error(LOGMSG_ERR_FAB_ADDON_IMPORT, class_path, e)
77
+
78
+
79
+ class AirflowAppBuilder:
80
+ """This is the base class for all the framework."""
81
+
82
+ baseviews: list[BaseView | Session] = []
83
+ # Flask app
84
+ app = None
85
+ # Database Session
86
+ session = None
87
+ # Security Manager Class
88
+ sm: BaseSecurityManager
89
+ # Babel Manager Class
90
+ bm = None
91
+ # dict with addon name has key and instantiated class has value
92
+ addon_managers: dict
93
+ # temporary list that hold addon_managers config key
94
+ _addon_managers: list
95
+
96
+ menu = None
97
+ indexview = None
98
+
99
+ static_folder = None
100
+ static_url_path = None
101
+
102
+ template_filters = None
103
+
104
+ def __init__(
105
+ self,
106
+ app=None,
107
+ session: Session | None = None,
108
+ menu=None,
109
+ indexview=None,
110
+ base_template="airflow/main.html",
111
+ static_folder="static/appbuilder",
112
+ static_url_path="/appbuilder",
113
+ enable_plugins: bool = False,
114
+ ):
115
+ """
116
+ App-builder constructor.
117
+
118
+ :param app:
119
+ The flask app object
120
+ :param session:
121
+ The SQLAlchemy session object
122
+ :param menu:
123
+ optional, a previous constructed menu
124
+ :param indexview:
125
+ optional, your customized indexview
126
+ :param static_folder:
127
+ optional, your override for the global static folder
128
+ :param static_url_path:
129
+ optional, your override for the global static url path
130
+ :param enable_plugins:
131
+ optional, whether plugins are enabled for this app. AirflowAppBuilder from FAB provider can be
132
+ instantiated in two modes:
133
+ - Plugins enabled. The Flask application is responsible to execute Airflow 2 plugins.
134
+ This application is only running if there are Airflow 2 plugins defined as part of the Airflow
135
+ environment
136
+ - Plugins disabled. The Flask application is responsible to execute the FAB auth manager login
137
+ process. This application is only running if FAB auth manager is the auth manager configured
138
+ in the Airflow environment
139
+ """
140
+ from airflow.providers_manager import ProvidersManager
141
+
142
+ providers_manager = ProvidersManager()
143
+ providers_manager.initialize_providers_configuration()
144
+ self.baseviews = []
145
+ self._addon_managers = []
146
+ self.addon_managers = {}
147
+ self.menu = menu
148
+ self.base_template = base_template
149
+ self.indexview = indexview
150
+ self.static_folder = static_folder
151
+ self.static_url_path = static_url_path
152
+ self.app = app
153
+ self.enable_plugins = enable_plugins
154
+ self.update_perms = conf.getboolean("fab", "UPDATE_FAB_PERMS")
155
+ self.auth_rate_limited = conf.getboolean("fab", "AUTH_RATE_LIMITED")
156
+ self.auth_rate_limit = conf.get("fab", "AUTH_RATE_LIMIT")
157
+ if app is not None:
158
+ self.init_app(app, session)
159
+
160
+ def init_app(self, app, session):
161
+ """
162
+ Will initialize the Flask app, supporting the app factory pattern.
163
+
164
+ :param app:
165
+ :param session: The SQLAlchemy session
166
+ """
167
+ app.config.setdefault("APP_NAME", "F.A.B.")
168
+ app.config.setdefault("APP_THEME", "")
169
+ app.config.setdefault("APP_ICON", "")
170
+ app.config.setdefault("LANGUAGES", {"en": {"flag": "gb", "name": "English"}})
171
+ app.config.setdefault("ADDON_MANAGERS", [])
172
+ app.config.setdefault("RATELIMIT_ENABLED", self.auth_rate_limited)
173
+ app.config.setdefault("FAB_BASE_TEMPLATE", self.base_template)
174
+ app.config.setdefault("FAB_STATIC_FOLDER", self.static_folder)
175
+ app.config.setdefault("FAB_STATIC_URL_PATH", self.static_url_path)
176
+ app.config.setdefault("AUTH_RATE_LIMITED", self.auth_rate_limited)
177
+ app.config.setdefault("AUTH_RATE_LIMIT", self.auth_rate_limit)
178
+
179
+ self.app = app
180
+
181
+ self.base_template = app.config.get("FAB_BASE_TEMPLATE", self.base_template)
182
+ self.static_folder = app.config.get("FAB_STATIC_FOLDER", self.static_folder)
183
+ self.static_url_path = app.config.get("FAB_STATIC_URL_PATH", self.static_url_path)
184
+ _index_view = app.config.get("FAB_INDEX_VIEW", None)
185
+ if _index_view is not None:
186
+ self.indexview = dynamic_class_import(_index_view)
187
+ elif not self.enable_plugins:
188
+ self.indexview = FabIndexView
189
+ else:
190
+ self.indexview = IndexView
191
+ _menu = app.config.get("FAB_MENU", None)
192
+ if _menu is not None:
193
+ self.menu = dynamic_class_import(_menu)
194
+ else:
195
+ self.menu = self.menu or Menu()
196
+
197
+ self._addon_managers = app.config["ADDON_MANAGERS"]
198
+ self.session = session
199
+ auth_manager = create_auth_manager()
200
+ auth_manager.appbuilder = self
201
+ if hasattr(auth_manager, "init_flask_resources"):
202
+ auth_manager.init_flask_resources()
203
+ if hasattr(auth_manager, "security_manager"):
204
+ self.sm = auth_manager.security_manager
205
+ else:
206
+ self.sm = AirflowSecurityManagerV2(self)
207
+ self.bm = BabelManager(self)
208
+ self._add_global_static()
209
+ self._add_global_filters()
210
+ app.before_request(self.sm.before_request)
211
+ self._add_admin_views()
212
+ self._add_addon_views()
213
+ self._init_extension(app)
214
+ self._swap_url_filter()
215
+
216
+ def _swap_url_filter(self):
217
+ """Use our url filtering util function so there is consistency between FAB and Airflow routes."""
218
+ from flask_appbuilder.security import views as fab_sec_views
219
+
220
+ from airflow.providers.fab.www.views import get_safe_url
221
+
222
+ fab_sec_views.get_safe_redirect = get_safe_url
223
+
224
+ def _init_extension(self, app):
225
+ app.appbuilder = self
226
+ if not hasattr(app, "extensions"):
227
+ app.extensions = {}
228
+ app.extensions["appbuilder"] = self
229
+
230
+ @property
231
+ def get_app(self):
232
+ """
233
+ Get current or configured flask app.
234
+
235
+ :return: Flask App
236
+ """
237
+ if self.app:
238
+ return self.app
239
+ else:
240
+ return current_app
241
+
242
+ @property
243
+ def get_session(self):
244
+ """
245
+ Get the current sqlalchemy session.
246
+
247
+ :return: SQLAlchemy Session
248
+ """
249
+ return self.session
250
+
251
+ @property
252
+ def app_name(self):
253
+ """
254
+ Get the App name.
255
+
256
+ :return: String with app name
257
+ """
258
+ return self.get_app.config["APP_NAME"]
259
+
260
+ @property
261
+ def app_theme(self):
262
+ """
263
+ Get the App theme name.
264
+
265
+ :return: String app theme name
266
+ """
267
+ return self.get_app.config["APP_THEME"]
268
+
269
+ @property
270
+ def app_icon(self):
271
+ """
272
+ Get the App icon location.
273
+
274
+ :return: String with relative app icon location
275
+ """
276
+ return self.get_app.config["APP_ICON"]
277
+
278
+ @property
279
+ def languages(self):
280
+ return self.get_app.config["LANGUAGES"]
281
+
282
+ @property
283
+ def version(self):
284
+ """
285
+ Get the current F.A.B. version.
286
+
287
+ :return: String with the current F.A.B. version
288
+ """
289
+ return __version__
290
+
291
+ def _add_global_filters(self):
292
+ self.template_filters = TemplateFilters(self.get_app, self.sm)
293
+
294
+ def _add_global_static(self):
295
+ bp = Blueprint(
296
+ "appbuilder",
297
+ "flask_appbuilder.base",
298
+ url_prefix="/static",
299
+ template_folder="templates",
300
+ static_folder=self.static_folder,
301
+ static_url_path=self.static_url_path,
302
+ )
303
+ self.get_app.register_blueprint(bp)
304
+
305
+ def _add_admin_views(self):
306
+ """Register indexview, utilview (back function), babel views and Security views."""
307
+ self.indexview = self._check_and_init(self.indexview)
308
+ self.add_view_no_menu(self.indexview)
309
+ self.add_view_no_menu(UtilView())
310
+ auth_manager = get_auth_manager()
311
+ if hasattr(auth_manager, "register_views"):
312
+ auth_manager.register_views()
313
+
314
+ def _add_addon_views(self):
315
+ """Register declared addons."""
316
+ for addon in self._addon_managers:
317
+ addon_class = dynamic_class_import(addon)
318
+ if addon_class:
319
+ # Instantiate manager with appbuilder (self)
320
+ addon_class = addon_class(self)
321
+ try:
322
+ addon_class.pre_process()
323
+ addon_class.register_views()
324
+ addon_class.post_process()
325
+ self.addon_managers[addon] = addon_class
326
+ log.info(LOGMSG_INF_FAB_ADDON_ADDED, addon)
327
+ except Exception as e:
328
+ log.exception(e)
329
+ log.error(LOGMSG_ERR_FAB_ADDON_PROCESS, addon, e)
330
+
331
+ def _check_and_init(self, baseview):
332
+ if hasattr(baseview, "datamodel"):
333
+ baseview.datamodel.session = self.session
334
+ if callable(baseview):
335
+ baseview = baseview()
336
+ return baseview
337
+
338
+ def add_view(
339
+ self,
340
+ baseview,
341
+ name,
342
+ href="",
343
+ icon="",
344
+ label="",
345
+ category="",
346
+ category_icon="",
347
+ category_label="",
348
+ menu_cond=None,
349
+ ):
350
+ """
351
+ Add your views associated with menus using this method.
352
+
353
+ :param baseview:
354
+ A BaseView type class instantiated or not.
355
+ This method will instantiate the class for you if needed.
356
+ :param name:
357
+ The string name that identifies the menu.
358
+ :param href:
359
+ Override the generated link for the menu.
360
+ You can use an url string or an endpoint name
361
+ if non provided default_view from view will be set as link.
362
+ :param icon:
363
+ Font-Awesome icon name, optional.
364
+ :param label:
365
+ The label that will be displayed on the menu,
366
+ if absent param name will be used
367
+ :param category:
368
+ The menu category where the menu will be included,
369
+ if non provided the view will be accessible as a top menu.
370
+ :param category_icon:
371
+ Font-Awesome icon name for the category, optional.
372
+ :param category_label:
373
+ The label that will be displayed on the menu,
374
+ if absent param name will be used
375
+ :param menu_cond:
376
+ If a callable, :code:`menu_cond` will be invoked when
377
+ constructing the menu items. If it returns :code:`True`,
378
+ then this link will be a part of the menu. Otherwise, it
379
+ will not be included in the menu items. Defaults to
380
+ :code:`None`, meaning the item will always be present.
381
+
382
+ Examples::
383
+
384
+ appbuilder = AppBuilder(app, db)
385
+ # Register a view, rendering a top menu without icon.
386
+ appbuilder.add_view(MyModelView(), "My View")
387
+ # or not instantiated
388
+ appbuilder.add_view(MyModelView, "My View")
389
+ # Register a view, a submenu "Other View" from "Other" with a phone icon.
390
+ appbuilder.add_view(MyOtherModelView, "Other View", icon="fa-phone", category="Others")
391
+ # Register a view, with category icon and translation.
392
+ appbuilder.add_view(
393
+ YetOtherModelView,
394
+ "Other View",
395
+ icon="fa-phone",
396
+ label=_("Other View"),
397
+ category="Others",
398
+ category_icon="fa-envelop",
399
+ category_label=_("Other View"),
400
+ )
401
+ # Register a view whose menu item will be conditionally displayed
402
+ appbuilder.add_view(
403
+ YourFeatureView,
404
+ "Your Feature",
405
+ icon="fa-feature",
406
+ label=_("Your Feature"),
407
+ menu_cond=lambda: is_feature_enabled("your-feature"),
408
+ )
409
+ # Add a link
410
+ appbuilder.add_link("google", href="www.google.com", icon="fa-google-plus")
411
+ """
412
+ baseview = self._check_and_init(baseview)
413
+ log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, name)
414
+
415
+ if not self._view_exists(baseview):
416
+ baseview.appbuilder = self
417
+ self.baseviews.append(baseview)
418
+ self._process_inner_views()
419
+ if self.app:
420
+ self.register_blueprint(baseview)
421
+ self._add_permission(baseview)
422
+ self.add_limits(baseview)
423
+ self.add_link(
424
+ name=name,
425
+ href=href,
426
+ icon=icon,
427
+ label=label,
428
+ category=category,
429
+ category_icon=category_icon,
430
+ category_label=category_label,
431
+ baseview=baseview,
432
+ cond=menu_cond,
433
+ )
434
+ return baseview
435
+
436
+ def add_link(
437
+ self,
438
+ name,
439
+ href,
440
+ icon="",
441
+ label="",
442
+ category="",
443
+ category_icon="",
444
+ category_label="",
445
+ baseview=None,
446
+ cond=None,
447
+ ):
448
+ """
449
+ Add your own links to menu using this method.
450
+
451
+ :param name:
452
+ The string name that identifies the menu.
453
+ :param href:
454
+ Override the generated link for the menu.
455
+ You can use an url string or an endpoint name
456
+ :param icon:
457
+ Font-Awesome icon name, optional.
458
+ :param label:
459
+ The label that will be displayed on the menu,
460
+ if absent param name will be used
461
+ :param category:
462
+ The menu category where the menu will be included,
463
+ if non provided the view will be accessible as a top menu.
464
+ :param category_icon:
465
+ Font-Awesome icon name for the category, optional.
466
+ :param category_label:
467
+ The label that will be displayed on the menu,
468
+ if absent param name will be used
469
+ :param baseview:
470
+ A BaseView type class instantiated.
471
+ :param cond:
472
+ If a callable, :code:`cond` will be invoked when
473
+ constructing the menu items. If it returns :code:`True`,
474
+ then this link will be a part of the menu. Otherwise, it
475
+ will not be included in the menu items. Defaults to
476
+ :code:`None`, meaning the item will always be present.
477
+ """
478
+ self.menu.add_link(
479
+ name=name,
480
+ href=href,
481
+ icon=icon,
482
+ label=label,
483
+ category=category,
484
+ category_icon=category_icon,
485
+ category_label=category_label,
486
+ baseview=baseview,
487
+ cond=cond,
488
+ )
489
+ if self.app:
490
+ self._add_permissions_menu(name)
491
+ if category:
492
+ self._add_permissions_menu(category)
493
+
494
+ def add_separator(self, category, cond=None):
495
+ """
496
+ Add a separator to the menu, you will sequentially create the menu.
497
+
498
+ :param category:
499
+ The menu category where the separator will be included.
500
+ :param cond:
501
+ If a callable, :code:`cond` will be invoked when
502
+ constructing the menu items. If it returns :code:`True`,
503
+ then this separator will be a part of the menu. Otherwise,
504
+ it will not be included in the menu items. Defaults to
505
+ :code:`None`, meaning the separator will always be present.
506
+ """
507
+ self.menu.add_separator(category, cond=cond)
508
+
509
+ def add_view_no_menu(self, baseview, endpoint=None, static_folder=None):
510
+ """
511
+ Add your views without creating a menu.
512
+
513
+ :param baseview: A BaseView type class instantiated.
514
+ """
515
+ baseview = self._check_and_init(baseview)
516
+ log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, "")
517
+
518
+ if not self._view_exists(baseview):
519
+ baseview.appbuilder = self
520
+ self.baseviews.append(baseview)
521
+ self._process_inner_views()
522
+ if self.app:
523
+ self.register_blueprint(baseview, endpoint=endpoint, static_folder=static_folder)
524
+ self._add_permission(baseview)
525
+ else:
526
+ log.warning(LOGMSG_WAR_FAB_VIEW_EXISTS, baseview.__class__.__name__)
527
+ return baseview
528
+
529
+ @property
530
+ def get_url_for_index(self):
531
+ return url_for(f"{self.indexview.endpoint}.{self.indexview.default_view}")
532
+
533
+ def get_url_for_login_with(self, next_url: str | None = None) -> str:
534
+ return get_auth_manager().get_url_login(next_url=next_url)
535
+
536
+ def get_url_for_locale(self, lang):
537
+ return url_for(
538
+ f"{self.bm.locale_view.endpoint}.{self.bm.locale_view.default_view}",
539
+ locale=lang,
540
+ )
541
+
542
+ def add_limits(self, baseview) -> None:
543
+ if hasattr(baseview, "limits"):
544
+ self.sm.add_limit_view(baseview)
545
+
546
+ def _add_permission(self, baseview, update_perms=False):
547
+ if self.update_perms or update_perms:
548
+ try:
549
+ if hasattr(self.sm, "add_permissions_view"):
550
+ self.sm.add_permissions_view(baseview.base_permissions, baseview.class_permission_name)
551
+ except Exception as e:
552
+ log.exception(e)
553
+ log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW, e)
554
+
555
+ def add_permissions(self, update_perms=False):
556
+ if self.update_perms or update_perms:
557
+ for baseview in self.baseviews:
558
+ self._add_permission(baseview, update_perms=update_perms)
559
+ self._add_menu_permissions(update_perms=update_perms)
560
+
561
+ def _add_permissions_menu(self, name, update_perms=False):
562
+ if self.update_perms or update_perms:
563
+ try:
564
+ if hasattr(self.sm, "add_permissions_menu"):
565
+ self.sm.add_permissions_menu(name)
566
+ except Exception as e:
567
+ log.exception(e)
568
+ log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_MENU, e)
569
+
570
+ def _add_menu_permissions(self, update_perms=False):
571
+ if self.update_perms or update_perms:
572
+ for category in self.menu.get_list():
573
+ self._add_permissions_menu(category.name, update_perms=update_perms)
574
+ for item in category.childs:
575
+ # don't add permission for menu separator
576
+ if item.name != "-":
577
+ self._add_permissions_menu(item.name, update_perms=update_perms)
578
+
579
+ def register_blueprint(self, baseview, endpoint=None, static_folder=None):
580
+ self.get_app.register_blueprint(
581
+ baseview.create_blueprint(self, endpoint=endpoint, static_folder=static_folder)
582
+ )
583
+
584
+ def _view_exists(self, view):
585
+ return any(baseview.__class__ == view.__class__ for baseview in self.baseviews)
586
+
587
+ def _process_inner_views(self):
588
+ for view in self.baseviews:
589
+ for inner_class in view.get_uninit_inner_views():
590
+ for v in self.baseviews:
591
+ if isinstance(v, inner_class) and v not in view.get_init_inner_views():
592
+ view.get_init_inner_views().append(v)
593
+
594
+
595
+ def init_appbuilder(app: Flask, enable_plugins: bool) -> AirflowAppBuilder:
596
+ """Init `Flask App Builder <https://flask-appbuilder.readthedocs.io/en/latest/>`__."""
597
+ return AirflowAppBuilder(
598
+ app=app,
599
+ session=settings.Session,
600
+ base_template="airflow/main.html",
601
+ enable_plugins=enable_plugins,
602
+ )
@@ -0,0 +1,82 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+ from __future__ import annotations
18
+
19
+ import logging
20
+
21
+ import pendulum
22
+
23
+ import airflow
24
+ from airflow.api_fastapi.app import get_auth_manager
25
+ from airflow.configuration import conf
26
+ from airflow.settings import STATE_COLORS
27
+ from airflow.utils.net import get_hostname
28
+ from airflow.utils.platform import get_airflow_git_version
29
+
30
+ logger = logging.getLogger(__name__)
31
+
32
+
33
+ def init_jinja_globals(app, enable_plugins: bool):
34
+ """Add extra globals variable to Jinja context."""
35
+ server_timezone = conf.get("core", "default_timezone")
36
+ if server_timezone == "system":
37
+ server_timezone = pendulum.local_timezone().name # type: ignore[operator]
38
+ elif server_timezone == "utc":
39
+ server_timezone = "UTC"
40
+
41
+ default_ui_timezone = conf.get("webserver", "default_ui_timezone")
42
+ if default_ui_timezone == "system":
43
+ default_ui_timezone = pendulum.local_timezone().name # type: ignore[operator]
44
+ elif default_ui_timezone == "utc":
45
+ default_ui_timezone = "UTC"
46
+ if not default_ui_timezone:
47
+ default_ui_timezone = server_timezone
48
+
49
+ expose_hostname = conf.getboolean("webserver", "EXPOSE_HOSTNAME")
50
+ hostname = get_hostname() if expose_hostname else "redact"
51
+
52
+ try:
53
+ airflow_version = airflow.__version__
54
+ except Exception as e:
55
+ airflow_version = None
56
+ logger.error(e)
57
+
58
+ git_version = get_airflow_git_version()
59
+
60
+ def prepare_jinja_globals():
61
+ extra_globals = {
62
+ "server_timezone": server_timezone,
63
+ "default_ui_timezone": default_ui_timezone,
64
+ "hostname": hostname,
65
+ "navbar_color": conf.get("webserver", "NAVBAR_COLOR"),
66
+ "navbar_text_color": conf.get("webserver", "NAVBAR_TEXT_COLOR"),
67
+ "navbar_hover_color": conf.get("webserver", "NAVBAR_HOVER_COLOR"),
68
+ "navbar_text_hover_color": conf.get("webserver", "NAVBAR_TEXT_HOVER_COLOR"),
69
+ "navbar_logo_text_color": conf.get("webserver", "NAVBAR_LOGO_TEXT_COLOR"),
70
+ "state_color_mapping": STATE_COLORS,
71
+ "airflow_version": airflow_version,
72
+ "git_version": git_version,
73
+ "show_plugin_message": enable_plugins,
74
+ "disable_nav_bar": not enable_plugins,
75
+ }
76
+
77
+ # Extra global specific to auth manager
78
+ extra_globals["auth_manager"] = get_auth_manager()
79
+
80
+ return extra_globals
81
+
82
+ app.context_processor(prepare_jinja_globals)