Exemple #1
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)
Exemple #2
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)