Creating django admin as an action for third-party users without an administrator - how

I am thinking of creating behavior similar to admin action outside of the admin. Thus, the user should be able to select objects using the checkbox, and after selecting an action with a drop-down menu, the selected action is performed for all selected objects.

I thought of the following approach.

Create a check box for each object in the .html template. The result will be this (I took it from the administrator):

<td><input type="checkbox" class="action-select" value="24" name="_selected_action" /></td> 

Then create a drop-down menu (take it from the administrator):

<div class="actions"> 
            <label>Action: <select name="action"> 
                <option value="" selected="selected">---------</option> 
                <option value="delete_selected">Delete selected contacts</option> 
            </select></label> 
            <button type="submit" class="button" title="Run the selected action" name="index" value="0">Go</button> 
        </div> 

Now I ask myself how can I match this dropdown with a python function. This is probably a function that I would place inside the model object to which I want to apply this action.

, - .

, , . , , HttpResponse.

, , , . - ?

:

, :

action_handler, , . post querydicts (request.POST. getitem ( "" )) (request.POST.getlist('_ selected_for_action')). ​​

, , action_handler.

@login_required
def action_handler(request, model):
    """
    PARAMETERS
    model = the django model upon which the actions are executed

    RETURN VALUES
    0 - POST param action is not available
      - POST param selected_action is not defined
      - selected_ids is empty
      - object_list is empty
    1 - Successfully conducted the delete_selected action
    2 - Successfully conducted the new_selection action

    DESCRIPTION
    handles all actions for arbitrary models
    Action:
        "delete selected" - calls the generic delete method, delete_objects(request, model, selected_ids)
    """
    try:
        selected_action = request.POST.__getitem__('action')
        selected_ids = request.POST.getlist('_selected_for_action')
        object_list = model.objects.filter(pk__in=selected_ids)
        if object_list.count() < 1:
            request.user.message_set.create(message='Please select at least one item!')
            return 0
        if selected_action == 'delete_selected':
            try:
                action_approved = request.POST.__getitem__('action_approved')
                if action_approved == '1':
                    delete_objects(request, model, selected_ids)
                    return 1

            except KeyError:
                #action_approved param is not available
                #show the objects check page for delete approval
                context = {
                    'action_name' : selected_action,
                    'object_list' : object_list,
                }
                return render_to_response("crm/object_delete_check.html", context,
                                       context_instance=RequestContext(request))

        if selected_action == 'new_selection':
            #add the selected objects to a new cs selection
            now = datetime.now()
            stamp = now.strftime("%Y-%m-%d/%H:%M")
            cs_name = 'cs_auto_created_' + str(stamp)
            cs = ContactSelection(id=None, name=cs_name)
            cs.created_by = request.user
            cs.save()
            for object in object_list:
                cs.contacts.add(object)
            request.user.message_set.create(message='Successfully created the %s selection' % cs.name)
            return 2

        request.user.message_set.create(message='This action is not available!')
        return 0

    except KeyError:
        request.user.message_set.create(message='Please select an action!')
        return 0

delete_objects (request, model, selected_ids) :

@login_required
def delete_objects(request, model, selected_ids):
    '''
    capsulate a bulk delete method
    delete all objects found for the given model
    fails silently since model.delete() always fails silently
    '''
    object_list = model.objects.filter(pk__in=selected_ids)
    count = object_list.count()
    if count == 1:
        name = model._meta.verbose_name.title()
    else:
        name = model._meta.verbose_name_plural.title()
    object_list.delete()
    request.user.message_set.create(message='Successfully deleted %s %s' % (count,name))
    return

, action_handler , .

+3
1

. , , . , ModelMultipleChoiceField a ChoiceField.

from django import forms

def action_formset(qset, actions):
    """A form factory which returns a form which allows the user to pick a specific action to 
    perform on a chosen subset of items from a queryset.
    """
    class _ActionForm(forms.Form):
        items = forms.ModelMultipleChoiceField(queryset = qset)
        actions = forms.ChoiceField(choices = zip(actions, actions))

    return _ActionForm

#in your views.py

def action_view(request):

    qset = Item.objects.all() #some way to get your items
    actions = ('tuple', 'of', 'action', 'names')

    formclass = action_formset(qset, actions)

    if request.method == 'POST':
        #deal with chosen items
        form = formclass(request.POST)
        chosen_action = form.cleaned_data['action']
        chosen_qset = form.cleaned_data['items']
        #do something with items
        return ...

    #deal with a GET request
    form = formclass()

    return ...

, . , .

+2

Source: https://habr.com/ru/post/1726803/


All Articles