Пример #1
0
    def test_04_active_layout_render_for_view(self):
        """
        Test active layout render (`render_for_view`).
        """
        try:
            # Create dashboard user
            create_dashboard_user()
        except:
            pass

        # Getting the admin (user with plugin data)
        user = User.objects.get(username=DASH_TEST_USER_USERNAME)

        # Faking the Django request
        request_factory = RequestFactory()
        request = request_factory.get("/dashboard/", data={"user": user})
        request.user = user
        workspace = None

        # 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]

        layout = get_layout(as_instance=True)

        # Fetching all dashboard entries for user and freezeing the 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")[:]
        )

        res = layout.render_for_view(dashboard_entries=dashboard_entries, request=request)
        return res
Пример #2
0
    def test_04_active_layout_render_for_view(self):
        """
        Test active layout render (`render_for_view`).
        """
        try:
            # Create dashboard user
            create_dashboard_user()
        except:
            pass
        User = get_user_model()
        # Getting the admin (user with plugin data)
        user = User.objects.get(username=DASH_TEST_USER_USERNAME)

        # Faking the Django request
        request_factory = RequestFactory()
        request = request_factory.get('/dashboard/', data={'user': user})
        request.user = user
        workspace = None

        # 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]

        layout = get_layout(as_instance=True)

        # Fetching all dashboard entries for user and freezeing the 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')[:]

        res = layout.render_for_view(dashboard_entries=dashboard_entries,
                                     request=request)
        return res
Пример #3
0
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))
Пример #4
0
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))
Пример #5
0
def plugin_widgets(request, placeholder_uid, workspace=None, position=None, \
                   template_name='dash/plugin_widgets.html', \
                   template_name_ajax='dash/plugin_widgets_ajax.html'):
    """
    Plugin widgets view. Lists all the widgets for the placeholder and
    workspace given.

    :param django.http.HttpRequest request:
    :param string placeholder_uid: Placeholder UID.
    :param int position: Position on the dashboard to which the widget is to
        be added.
    :param string template_name:
    :param string template_name_ajax: Tempalte used for AJAX requests.
    :return django.http.HttpResponse:
    """
    # 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
        )
    placeholder = layout.get_placeholder(placeholder_uid)

    if not validate_placeholder_uid(layout, placeholder_uid):
        raise Http404(_("Invalid placeholder: {0}").format(placeholder_uid))

    occupied_cells = build_cells_matrix(
        request.user,
        layout,
        placeholder,
        workspace = workspace
        )

    # Here we checking if clipboard contains a plugin which is suitable for
    # being pasted into the cell given.
    paste_from_clipboard_url = None

    # First get the clipboard data.
    clipboard_plugin_data = get_plugin_data_from_clipboard(request, layout.uid)

    # If clipboard data is not empty, check if the data is suitable for
    # being pasted into the position given.
    if clipboard_plugin_data:
        can_paste_from_clipboard = can_paste_entry_from_clipboard(
            request = request,
            layout = layout,
            placeholder_uid = placeholder_uid,
            position = position,
            workspace = workspace
            )

        if can_paste_from_clipboard:
            kwargs = {
                'placeholder_uid': placeholder_uid,
                'position': position
            }
            if workspace:
                kwargs.update({'workspace': workspace,})

            paste_from_clipboard_url = reverse(
                'dash.paste_dashboard_entry',
                kwargs = kwargs
                )

    if int(position) in occupied_cells:
        registered_plugins = get_user_plugins(request.user)
        user_plugin_uids = [uid for uid, repr in registered_plugins]

        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)\
                                                                        .filter(placeholder_uid=placeholder.uid)\
                                                                        .order_by('placeholder_uid', 'position')[:]

        for dashboard_entry in dashboard_entries:
            if int(position) == dashboard_entry.position:
                occupied_cells = get_occupied_cells(
                                layout,
                                placeholder,
                                dashboard_entry.plugin_uid,
                                dashboard_entry.position
                                )

        context = {
            'layout': layout,
            'grouped_widgets': get_widgets(
                layout,
                placeholder,
                request.user,
                workspace = workspace,
                position = position,
                occupied_cells = occupied_cells
                ),
            'dashboard_settings': dashboard_settings,
            'paste_from_clipboard_url': paste_from_clipboard_url,
        }

    else:
        context = {
            'layout': layout,
            'grouped_widgets': get_widgets(
                layout,
                placeholder,
                request.user,
                workspace = workspace,
                position = position,
                occupied_cells = occupied_cells
                ),
            'dashboard_settings': dashboard_settings,
            'paste_from_clipboard_url': paste_from_clipboard_url,
        }
    if request.is_ajax():
        template_name = layout.plugin_widgets_template_name_ajax
    return render_to_response(
        template_name,
        context,
        context_instance = RequestContext(request)
        )
Пример #6
0
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)
        )
Пример #7
0
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)