From 00d58490a5fdbd201fc4af0f601cecb94d509fbb Mon Sep 17 00:00:00 2001 From: Shinsuke Nara Date: Tue, 17 Oct 2017 17:48:05 +0900 Subject: [PATCH] Implement dashboard deletion REST API. --- redash/handlers/api.py | 1 + redash/handlers/dashboards.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/redash/handlers/api.py b/redash/handlers/api.py index 6de8802c57..de74942f33 100644 --- a/redash/handlers/api.py +++ b/redash/handlers/api.py @@ -82,6 +82,7 @@ def json_representation(data, code, headers=None): api.add_org_resource(DashboardResource, '/api/dashboards/', endpoint='dashboard') api.add_org_resource(PublicDashboardResource, '/api/dashboards/public/', endpoint='public_dashboard') api.add_org_resource(DashboardShareResource, '/api/dashboards//share', endpoint='dashboard_share') +api.add_org_resource(DashboardUserResource, '/api/dashboards/user/', endpoint='dashboard_user_id') api.add_org_resource(DataSourceTypeListResource, '/api/data_sources/types', endpoint='data_source_types') api.add_org_resource(DataSourceListResource, '/api/data_sources', endpoint='data_sources') diff --git a/redash/handlers/dashboards.py b/redash/handlers/dashboards.py index 7fe147b5aa..88cf0eec54 100644 --- a/redash/handlers/dashboards.py +++ b/redash/handlers/dashboards.py @@ -12,6 +12,7 @@ from redash.security import csp_allows_embeding from redash.serializers import serialize_dashboard from sqlalchemy.orm.exc import StaleDataError +from sqlalchemy.exc import IntegrityError # Ordering map for relationships @@ -351,3 +352,18 @@ def get(self): }) return response + + +class DashboardUserResource(BaseResource): + @require_admin + def delete(self, user_id): + dashboards = models.Dashboard.query.filter(models.Dashboard.user_id == user_id) + id = [dashboard.id for dashboard in dashboards] + models.Widget.query.filter(models.Widget.dashboard_id.in_(id)).delete(synchronize_session='fetch') + dashboards.delete() + try: + models.db.session.commit() + except IntegrityError as e: + abort(500) + + return Response(status = 204, content_type = "")