# TODO: either convert all requests to use slugs or ids dashboard_properties = request.get_json(force=True) dashboard = models.Dashboard.get(models.Dashboard.id == dashboard_slug) dashboard.layout = dashboard_properties['layout'] dashboard.name = dashboard_properties['name'] dashboard.save() return dashboard.to_dict(with_widgets=True) def delete(self, dashboard_slug): dashboard = models.Dashboard.get_by_slug(dashboard_slug) dashboard.is_archived = True dashboard.save() api.add_resource(DashboardListAPI, '/api/dashboards', endpoint='dashboards') api.add_resource(DashboardAPI, '/api/dashboards/<dashboard_slug>', endpoint='dashboard') class WidgetListAPI(BaseResource): def post(self): widget_properties = request.get_json(force=True) widget_properties['options'] = json.dumps(widget_properties['options']) widget_properties.pop('id', None) widget_properties['dashboard'] = widget_properties.pop('dashboard_id') widget_properties['visualization'] = widget_properties.pop( 'visualization_id') widget = models.Widget(**widget_properties) widget.save()
super(BaseResource, self).__init__(*args, **kwargs) self._user = None @property def current_user(self): return current_user._get_current_object() class EventAPI(BaseResource): def post(self): events_list = request.get_json(force=True) for event in events_list: events.record_event(event) api.add_resource(EventAPI, '/api/events', endpoint='events') class DataSourceListAPI(BaseResource): def get(self): data_sources = [ds.to_dict() for ds in models.DataSource.select()] return data_sources api.add_resource(DataSourceListAPI, '/api/data_sources', endpoint='data_sources') class DashboardListAPI(BaseResource): def get(self): dashboards = [d.to_dict() for d in models.Dashboard.select().where(models.Dashboard.is_archived==False)]
@require_permission('admin_groups') def get(self): source = models.DataSource.select().where( models.DataSource.type == "pg")[0] qr = data.query_runner.get_query_runner(source.type, source.options) tablenames = qr( "SELECT table_name FROM information_schema.tables WHERE table_schema='public' ORDER BY table_name" ) result = {} result["tablenames"] = [ t["table_name"] for t in json.loads(tablenames[0])["rows"] ] return result api.add_resource(TableAPI, '/api/tables', endpoint='tables') class GroupListAPI(BaseResource): @require_permission('admin_groups') def get(self): groups = [g.to_dict() for g in models.Group.select()] return groups @require_permission('admin_groups') def post(self): json = request.get_json(force=True) g = models.Group(name=json['name'], tables=json["tables"], permissions=json["permissions"]) g.save()
return response class TableAPI(BaseResource): @require_permission('admin_groups') def get(self): source = models.DataSource.select().where(models.DataSource.type == "pg")[0] qr = data.query_runner.get_query_runner(source.type, source.options) tablenames = qr("SELECT table_name FROM information_schema.tables WHERE table_schema='public' ORDER BY table_name") result = {} result["tablenames"] = [t["table_name"] for t in json.loads(tablenames[0])["rows"]] return result api.add_resource(TableAPI, '/api/tables', endpoint='tables') class GroupListAPI(BaseResource): @require_permission('admin_groups') def get(self): groups = [g.to_dict() for g in models.Group.select()] return groups @require_permission('admin_groups') def post(self): json = request.get_json(force=True) g = models.Group(name=json['name'], tables=json["tables"], permissions=json["permissions"]) g.save() return g.to_dict()