Пример #1
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)

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

    if request.is_ajax():
        template_name = template_name_ajax
    return render_to_response(template_name, context, context_instance=RequestContext(request))
Пример #2
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)
        )
Пример #3
0
def add_dashboard_entry(request, placeholder_uid, plugin_uid, workspace=None, \
                        position=None, \
                        template_name='dash/add_dashboard_entry.html', \
                        template_name_ajax='dash/add_dashboard_entry_ajax.html'):
    """
    Add dashboard entry.

    :param django.http.HttpRequest request:
    :param string placeholder_uid: Placeholder UID.
    :param string plugin_uid: Plugin UID.
    :param string workspace: Workspace slug.
    :param int position: If given, provided as position for the
        plugin (conflict resolution should take place).
    :param string template_name:
    :param string template_name_ajax: Template 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
        )

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

    if not validate_plugin_uid(plugin_uid):
        raise Http404(_("Invalid plugin name: {0}").format(plugin_uid))

    plugin = plugin_registry.get(plugin_uid)(layout.uid, placeholder_uid)
    plugin.request = request

    if plugin.add_form_template:
        template_name = plugin.add_form_template

    # Template context
    context = {
        'layout': layout,
        'dashboard_settings': dashboard_settings
    }

    obj = DashboardEntry()
    obj.layout_uid = layout.uid
    obj.placeholder_uid = placeholder_uid
    obj.plugin_uid = plugin_uid
    obj.user = request.user

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

    # If plugin has form, it is configurable which means we have to load the
    # plugin form and validate user input.
    plugin_form = plugin.get_form()
    if plugin_form:
        # If POST request and form data is valid, save the data and redirect
        # to the dashboard edit.
        if 'POST' == request.method:
            form = plugin.get_initialised_create_form_or_404(
                data=request.POST, files=request.FILES
                )
            if form.is_valid():
                # Saving the plugin form data.
                form.save_plugin_data(request=request)

                # Getting the plugin data.
                obj.plugin_data = form.get_plugin_data(request=request)

                # Handling the workspace.
                workspace_id = None
                obj.workspace = workspace_id
                if workspace:
                    workspace_slug = slugify_workspace(workspace)
                    workspace_id = DashboardWorkspace.objects.get(name=workspace).pk
                    try:
                        obj.workspace = DashboardWorkspace._default_manager.get(
                            slug = workspace_slug,
                            user = request.user,
                            layout_uid = layout.uid
                            )
                    except ObjectDoesNotExist as e:
                        messages.info(
                            request,
                            _('The workspace with slug "{0}" does not belong to '
                              'layout "{1}".').format(workspace_slug, layout.name)
                            )
                        return redirect('dash.edit_dashboard')

                # If position given, use it.
                try:
                    position = int(position)
                except Exception as e:
                    position = None

                if position:
                    obj.position = position

                # Save the object.
                obj.save()
                if int(position) in occupied_cells:
                    if not DashboardEntry.objects.filter(position=int(position), layout_uid=layout.uid,
                                                         workspace_id=workspace_id,
                                                         plugin_uid__startswith='WidgetSwitcher').exists():
                        pattern = re.compile(r'\d+x\d+')
                        dimensions = pattern.search(str(plugin_uid))
                        switcher = DashboardEntry()
                        switcher.layout_uid = layout.uid
                        switcher.placeholder_uid = placeholder_uid
                        switcher.plugin_uid = 'WidgetSwitcher_'+dimensions.group()
                        switcher.user = request.user
                        switcher.plugin_data = json.dumps({"timer": 10})
                        switcher.position = int(position)
                        switcher.workspace = None
                        if workspace:
                            switcher.workspace = DashboardWorkspace._default_manager.get(
                                    slug=workspace_slug,
                                    user=request.user,
                                    layout_uid=layout.uid
                                    )
                        switcher.save()

                messages.info(request, _('The dashboard widget "{0}" was added '
                                         'successfully.').format(plugin.name))

                # Redirect to the dashboard view.
                if obj.workspace:
                    return redirect(
                        'dash.edit_dashboard', workspace=obj.workspace.slug
                        )
                else:
                    return redirect('dash.edit_dashboard')

        # If POST but data invalid, show the form with errors.
        else:
            form = plugin.get_initialised_create_form_or_404()

        context.update(
            {'form': form, 'plugin_uid': plugin_uid, 'plugin': plugin}
            )

    # If plugin is not configurable, it's just saved as is.
    else:
        obj.save()
        return redirect('dash.edit_dashboard')

    if request.is_ajax():
        template_name = template_name_ajax

    return render_to_response(
        template_name, context, context_instance=RequestContext(request)
        )
Пример #4
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
                )

    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)
        )
Пример #5
0
def add_dashboard_entry(request, placeholder_uid, plugin_uid, workspace=None, \
                        position=None, \
                        template_name='dash/add_dashboard_entry.html', \
                        template_name_ajax='dash/add_dashboard_entry_ajax.html'):
    """
    Add dashboard entry.

    :param django.http.HttpRequest request:
    :param string placeholder_uid: Placeholder UID.
    :param string plugin_uid: Plugin UID.
    :param string workspace: Workspace slug.
    :param int position: If given, provided as position for the
        plugin (conflict resolution should take place).
    :param string template_name:
    :param string template_name_ajax: Template 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)

    if workspace:
        workspace_slug = slugify_workspace(workspace)
        filters = {
            'slug': workspace_slug,
            'user': request.user,
        }
        if not dashboard_settings.allow_different_layouts:
            filters.update({
                'layout_uid': dashboard_settings.layout_uid,
            })
        try:
            workspace = DashboardWorkspace._default_manager.get(**filters)
        except ObjectDoesNotExist as e:
            if dashboard_settings.allow_different_layouts:
                message = _('The workspace with slug "{0}" was not foundi.').format(workspace_slug)
            else:
                message = __('The workspace with slug "{0}" does not belong to '
                'layout "{1}".').format(workspace_slug, layout.name)
            messages.info(request, message)
            return redirect('dash.edit_dashboard')

    if dashboard_settings.allow_different_layouts and workspace:
        layout_uid = workspace.layout_uid
    else:
        layout_uid = dashboard_settings.layout_uid

    layout = get_layout(layout_uid=layout_uid, as_instance=True)

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

    if not validate_plugin_uid(plugin_uid):
        raise Http404(_("Invalid plugin name: {0}").format(plugin_uid))

    plugin = plugin_registry.get(plugin_uid)(layout.uid, placeholder_uid)
    plugin.request = request

    if plugin.add_form_template:
        template_name = plugin.add_form_template

    # Template context
    context = {
        'layout': layout,
        'dashboard_settings': dashboard_settings
    }

    obj = DashboardEntry()
    obj.layout_uid = layout.uid
    obj.placeholder_uid = placeholder_uid
    obj.plugin_uid = plugin_uid
    obj.user = request.user
    obj.workspace = workspace

    # If plugin has form, it is configurable which means we have to load the
    # plugin form and validate user input.
    plugin_form = plugin.get_form()
    if plugin_form:
        # If POST request and form data is valid, save the data and redirect
        # to the dashboard edit.
        if 'POST' == request.method:
            form = plugin.get_initialised_create_form_or_404(
                data=request.POST, files=request.FILES
                )
            if form.is_valid():
                # Saving the plugin form data.
                form.save_plugin_data(request=request)

                # Getting the plugin data.
                obj.plugin_data = form.get_plugin_data(request=request)

                # If position given, use it.
                try:
                    position = int(position)
                except Exception as e:
                    position = None

                if position:
                    obj.position = position

                # Save the object.
                obj.save()

                messages.info(request, _('The dashboard widget "{0}" was added '
                                         'successfully.').format(plugin.name))

                # Redirect to the dashboard view.
                if obj.workspace:
                    return redirect(
                        'dash.edit_dashboard', workspace=obj.workspace.slug
                        )
                else:
                    return redirect('dash.edit_dashboard')

        # If POST but data invalid, show the form with errors.
        else:
            form = plugin.get_initialised_create_form_or_404()

        context.update(
            {'form': form, 'plugin_uid': plugin_uid, 'plugin': plugin}
            )

    # If plugin is not configurable, it's just saved as is.
    else:
        obj.save()
        return redirect('dash.edit_dashboard')

    if request.is_ajax():
        template_name = template_name_ajax

    return render_to_response(
        template_name, context, context_instance=RequestContext(request)
        )
