def to_dict(self, with_widgets=False, user=None): layout = json.loads(self.layout) if with_widgets: widget_list = Widget.select(Widget, Visualization, Query, User)\ .where(Widget.dashboard == self.id)\ .join(Visualization, join_type=peewee.JOIN_LEFT_OUTER)\ .join(Query, join_type=peewee.JOIN_LEFT_OUTER)\ .join(User, join_type=peewee.JOIN_LEFT_OUTER) widgets = {} for w in widget_list: if w.visualization_id is None: widgets[w.id] = w.to_dict() elif user and has_access(w.visualization.query.groups, user, view_only): widgets[w.id] = w.to_dict() else: widgets[w.id] = project( w.to_dict(), ('id', 'width', 'dashboard_id', 'options', 'created_at', 'updated_at')) widgets[w.id]['restricted'] = True # The following is a workaround for cases when the widget object gets deleted without the dashboard layout # updated. This happens for users with old databases that didn't have a foreign key relationship between # visualizations and widgets. # It's temporary until better solution is implemented (we probably should move the position information # to the widget). widgets_layout = [] for row in layout: new_row = [] for widget_id in row: widget = widgets.get(widget_id, None) if widget: new_row.append(widget) widgets_layout.append(new_row) # widgets_layout = map(lambda row: map(lambda widget_id: widgets.get(widget_id, None), row), layout) else: widgets_layout = None return { 'id': self.id, 'slug': self.slug, 'name': self.name, 'user_id': self.user_id, 'layout': layout, 'dashboard_filters_enabled': self.dashboard_filters_enabled, 'widgets': widgets_layout, 'is_archived': self.is_archived, 'updated_at': self.updated_at, 'created_at': self.created_at }
def to_dict(self, with_widgets=False, user=None): layout = json.loads(self.layout) if with_widgets: widget_list = Widget.select(Widget, Visualization, Query, User)\ .where(Widget.dashboard == self.id)\ .join(Visualization, join_type=peewee.JOIN_LEFT_OUTER)\ .join(Query, join_type=peewee.JOIN_LEFT_OUTER)\ .join(User, join_type=peewee.JOIN_LEFT_OUTER) widgets = {} for w in widget_list: if w.visualization_id is None: widgets[w.id] = w.to_dict() elif user and has_access(w.visualization.query.groups, user, view_only): widgets[w.id] = w.to_dict() else: widgets[w.id] = project(w.to_dict(), ('id', 'width', 'dashboard_id', 'options', 'created_at', 'updated_at')) widgets[w.id]['restricted'] = True # The following is a workaround for cases when the widget object gets deleted without the dashboard layout # updated. This happens for users with old databases that didn't have a foreign key relationship between # visualizations and widgets. # It's temporary until better solution is implemented (we probably should move the position information # to the widget). widgets_layout = [] for row in layout: new_row = [] for widget_id in row: widget = widgets.get(widget_id, None) if widget: new_row.append(widget) widgets_layout.append(new_row) # widgets_layout = map(lambda row: map(lambda widget_id: widgets.get(widget_id, None), row), layout) else: widgets_layout = None return { 'id': self.id, 'slug': self.slug, 'name': self.name, 'user_id': self.user_id, 'layout': layout, 'groups': self.groups, 'dashboard_filters_enabled': self.dashboard_filters_enabled, 'widgets': widgets_layout, 'is_archived': self.is_archived, 'updated_at': self.updated_at, 'created_at': self.created_at }