def delete(self, dashboard_id): """ Archives a dashboard. :qparam number id: Id of dashboard to retrieve. Responds with the archived :ref:`dashboard <dashboard-response-label>`. """ dashboard = models.Dashboard.get_by_id_and_org(dashboard_id, self.current_org) dashboard.is_archived = True dashboard.record_changes(changed_by=self.current_user) models.db.session.add(dashboard) d = DashboardSerializer(dashboard, with_widgets=True, user=self.current_user).serialize() models.db.session.commit() self.record_event({ "action": "archive", "object_id": dashboard.id, "object_type": "dashboard" }) return d
def post(self, dashboard_id): """ Modifies a dashboard. :qparam number id: Id of dashboard to retrieve. Responds with the updated :ref:`dashboard <dashboard-response-label>`. :status 200: success :status 409: Version conflict -- dashboard modified since last read """ dashboard_properties = request.get_json(force=True) # TODO: either convert all requests to use slugs or ids dashboard = models.Dashboard.get_by_id_and_org(dashboard_id, self.current_org) require_object_modify_permission(dashboard, self.current_user) updates = project( dashboard_properties, ( "name", "layout", "version", "tags", "is_draft", "is_archived", "dashboard_filters_enabled", "options", ), ) # SQLAlchemy handles the case where a concurrent transaction beats us # to the update. But we still have to make sure that we're not starting # out behind. if "version" in updates and updates["version"] != dashboard.version: abort(409) updates["changed_by"] = self.current_user self.update_model(dashboard, updates) models.db.session.add(dashboard) try: models.db.session.commit() except StaleDataError: abort(409) result = DashboardSerializer(dashboard, with_widgets=True, user=self.current_user).serialize() self.record_event({ "action": "edit", "object_id": dashboard.id, "object_type": "dashboard" }) return result
def get(self, dashboard_slug=None): """ Retrieves a dashboard. :qparam string slug: Slug of dashboard to retrieve. .. _dashboard-response-label: :>json number id: Dashboard ID :>json string name: :>json string slug: :>json number user_id: ID of the dashboard creator :>json string created_at: ISO format timestamp for dashboard creation :>json string updated_at: ISO format timestamp for last dashboard modification :>json number version: Revision number of dashboard :>json boolean dashboard_filters_enabled: Whether filters are enabled or not :>json boolean is_archived: Whether this dashboard has been removed from the index or not :>json boolean is_draft: Whether this dashboard is a draft or not. :>json array layout: Array of arrays containing widget IDs, corresponding to the rows and columns the widgets are displayed in :>json array widgets: Array of arrays containing :ref:`widget <widget-response-label>` data .. _widget-response-label: Widget structure: :>json number widget.id: Widget ID :>json number widget.width: Widget size :>json object widget.options: Widget options :>json number widget.dashboard_id: ID of dashboard containing this widget :>json string widget.text: Widget contents, if this is a text-box widget :>json object widget.visualization: Widget contents, if this is a visualization widget :>json string widget.created_at: ISO format timestamp for widget creation :>json string widget.updated_at: ISO format timestamp for last widget modification """ dashboard = get_object_or_404( models.Dashboard.get_by_slug_and_org, dashboard_slug, self.current_org ) response = DashboardSerializer( dashboard, with_widgets=True, user=self.current_user ).serialize() api_key = models.ApiKey.get_by_object(dashboard) if api_key: response["public_url"] = url_for( "redash.public_dashboard", token=api_key.api_key, org_slug=self.current_org.slug, _external=True, ) response["api_key"] = api_key.api_key response["can_edit"] = can_modify(dashboard, self.current_user) self.record_event( {"action": "view", "object_id": dashboard.id, "object_type": "dashboard"} ) return response
def post(self): """ Creates a new dashboard. :<json string name: Dashboard name Responds with a :ref:`dashboard <dashboard-response-label>`. """ dashboard_properties = request.get_json(force=True) dashboard = models.Dashboard( name=dashboard_properties["name"], org=self.current_org, user=self.current_user, is_draft=True, layout="[]", ) models.db.session.add(dashboard) models.db.session.commit() return DashboardSerializer(dashboard).serialize()