Пример #6
0
def paste_entry_from_clipboard(request, layout, placeholder_uid, position, \
                         workspace=None, entries=[], check_only=False, \
                         clear_clipboard=True):
    """
    Pastes entry from clipboard to the given placeholder of a workspace
    selected.

    :param django.http.HttpRequest request:
    :param str layout_uid: Placeholder UID.
    :param str placeholder_uid: Placeholder UID.
    :param int position: Position.
    :param mixed workspace: Either str or ``dash.models.DashboardWorkspace``
        instance. If str is given, a database hit occurs in order to obtain
        the ``dash.models.DashboardWorkspace`` instance. Thus, if you have the
        workspace instance already, provide it as is, in order to minify
        database hits.
    :param iterable entries: If given, entries are not fetched but the
        existing iterable is used. Each item in the iterable should be an
        instance of ``dash.models.DashboardEntry``.
    :param bool check_only: If set to True, it's only checked if it's possible
        to paste from clipboard (the ``dashboard_entry.save()`` part is
        skipped, which means that the entry is not saved in the database).
    :return tuple (str, bool): (Plugin name, boolean True) tuple on success
        and (error message, boolean False) on failure.
    """
    clipboard_plugin_data = get_plugin_data_from_clipboard(request, layout.uid)
    if not clipboard_plugin_data:
        return (_("Clipboard is empty!"), False)

    if not validate_placeholder_uid(layout, placeholder_uid):
        return (_("Invalid placeholder `{0}`.").format(placeholder_uid), False)

    user_plugin_uids = get_user_plugin_uids(request.user)

    if isinstance(workspace, DashboardWorkspace):
        workspace_obj = workspace
    else:
        if workspace:
            try:
                workspace_obj = DashboardWorkspace._default_manager \
                                                  .get(slug=workspace)
            except ObjectDoesNotExist as e:
                workspace_obj = None
        else:
            workspace_obj = None

    dashboard_entry = DashboardEntry(
        user=request.user,
        workspace=workspace_obj,
        layout_uid=layout.uid,
        placeholder_uid=placeholder_uid,
        plugin_uid=clipboard_plugin_data['plugin_uid'],
        plugin_data=clipboard_plugin_data['plugin_data'],
        position=position)

    # We should now check if we have a space for pasting the plugin. For that
    # we should get the plugin and see if there's a space available for
    # the (workspace, placeholder, user) triple given.

    # Get the plugin.
    plugin = dashboard_entry.get_plugin()

    if not plugin.uid in user_plugin_uids:
        return (_("You're not allowed to "
                  "use the {0} plugin.".format(safe_text(plugin.name))), False)

    # Getting occupied cells
    placeholder = layout.get_placeholder(placeholder_uid)
    occupied_cells = build_cells_matrix(request.user,
                                        layout,
                                        placeholder,
                                        workspace=workspace)

    # Get cells occupied by plugin widget.
    widget_occupied_cells = get_occupied_cells(layout,
                                               placeholder,
                                               plugin.uid,
                                               position,
                                               check_boundaries=True)

    if widget_occupied_cells is not False \
       and not lists_overlap(widget_occupied_cells, occupied_cells):

        try:
            if not check_only:
                # Save data
                dashboard_entry.save()

                # Clear the clipboard
                clear_clipboard_data(request, layout.uid)

            return (plugin.uid, True)
        except Exception as e:
            return (str(e), False)
