From 5e567adae1c20eec6c600fc0894692e83ec3db63 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 16 Jul 2023 11:20:41 +0300 Subject: [PATCH 1/6] fix(chore): dashboard requests to database equal the number of slices it has --- superset/daos/dashboard.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/superset/daos/dashboard.py b/superset/daos/dashboard.py index 69169e1d02b3..1a75400e8e7f 100644 --- a/superset/daos/dashboard.py +++ b/superset/daos/dashboard.py @@ -56,24 +56,10 @@ class DashboardDAO(BaseDAO[Dashboard]): @classmethod def get_by_id_or_slug(cls, id_or_slug: int | str) -> Dashboard: - if is_uuid(id_or_slug): + if is_uuid(id_or_slug) or is_int(id_or_slug) or is_slug(id_or_slug): # just get dashboard if it's uuid dashboard = Dashboard.get(id_or_slug) else: - query = ( - db.session.query(Dashboard) - .filter(id_or_slug_filter(id_or_slug)) - .outerjoin(Slice, Dashboard.slices) - .outerjoin(Slice.table) - .outerjoin(Dashboard.owners) - .outerjoin(Dashboard.roles) - ) - # Apply dashboard base filters - query = cls.base_filter("id", SQLAInterface(Dashboard, db.session)).apply( - query, None - ) - dashboard = query.one_or_none() - if not dashboard: raise DashboardNotFoundError() # make sure we still have basic access check from security manager From ba140a76e2f459d99ab5d27f89a3d503a4d2939f Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 16 Jul 2023 11:55:37 +0300 Subject: [PATCH 2/6] ooops --- superset/daos/dashboard.py | 2 +- superset/models/dashboard.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/superset/daos/dashboard.py b/superset/daos/dashboard.py index 1a75400e8e7f..a666d39ad87b 100644 --- a/superset/daos/dashboard.py +++ b/superset/daos/dashboard.py @@ -41,7 +41,7 @@ from superset.dashboards.filters import DashboardAccessFilter, is_uuid from superset.extensions import db from superset.models.core import FavStar, FavStarClassName -from superset.models.dashboard import Dashboard, id_or_slug_filter +from superset.models.dashboard import Dashboard, is_int, is_slug from superset.models.embedded_dashboard import EmbeddedDashboard from superset.models.filter_set import FilterSet from superset.models.slice import Slice diff --git a/superset/models/dashboard.py b/superset/models/dashboard.py index e6b91a60d333..246defc7754b 100644 --- a/superset/models/dashboard.py +++ b/superset/models/dashboard.py @@ -463,6 +463,12 @@ def is_int(value: str | int) -> bool: return False +def is_slug(value: str) -> bool: + if type(value) == str and not is_uuid(value): + return True + return False + + def id_or_slug_filter(id_or_slug: int | str) -> BinaryExpression: if is_int(id_or_slug): return Dashboard.id == int(id_or_slug) From 3e7b9bfc725d1b17f9d5fd2bab9f688a7d4e60e8 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 16 Jul 2023 12:48:01 +0300 Subject: [PATCH 3/6] fix(chore): simplify getting dashboard in get_by_id_or_slug --- superset/daos/dashboard.py | 10 ++++------ superset/models/dashboard.py | 6 ------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/superset/daos/dashboard.py b/superset/daos/dashboard.py index a666d39ad87b..1942da7a68fc 100644 --- a/superset/daos/dashboard.py +++ b/superset/daos/dashboard.py @@ -38,10 +38,10 @@ OWNER_ID_FIELD, OWNER_TYPE_FIELD, ) -from superset.dashboards.filters import DashboardAccessFilter, is_uuid +from superset.dashboards.filters import DashboardAccessFilter from superset.extensions import db from superset.models.core import FavStar, FavStarClassName -from superset.models.dashboard import Dashboard, is_int, is_slug +from superset.models.dashboard import Dashboard from superset.models.embedded_dashboard import EmbeddedDashboard from superset.models.filter_set import FilterSet from superset.models.slice import Slice @@ -56,10 +56,8 @@ class DashboardDAO(BaseDAO[Dashboard]): @classmethod def get_by_id_or_slug(cls, id_or_slug: int | str) -> Dashboard: - if is_uuid(id_or_slug) or is_int(id_or_slug) or is_slug(id_or_slug): - # just get dashboard if it's uuid - dashboard = Dashboard.get(id_or_slug) - else: + dashboard = Dashboard.get(id_or_slug) + if dashboard is None: raise DashboardNotFoundError() # make sure we still have basic access check from security manager diff --git a/superset/models/dashboard.py b/superset/models/dashboard.py index 246defc7754b..e6b91a60d333 100644 --- a/superset/models/dashboard.py +++ b/superset/models/dashboard.py @@ -463,12 +463,6 @@ def is_int(value: str | int) -> bool: return False -def is_slug(value: str) -> bool: - if type(value) == str and not is_uuid(value): - return True - return False - - def id_or_slug_filter(id_or_slug: int | str) -> BinaryExpression: if is_int(id_or_slug): return Dashboard.id == int(id_or_slug) From d1f8af9c2d35328cba9bb4c84787cb8225e9ec02 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 16 Jul 2023 12:55:09 +0300 Subject: [PATCH 4/6] removed unused --- superset/daos/dashboard.py | 1 - 1 file changed, 1 deletion(-) diff --git a/superset/daos/dashboard.py b/superset/daos/dashboard.py index 1942da7a68fc..c0348105ba76 100644 --- a/superset/daos/dashboard.py +++ b/superset/daos/dashboard.py @@ -23,7 +23,6 @@ from flask import g from flask_appbuilder.models.sqla import Model -from flask_appbuilder.models.sqla.interface import SQLAInterface from sqlalchemy.exc import SQLAlchemyError from superset import security_manager From 7068e107738bd236b11b8d1ce5362ba6e130fedd Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 16 Jul 2023 16:07:55 +0300 Subject: [PATCH 5/6] fix(chore): return back base filter in dashboard get_by_id_or_slug & removed joining slices --- superset/daos/dashboard.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/superset/daos/dashboard.py b/superset/daos/dashboard.py index c0348105ba76..e63f3c93c2a8 100644 --- a/superset/daos/dashboard.py +++ b/superset/daos/dashboard.py @@ -23,6 +23,7 @@ from flask import g from flask_appbuilder.models.sqla import Model +from flask_appbuilder.models.sqla.interface import SQLAInterface from sqlalchemy.exc import SQLAlchemyError from superset import security_manager @@ -37,10 +38,10 @@ OWNER_ID_FIELD, OWNER_TYPE_FIELD, ) -from superset.dashboards.filters import DashboardAccessFilter +from superset.dashboards.filters import DashboardAccessFilter, is_uuid from superset.extensions import db from superset.models.core import FavStar, FavStarClassName -from superset.models.dashboard import Dashboard +from superset.models.dashboard import Dashboard, id_or_slug_filter from superset.models.embedded_dashboard import EmbeddedDashboard from superset.models.filter_set import FilterSet from superset.models.slice import Slice @@ -55,8 +56,22 @@ class DashboardDAO(BaseDAO[Dashboard]): @classmethod def get_by_id_or_slug(cls, id_or_slug: int | str) -> Dashboard: - dashboard = Dashboard.get(id_or_slug) - if dashboard is None: + if is_uuid(id_or_slug): + # just get dashboard if it's uuid + dashboard = Dashboard.get(id_or_slug) + else: + query = ( + db.session.query(Dashboard) + .filter(id_or_slug_filter(id_or_slug)) + .outerjoin(Dashboard.owners) + .outerjoin(Dashboard.roles) + ) + # Apply dashboard base filters + query = DashboardAccessFilter("id", SQLAInterface(Dashboard, db.session)).apply( + query, None + ) + dashboard = query.one_or_none() + if not dashboard: raise DashboardNotFoundError() # make sure we still have basic access check from security manager From 219310ecc1712b245471acfef8854cb37e765a36 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 16 Jul 2023 16:19:09 +0300 Subject: [PATCH 6/6] pylint --- superset/daos/dashboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset/daos/dashboard.py b/superset/daos/dashboard.py index e63f3c93c2a8..61229d057065 100644 --- a/superset/daos/dashboard.py +++ b/superset/daos/dashboard.py @@ -67,7 +67,7 @@ def get_by_id_or_slug(cls, id_or_slug: int | str) -> Dashboard: .outerjoin(Dashboard.roles) ) # Apply dashboard base filters - query = DashboardAccessFilter("id", SQLAInterface(Dashboard, db.session)).apply( + query = cls.base_filter("id", SQLAInterface(Dashboard, db.session)).apply( query, None ) dashboard = query.one_or_none()