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