Пример #7
0
def paste_entry_from_clipboard(request, layout, placeholder_uid, position, \
                         workspace=None, entries=[], check_only=False, \
                         clear_clipboard=True):
    """
    Pastes entry from clipboard to the given placeholder of a workspace
    selected.

    :param django.http.HttpRequest request:
    :param str layout_uid: Placeholder UID.
    :param str placeholder_uid: Placeholder UID.
    :param int position: Position.
    :param mixed workspace: Either str or ``dash.models.DashboardWorkspace``
        instance. If str is given, a database hit occurs in order to obtain
        the ``dash.models.DashboardWorkspace`` instance. Thus, if you have the
        workspace instance already, provide it as is, in order to minify
        database hits.
    :param iterable entries: If given, entries are not fetched but the
        existing iterable is used. Each item in the iterable should be an
        instance of ``dash.models.DashboardEntry``.
    :param bool check_only: If set to True, it's only checked if it's possible
        to paste from clipboard (the ``dashboard_entry.save()`` part is
        skipped, which means that the entry is not saved in the database).
    :return tuple (str, bool): (Plugin name, boolean True) tuple on success
        and (error message, boolean False) on failure.
    """
    clipboard_plugin_data = get_plugin_data_from_clipboard(request, layout.uid)
    if not clipboard_plugin_data:
        return (_("Clipboard is empty!"), False)

    if not validate_placeholder_uid(layout, placeholder_uid):
        return (_("Invalid placeholder `{0}`.").format(placeholder_uid), False)

    user_plugin_uids = get_user_plugin_uids(request.user)

    if isinstance(workspace, DashboardWorkspace):
        workspace_obj = workspace
    else:
        if workspace:
            try:
                workspace_obj = DashboardWorkspace._default_manager \
                                                  .get(slug=workspace)
            except ObjectDoesNotExist as e:
                workspace_obj = None
        else:
            workspace_obj = None

    dashboard_entry = DashboardEntry(
        user = request.user,
        workspace = workspace_obj,
        layout_uid = layout.uid,
        placeholder_uid = placeholder_uid,
        plugin_uid = clipboard_plugin_data['plugin_uid'],
        plugin_data = clipboard_plugin_data['plugin_data'],
        position = position
        )

    # We should now check if we have a space for pasting the plugin. For that
    # we should get the plugin and see if there's a space available for
    # the (workspace, placeholder, user) triple given.

    # Get the plugin.
    plugin = dashboard_entry.get_plugin()

    if not plugin.uid in user_plugin_uids:
        return (_("You're not allowed to "
                  "use the {0} plugin.".format(safe_text(plugin.name))), False)

    # Getting occupied cells
    placeholder = layout.get_placeholder(placeholder_uid)
    occupied_cells = build_cells_matrix(
        request.user,
        layout,
        placeholder,
        workspace = workspace
        )

    # Get cells occupied by plugin widget.
    widget_occupied_cells = get_occupied_cells(
        layout, placeholder, plugin.uid, position, check_boundaries=True
        )

    if widget_occupied_cells is not False \
       and not lists_overlap(widget_occupied_cells, occupied_cells):

        try:
            if not check_only:
                # Save data
                dashboard_entry.save()

                # Clear the clipboard
                clear_clipboard_data(request, layout.uid)

            return (plugin.uid, True)
        except Exception as e:
            return (str(e), False)