def edit_dashboard(request, workspace=None): """ Edit dashboard. :param django.http.HttpRequest request: :param string workspace: Workspace slug. If given, the workspace loaded. Otherwise we deal with no workspace. :return django.http.HttpResponse: """ # Getting the list of plugins that user is allowed to use. registered_plugins = get_user_plugins(request.user) user_plugin_uids = [uid for uid, repr in registered_plugins] # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_or_create_dashboard_settings(request.user) layout = get_layout(layout_uid=dashboard_settings.layout_uid, as_instance=True) # Getting the (frozen) queryset. dashboard_entries = DashboardEntry._default_manager \ .get_for_user(user=request.user, layout_uid=layout.uid, workspace=workspace) \ .select_related('workspace', 'user') \ .filter(plugin_uid__in=user_plugin_uids) \ .order_by('placeholder_uid', 'position')[:] placeholders = layout.get_placeholder_instances(dashboard_entries, workspace=workspace, request=request) layout.collect_widget_media(dashboard_entries) context = { 'placeholders': placeholders, 'placeholders_dict': iterable_to_dict(placeholders, key_attr_name='uid'), 'css': layout.get_css(placeholders), 'layout': layout, 'edit_mode': True, 'dashboard_settings': dashboard_settings } workspaces = get_workspaces(request.user, layout.uid, workspace) # If workspace with slug given is not found in the list of workspaces # redirect to the default dashboard. if workspaces['current_workspace_not_found']: messages.info( request, _('The workspace with slug "{0}" does not belong to layout "{1}".').format(workspace, layout.name) ) return redirect('dash.edit_dashboard') context.update(workspaces) context.update({'public_dashboard_url': get_public_dashboard_url(dashboard_settings)}) template_name = layout.get_edit_template_name(request) return render_to_response(template_name, context, context_instance=RequestContext(request))
def render_for_edit(self, dashboard_entries=None, workspace=None, request=None): """ Renders the layout. NOTE: This is not used at the moment. You most likely want the `dash.views.edit_dashboard` view. :param iterable dashboard_entries: :param string workspace: Current workspace. :param django.http.HttpRequest request: :return string: """ placeholders = self.get_placeholder_instances(dashboard_entries, workspace, request) context = { "placeholders": placeholders, "placeholders_dict": iterable_to_dict(placeholders, key_attr_name="uid"), "request": request, "css": self.get_css(placeholders), } return render_to_string(self.get_edit_template_name(request), context)
def render_for_view(self, dashboard_entries=None, workspace=None, request=None): """ Renders the layout. NOTE: This is not used at the moment. You most likely want the `dash.views.dashboard` view. :param iterable dashboard_entries: :param string workspace: Current workspace. :param django.http.HttpRequest request: :return string: """ placeholders = self.get_placeholder_instances(dashboard_entries, workspace, request) context = { 'placeholders': placeholders, 'placeholders_dict': iterable_to_dict(placeholders, key_attr_name='uid'), 'request': request, 'css': self.get_css(placeholders) } return render_to_string(self.get_view_template_name(request), context)
def public_dashboard(request, username, workspace=None, template_name='public_dashboard/public_dashboard.html'): """ Public dashboard. :param django.http.HttpRequest: :param string username: :param string workspace: Workspace slug. :param string template_name: :return django.http.HttpResponse: """ # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_dashboard_settings(username) if dashboard_settings: layout = get_layout(layout_uid=dashboard_settings.layout_uid, as_instance=True) user = dashboard_settings.user else: raise Http404 # Getting the list of plugins that user is allowed to use. registered_plugins = get_user_plugins(user) user_plugin_uids = [uid for uid, repr in registered_plugins] logger.debug(user_plugin_uids) # A complex query required. All entries shall be taken from default dashboard (no workspace) and # joined with all entries of workspaces set to be public. Getting the (frozen) queryset. if workspace: entries_q = Q( user = user, layout_uid = layout.uid, workspace__slug = workspace, workspace__is_public = True, plugin_uid__in = user_plugin_uids ) else: entries_q = Q(user=user, layout_uid=layout.uid, workspace=None) dashboard_entries = DashboardEntry._default_manager \ .filter(entries_q) \ .select_related('workspace', 'user') \ .order_by('placeholder_uid', 'position')[:] #logger.debug(dashboard_entries) placeholders = layout.get_placeholder_instances(dashboard_entries, request=request) layout.collect_widget_media(dashboard_entries) context = { 'placeholders': placeholders, 'placeholders_dict': iterable_to_dict(placeholders, key_attr_name='uid'), 'css': layout.get_css(placeholders), 'layout': layout, 'user': user, 'master_template': layout.get_view_template_name(request, origin='dash.public_dashboard'), 'dashboard_settings': dashboard_settings } workspaces = get_workspaces(user, layout.uid, workspace, public=True) # If workspace with slug given is not found in the list of workspaces # redirect to the default dashboard. if workspaces['current_workspace_not_found']: messages.info( request, _('The workspace with slug "{0}" does not exist.').format(workspace) ) return redirect('dash.public_dashboard', username=username) context.update(workspaces) return render_to_response(template_name, context, context_instance=RequestContext(request))
def dashboard(request, workspace=None): """ Dashboard. :param django.http.HttpRequest request: :param string workspace: Workspace slug. If given, the workspace loaded. Otherwise we deal with no workspace. :return django.http.HttpResponse: """ # Getting the list of plugins that user is allowed to use. registered_plugins = get_user_plugins(request.user) user_plugin_uids = [uid for uid, repr in registered_plugins] # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_or_create_dashboard_settings(request.user) workspaces = get_workspaces( request.user, dashboard_settings.layout_uid, workspace, different_layouts=dashboard_settings.allow_different_layouts ) layout = get_layout( layout_uid=workspaces['current_workspace'].layout_uid if workspaces['current_workspace'] else dashboard_settings.layout_uid, as_instance=True ) # If workspace with slug given is not found in the list of workspaces # redirect to the default dashboard. if workspaces['current_workspace_not_found']: msg = _('The workspace with slug "{0}" does ' 'not belong to layout "{1}".').format(workspace, layout.name) if dashboard_settings.allow_different_layouts: msg = _('The workspace with slug "{0}" does not exist').format( workspace ) messages.info(request, msg) return redirect('dash.edit_dashboard') # Getting the (frozen) queryset. dashboard_entries = DashboardEntry._default_manager \ .get_for_user(user=request.user, \ layout_uid=layout.uid, \ workspace=workspace) \ .select_related('workspace', 'user') \ .filter(plugin_uid__in=user_plugin_uids) \ .order_by('placeholder_uid', 'position')[:] placeholders = layout.get_placeholder_instances( dashboard_entries, request=request ) layout.collect_widget_media(dashboard_entries) context = { 'placeholders': placeholders, 'placeholders_dict': iterable_to_dict(placeholders, \ key_attr_name='uid'), 'css': layout.get_css(placeholders), 'layout': layout, 'dashboard_settings': dashboard_settings } context.update(workspaces) context.update( {'public_dashboard_url': get_public_dashboard_url(dashboard_settings)} ) template_name = layout.get_view_template_name(request) return render_to_response( template_name, context, context_instance=RequestContext(request) )
def public_dashboard(request, username, workspace=None, template_name='public_dashboard/public_dashboard.html'): """ Public dashboard. :param django.http.HttpRequest: :param string username: :param string workspace: Workspace slug. :param string template_name: :return django.http.HttpResponse: """ # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_dashboard_settings(username) if dashboard_settings: layout = get_layout(layout_uid=dashboard_settings.layout_uid, as_instance=True) user = dashboard_settings.user else: raise Http404 # Getting the list of plugins that user is allowed to use. registered_plugins = get_user_plugins(user) user_plugin_uids = [uid for uid, repr in registered_plugins] logger.debug(user_plugin_uids) # A complex query required. All entries shall be taken from default dashboard (no workspace) and # joined with all entries of workspaces set to be public. Getting the (frozen) queryset. if workspace: entries_q = Q(user=user, layout_uid=layout.uid, workspace__slug=workspace, workspace__is_public=True, plugin_uid__in=user_plugin_uids) else: entries_q = Q(user=user, layout_uid=layout.uid, workspace=None) dashboard_entries = DashboardEntry._default_manager \ .filter(entries_q) \ .select_related('workspace', 'user') \ .order_by('placeholder_uid', 'position')[:] #logger.debug(dashboard_entries) placeholders = layout.get_placeholder_instances(dashboard_entries, request=request) layout.collect_widget_media(dashboard_entries) context = { 'placeholders': placeholders, 'placeholders_dict': iterable_to_dict(placeholders, key_attr_name='uid'), 'css': layout.get_css(placeholders), 'layout': layout, 'user': user, 'master_template': layout.get_view_template_name(request, origin='dash.public_dashboard'), 'dashboard_settings': dashboard_settings } workspaces = get_workspaces(user, layout.uid, workspace, public=True) # If workspace with slug given is not found in the list of workspaces # redirect to the default dashboard. if workspaces['current_workspace_not_found']: messages.info( request, _('The workspace with slug "{0}" does not exist.').format( workspace)) return redirect('dash.public_dashboard', username=username) context.update(workspaces) return render(request, template_name, context)