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

More flexibility with FAB menu links #13903

Merged
merged 1 commit into from
Feb 17, 2021
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
9 changes: 2 additions & 7 deletions airflow/www/extensions/init_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,8 @@ def init_plugins(app):
appbuilder.add_view_no_menu(view["view"])

for menu_link in sorted(plugins_manager.flask_appbuilder_menu_links, key=lambda x: x["name"]):
log.debug("Adding menu link %s", menu_link["name"])
appbuilder.add_link(
menu_link["name"],
href=menu_link["href"],
category=menu_link["category"],
category_icon=menu_link["category_icon"],
)
log.debug("Adding menu link %s to %s", menu_link["name"], menu_link["href"])
appbuilder.add_link(**menu_link)

for blue_print in plugins_manager.flask_blueprints:
log.debug("Adding blueprint %s:%s", blue_print["name"], blue_print["blueprint"].import_name)
Expand Down
19 changes: 12 additions & 7 deletions docs/apache-airflow/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ looks like:
flask_blueprints = []
# A list of dictionaries containing FlaskAppBuilder BaseView object and some metadata. See example below
appbuilder_views = []
# A list of dictionaries containing FlaskAppBuilder BaseView object and some metadata. See example below
# A list of dictionaries containing kwargs for FlaskAppBuilder add_link. See example below
appbuilder_menu_items = []
# A callback to perform actions when airflow starts and the plugin is loaded.
# NOTE: Ensure your plugin has *args, and **kwargs in the method definition
Expand Down Expand Up @@ -210,11 +210,16 @@ definitions in Airflow.
"view": v_appbuilder_nomenu_view
}

# Creating a flask appbuilder Menu Item
appbuilder_mitem = {"name": "Google",
"category": "Search",
"category_icon": "fa-th",
"href": "https://www.google.com"}
# Creating flask appbuilder Menu Items
appbuilder_mitem = {
"name": "Google",
"href": "https://www.google.com",
"category": "Search",
}
appbuilder_mitem_toplevel = {
"name": "Apache",
"href": "https://www.apache.org/",
}

# A global operator extra link that redirect you to
# task logs stored in S3
Expand Down Expand Up @@ -247,7 +252,7 @@ definitions in Airflow.
macros = [plugin_macro]
flask_blueprints = [bp]
appbuilder_views = [v_appbuilder_package, v_appbuilder_nomenu_package]
appbuilder_menu_items = [appbuilder_mitem]
appbuilder_menu_items = [appbuilder_mitem, appbuilder_mitem_toplevel]
global_operator_extra_links = [GoogleLink(),]
operator_extra_links = [S3LogLink(), ]

Expand Down
12 changes: 8 additions & 4 deletions tests/plugins/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ def test(self):

v_nomenu_appbuilder_package = {"view": v_appbuilder_view}

# Creating a flask appbuilder Menu Item
# Creating flask appbuilder Menu Items
appbuilder_mitem = {
"name": "Google",
"category": "Search",
"category_icon": "fa-th",
"href": "https://www.google.com",
"category": "Search",
}
appbuilder_mitem_toplevel = {
"name": "apache",
"href": "https://www.apache.org/",
"label": "The Apache Software Foundation",
}

# Creating a flask blueprint to intergrate the templates and static folder
Expand All @@ -105,7 +109,7 @@ class AirflowTestPlugin(AirflowPlugin):
macros = [plugin_macro]
flask_blueprints = [bp]
appbuilder_views = [v_appbuilder_package]
appbuilder_menu_items = [appbuilder_mitem]
appbuilder_menu_items = [appbuilder_mitem, appbuilder_mitem_toplevel]
global_operator_extra_links = [
AirflowLink(),
GithubLink(),
Expand Down
27 changes: 19 additions & 8 deletions tests/plugins/test_plugins_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,32 @@ class AirflowNoMenuViewsPlugin(AirflowPlugin):
assert len(plugin_views) == 1

def test_flaskappbuilder_menu_links(self):
from tests.plugins.test_plugin import appbuilder_mitem
from tests.plugins.test_plugin import appbuilder_mitem, appbuilder_mitem_toplevel

# menu item should exist matching appbuilder_mitem
links = [
# menu item (category) should exist matching appbuilder_mitem.category
categories = [
menu_item
for menu_item in self.appbuilder.menu.menu
if menu_item.name == appbuilder_mitem['category']
]
assert len(categories) == 1

assert len(links) == 1
# menu link should be a child in the category
category = categories[0]
assert category.name == appbuilder_mitem['category']
assert category.childs[0].name == appbuilder_mitem['name']
assert category.childs[0].href == appbuilder_mitem['href']

# menu link should also have a link matching the name of the package.
link = links[0]
assert link.name == appbuilder_mitem['category']
assert link.childs[0].name == appbuilder_mitem['name']
# a top level link isn't nested in a category
top_levels = [
menu_item
for menu_item in self.appbuilder.menu.menu
if menu_item.name == appbuilder_mitem_toplevel['name']
]
assert len(top_levels) == 1
link = top_levels[0]
assert link.href == appbuilder_mitem_toplevel['href']
assert link.label == appbuilder_mitem_toplevel['label']

def test_app_blueprints(self):
from tests.plugins.test_plugin import bp
Expand Down
2 changes: 1 addition & 1 deletion tests/www/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def prepare_dagruns(self):
)

def test_index(self):
with assert_queries_count(42):
with assert_queries_count(43):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea what caused an extra query?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the new menu link I added (based on me adding a second new link and it going up to 44). I don't know why FAB is doing a query per menu link, but that seems to be the behavior 🤷‍♂️.

resp = self.client.get('/', follow_redirects=True)
self.check_content_in_response('DAGs', resp)

Expand Down