From d22c8a8ef4f31a42108585f764a60ccf86c67faa Mon Sep 17 00:00:00 2001 From: esensar Date: Wed, 10 Oct 2018 22:19:13 +0200 Subject: [PATCH] Add delete dashboard route --- app/api/resources/dashboard.py | 9 +++++++++ .../resources/swagger/delete_dashboard_spec.yaml | 13 +++++++++++++ app/dashboards/api.py | 11 +++++++++++ app/dashboards/models.py | 7 +++++++ 4 files changed, 40 insertions(+) create mode 100644 app/api/resources/swagger/delete_dashboard_spec.yaml diff --git a/app/api/resources/dashboard.py b/app/api/resources/dashboard.py index 55f308b..50a5245 100644 --- a/app/api/resources/dashboard.py +++ b/app/api/resources/dashboard.py @@ -35,6 +35,15 @@ class DashboardResource(ProtectedResource): if success: return '', 204 + @swag_from('swagger/delete_dashboard_spec.yaml') + def delete(self, dashboard_id): + requested_dashboard = dashboard.get_dashboard(dashboard_id) + if requested_dashboard.account_id != g.current_account.id: + abort(403, message='You are not allowed to access this dashboard', + status='error') + dashboard.delete_dashboard(dashboard_id) + return '', 204 + class DashboardListResource(ProtectedResource): @use_args(DashboardSchema(), locations=('json',)) diff --git a/app/api/resources/swagger/delete_dashboard_spec.yaml b/app/api/resources/swagger/delete_dashboard_spec.yaml new file mode 100644 index 0000000..cf877a0 --- /dev/null +++ b/app/api/resources/swagger/delete_dashboard_spec.yaml @@ -0,0 +1,13 @@ +Deletes a dashboard +--- +tags: + - Dashboard +parameters: + - in: path + name: dashboard_id + required: true + type: integer + description: Id of the dashboard +responses: + 204: + description: Success diff --git a/app/dashboards/api.py b/app/dashboards/api.py index e35e809..390c80f 100644 --- a/app/dashboards/api.py +++ b/app/dashboards/api.py @@ -43,6 +43,17 @@ def update_dashboard(dashboard_id, dashboard_data): dashboard.save() +def delete_dashboard(dashboard_id): + """ + Tries to delete dashboard with given id + + :param dashboard_id: Id of requested dashboard + :type name: int + """ + dashboard = Dashboard.get(id=dashboard_id) + dashboard.delete() + + def get_dashboards(account_id): """ Tries to fetch dashboards owned by account with given id diff --git a/app/dashboards/models.py b/app/dashboards/models.py index b389105..90ec9d0 100644 --- a/app/dashboards/models.py +++ b/app/dashboards/models.py @@ -29,6 +29,13 @@ class Dashboard(db.Model): db.session.add(self) db.session.commit() + def delete(self): + """ + Deletes this dashboard from database + """ + db.session.delete(self) + db.session.commit() + @staticmethod def exists_with_any_of(**kwargs): """