def render_widget(widget_instance): """ Django custom template tag used to render widgets. The rendering is done using the template defined in the associated widget type. If the widget type implements get_context_data then that is used to generate the template context. """ widget = None # Get settings, if any if widget_instance.settings: settings = widget_instance.settings else: settings = {} # Get widget try: widget = get_widget(widget_instance.widget_type) except MissingWidgetType: template_name = 'dashboard/widget-error.html' context = {} context['error'] = _('Unknown widget type %(widget_type)s') % { 'widget_type': widget_instance.widget_type } context['empty_type'] = widget_instance.widget_type if widget: # Get template try: template_name = widget.template_name except AttributeError: template_name = 'dashboard/widget-error.html' context = {} context['error'] = _('Missing template for %(widget_type)s') % { 'widget_type': widget_instance.widget_type } widget = None if widget: # Get context try: context = widget.get_context_data(**settings) except AttributeError: context = {} except WidgetError as e: template_name = 'dashboard/widget-error.html' context = {} context['error'] = str(e) except Exception as e: logger.exception('Error while fetching widget context data: %s', e) template_name = 'dashboard/widget-error.html' context = {} context['error'] = _('Widget error. See error logs.') return render_to_string(template_name, context)
def get_context_data(self, **kwargs): """ Get the list of widgets for this dashboard, and organise them in row/columns to be displayed by the template. """ context = super(DashboardView, self).get_context_data(**kwargs) or {} # Get dashboard if 'name' in self.kwargs and self.kwargs['name']: name = self.kwargs['name'] else: name = 'main' dashboard = Dashboard.objects.get(name=name) context['name'] = dashboard.name # Get widgets and sort them by row widgets = dashboard.widgetinstance_set.all().order_by('row', 'column') context['rows'] = [] current_row = [] current_row_number = None for widget in widgets: if current_row_number is None: current_row_number = widget.row elif current_row_number != widget.row: context['rows'].append(current_row) current_row_number = widget.row current_row = [] current_row.append(widget) if len(current_row) > 0: context['rows'].append(current_row) # Ensure we have all the javascript & css dependencies require_assets('dashboard/dashboard.css') for widget in widgets: try: widget_type = get_widget(widget.widget_type) except MissingWidgetType: continue if hasattr(widget_type, 'javascript'): require_assets(*widget_type.javascript) if hasattr(widget_type, 'css'): require_assets(*widget_type.css) # Return the context return context
def test_registering_twice_overrides_existing_widget(self): test_widget = TestWidget() register_widget('test-widget', test_widget) test_widget_2 = TestWidget() register_widget('test-widget', test_widget_2) self.assertEqual(get_widget('test-widget'), test_widget_2)
def test_widget_is_registered(self): test_widget = TestWidget() register_widget('test-widget', test_widget) self.assertEqual(get_widget('test-widget'), test_widget)