PageRenderTime 204ms CodeModel.GetById 110ms app.highlight 8ms RepoModel.GetById 83ms app.codeStats 0ms

/django/contrib/admin/actions.py

https://code.google.com/p/mango-py/
Python | 85 lines | 75 code | 2 blank | 8 comment | 0 complexity | c15093942ec8f1ef97369b552c590fcb MD5 | raw file
 1"""
 2Built-in, globally-available admin actions.
 3"""
 4
 5from django import template
 6from django.core.exceptions import PermissionDenied
 7from django.contrib.admin import helpers
 8from django.contrib.admin.util import get_deleted_objects, model_ngettext
 9from django.db import router
10from django.shortcuts import render_to_response
11from django.utils.encoding import force_unicode
12from django.utils.translation import ugettext_lazy, ugettext as _
13
14def delete_selected(modeladmin, request, queryset):
15    """
16    Default action which deletes the selected objects.
17
18    This action first displays a confirmation page whichs shows all the
19    deleteable objects, or, if the user has no permission one of the related
20    childs (foreignkeys), a "permission denied" message.
21
22    Next, it delets all selected objects and redirects back to the change list.
23    """
24    opts = modeladmin.model._meta
25    app_label = opts.app_label
26
27    # Check that the user has delete permission for the actual model
28    if not modeladmin.has_delete_permission(request):
29        raise PermissionDenied
30
31    using = router.db_for_write(modeladmin.model)
32
33    # Populate deletable_objects, a data structure of all related objects that
34    # will also be deleted.
35    deletable_objects, perms_needed, protected = get_deleted_objects(
36        queryset, opts, request.user, modeladmin.admin_site, using)
37
38    # The user has already confirmed the deletion.
39    # Do the deletion and return a None to display the change list view again.
40    if request.POST.get('post'):
41        if perms_needed:
42            raise PermissionDenied
43        n = queryset.count()
44        if n:
45            for obj in queryset:
46                obj_display = force_unicode(obj)
47                modeladmin.log_deletion(request, obj, obj_display)
48            queryset.delete()
49            modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
50                "count": n, "items": model_ngettext(modeladmin.opts, n)
51            })
52        # Return None to display the change list page again.
53        return None
54
55    if len(queryset) == 1:
56        objects_name = force_unicode(opts.verbose_name)
57    else:
58        objects_name = force_unicode(opts.verbose_name_plural)
59
60    if perms_needed or protected:
61        title = _("Cannot delete %(name)s") % {"name": objects_name}
62    else:
63        title = _("Are you sure?")
64
65    context = {
66        "title": title,
67        "objects_name": objects_name,
68        "deletable_objects": [deletable_objects],
69        'queryset': queryset,
70        "perms_lacking": perms_needed,
71        "protected": protected,
72        "opts": opts,
73        "root_path": modeladmin.admin_site.root_path,
74        "app_label": app_label,
75        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
76    }
77
78    # Display the confirmation page
79    return render_to_response(modeladmin.delete_selected_confirmation_template or [
80        "admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.object_name.lower()),
81        "admin/%s/delete_selected_confirmation.html" % app_label,
82        "admin/delete_selected_confirmation.html"
83    ], context, context_instance=template.RequestContext(request))
84
85delete_selected.short_description = ugettext_lazy("Delete selected %(verbose_name_plural)s")