(
    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'
)
return render(request, template_name, context)
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:
Add dashboard entry.
Add dashboard entry .
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)

    workspace_obj = None
    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_obj = DashboardWorkspace._default_manager.get(**filters)
        except ObjectDoesNotExist as e:
            if dashboard_settings.allow_different_layouts:
                message = _('The workspace with slug "{0}" was not found.'
                            '').format(workspace_slug)
            else:
                message = _(
                    'The workspace with slug "{0}" does not belong to '
                    'layout "{1}".'
                ).format(workspace_slug, get_layout(as_instance=True).name)
            messages.info(request, message)
            return redirect('dash.edit_dashboard')

    if dashboard_settings.allow_different_layouts and workspace_obj:
        layout_uid = workspace_obj.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(
            gettext("Invalid placeholder: {0}").format(placeholder_uid)
        )

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

    placeholder = layout.get_placeholder(placeholder_uid)

    # Cell that would be occupied by the plugin upon addition.
    widget_occupied_cells = get_occupied_cells(
        layout,
        placeholder,
        plugin_uid,
        position,
        check_boundaries=True,
        fail_silently=True
    )

    # Cells currently occupied in the workspace given.
    occupied_cells = build_cells_matrix(
        request.user,
        layout,
        placeholder,
        workspace
    )

    # Checking if it's still possible to insert a widget.
    if widget_occupied_cells is False \
       or lists_overlap(widget_occupied_cells, occupied_cells):

        raise Http404(gettext("Collisions detected"))

    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_obj

    # 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 request.method == 'POST':
            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:
                    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 layout.add_dashboard_entry_ajax_template_name:
        template_name_ajax = layout.add_dashboard_entry_ajax_template_name

    context.update(
        {'add_dashboard_entry_ajax_template_name': template_name_ajax}
    )

    if request.is_ajax():
        template_name = template_name_ajax
    elif layout.add_dashboard_entry_template_name:
        template_name = layout.add_dashboard_entry_template_name

    return render(request, template_name, context)
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' ) : # Getting dashboard settings for the user. Then get users' layout. dashboard_settings = get_or_create_dashboard_settings ( request . user ) workspace_obj = None 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_obj = DashboardWorkspace . _default_manager . get ( * * filters ) except ObjectDoesNotExist as e : if dashboard_settings . allow_different_layouts : message = _ ( 'The workspace with slug "{0}" was not found.' '' ) . format ( workspace_slug ) else : message = _ ( 'The workspace with slug "{0}" does not belong to ' 'layout "{1}".' ) . format ( workspace_slug , get_layout ( as_instance = True ) . name ) messages . info ( request , message ) return redirect ( 'dash.edit_dashboard' ) if dashboard_settings . allow_different_layouts and workspace_obj : layout_uid = workspace_obj . 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 ( gettext ( "Invalid placeholder: {0}" ) . format ( placeholder_uid ) ) if not validate_plugin_uid ( plugin_uid ) : raise Http404 ( gettext ( "Invalid plugin name: {0}" ) . format ( plugin_uid ) ) placeholder = layout . get_placeholder ( placeholder_uid ) # Cell that would be occupied by the plugin upon addition. widget_occupied_cells = get_occupied_cells ( layout , placeholder , plugin_uid , position , check_boundaries = True , fail_silently = True ) # Cells currently occupied in the workspace given. occupied_cells = build_cells_matrix ( request . user , layout , placeholder , workspace ) # Checking if it's still possible to insert a widget. if widget_occupied_cells is False or lists_overlap ( widget_occupied_cells , occupied_cells ) : raise Http404 ( gettext ( "Collisions detected" ) ) 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_obj # 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 request . method == 'POST' : 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 : 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 layout . add_dashboard_entry_ajax_template_name : template_name_ajax = layout . add_dashboard_entry_ajax_template_name context . update ( { 'add_dashboard_entry_ajax_template_name' : template_name_ajax } ) if request . is_ajax ( ) : template_name = template_name_ajax elif layout . add_dashboard_entry_template_name : template_name = layout . add_dashboard_entry_template_name return render ( request , template_name , context )